gpt4 book ai didi

java - GAE 测试环境中的数据存储在 JUnit 测试之间泄漏数据

转载 作者:行者123 更新时间:2023-12-01 05:30:30 25 4
gpt4 key购买 nike

我想使用 Junit 在本地对 Google App Engine 进行一些自动化数据存储测试。

我编写了一个类“Agent.java”,其中包含三个字符串“name”、“owner”和“url”。类“Player”是抽象的,但不提供附加属性。

public class Agent extends Player implements Serializable {

/** to serialize Agent */
private static final long serialVersionUID = -6859912740484191335L;

/** The name of the Agent is the key-element of the agent-class*/
@Id String name;
/** Url to the Agent */
String url;

@Index String owner;
...

其次是 Setter 和 Getter。

我已将 4 个所需的库从 sdk 1.6.0 复制到项目“war/WEB-INF/lib”文件夹中,并包含 Junit4 容器。

我的测试类如下所示:

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Logger;

import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.code.twig.annotation.AnnotationObjectDatastore;

public class AgentContrTest {

private static final Logger log = Logger.getLogger("AgentContrTest.class");

private static UserController uc;
private static GameController gc;
private static AgentController ac;

private static final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

private AnnotationObjectDatastore datastore = new AnnotationObjectDatastore(false);


@BeforeClass
public static void setUpOnce() {

gc = GameController.getInstance();
uc = UserController.getInstance();
ac = AgentController.getInstance();

}

@Before
public void setUp() {
helper.setUp();
try {
uc.register("userForTest", "test", "test@gmail.de", false);

}
catch (NameExistsException ne) {
}
catch (EmailFormatException ee) {
}
}

@After
public void tearDown() {
helper.tearDown();
}

// Testing the raising of NameExistsException in createAgent(String name, String url, String owner)
@Test(expected=NameExistsException.class)
public void testCreateAgentExc1() throws NameExistsException {

Agent ag1 = ac.createAgent("Agent1", "www.agent1.com", "Owner1");
Agent ag2 = ac.createAgent("Agent1", "www.agent2.com", "Owner2");
}

// Testing getAgents()
@Test
public void testGetAgents1() throws NameExistsException {
datastore.disassociateAll();
ArrayList<Agent> agents1 = ac.getAgents();

ac.createAgent("Agent1", "www.agent1.com", "Owner1");
ac.createAgent("Agent2", "www.agent2.com", "Owner2");
ac.createAgent("Agent3", "www.agent3.com", "userForTest");

ArrayList<Agent> agents2 = ac.getAgents();

assertTrue(agents1.size()==0);
assertTrue(agents2.size()==3);
datastore.disassociateAll();
}

// Testing getAgents(String user)
@Test
public void testGetAgents2() throws NameExistsException {
ArrayList<Agent> agents = ac.getAgents();
assertTrue(agents.size()==0);
datastore.disassociateAll();

ac.createAgent("Agent1", "www.agent1.com", "Owner1");
ac.createAgent("Agent2", "www.agent2.com", "Owner2");
ac.createAgent("Agent3", "www.agent3.com", "userForTest");


ArrayList<Agent> agents2 = ac.getAgents("userForTest");

assertTrue(agents2.size()==1);
}

这些是我正在测试的 AgentController 中的函数:

public ArrayList<Agent> getAgents(String user) {

ArrayList<Agent> agents = new ArrayList<Agent>();

Iterator<Agent> agentIterator = datastore.find().type(Agent.class)
.addFilter("owner", FilterOperator.EQUAL, user)
.now();

while (agentIterator.hasNext()) {
agents.add(agentIterator.next());
}

return agents;

}

public Agent createAgent(String name, String url, String owner) throws NameExistsException {

Agent agent = datastore.load(Agent.class, name);
if (agent != null)
throw new NameExistsException();

agent = new Agent();
agent.setName(name);
agent.setUrl(url);
agent.setOwner(owner);

datastore.store(agent);

return agent;

}

testCreateAgentExc1 工作正常。但是 testGetAgents2() 抛出了 NameExistsException,这是它不应该做的。如果我将此测试中的代理重命名为“Agent4”到“Agent6”,它就可以正常工作。

由于 'http://code.google.com/intl/de-DE/appengine/docs/java/tools/localunittesting.html '数据存储区应删除测试之间的所有数据,因此不应引发 NameExistsException

最佳答案

您没有在测试之间重置datastore对象。我不确定 twig 是如何工作的,但它(或其配置)是泄漏的原因。

关于java - GAE 测试环境中的数据存储在 JUnit 测试之间泄漏数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9067467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com