单元测试 PersistenceLayer (JPA/EclipseLink)
如果没有准备好的内存数据库,我不确定测试数据库的正确方法是什么。 我有以下 JUnit4 的 TestSuite。
忽略 JpaManager,我只需要这个,因为应用程序作为 Eclipse RCP 运行,而不是在容器中运行,也没有使用 Spring(尚未),所以我无法注入 EntityManager 引用,必须手动处理它。< /em>
public class CustomerJpaTest {
private Customer testCustomer;
@Before
public void setUp() throws Exception {
JpaManager.getInstance().begin();
// create a new user for testing
CustomerJpaDao dao = new CustomerJpaDao();
testCustomer = new Customer();
testCustomer.setName("Someone");
dao.persist(testCustomer);
JpaManager.getInstance().commit();
}
@After
public void tearDown() throws Exception {
// remove previously created user
CustomerJpaDao dao = new CustomerJpaDao();
dao.remove(testCustomer);
JpaManager.getInstance().commit();
JpaManager.getInstance().dispose();
}
@Test
public void testCustomerSaving() throws Exception {
// not sure yet
}
@Test
public void testCustomerLoading() throws Exception {
ICustomerDao dao = new CustomerJpaDao();
Customer customer = dao.findByName("Someone");
assertEquals("Someone", customer.getName());
}
}
由于我在真实的数据库上运行,因此我正在创建我将在 setUp 方法中测试的对象,并在测试完成后将其删除。 这正是我的问题:这个setUp和tearDown实际上也可能是某种测试(testCustomerSaving,testCustomerDelete),但是当我以特定顺序运行测试时,它们不会被隔离(当保存失败,加载也失败,然后删除)。
这样做的正确方法是什么?
I am not sure what is the correct way of testing my Database, without an prepared In-Memory-Database. I have following TestSuite for JUnit4.
Ignore the JpaManager, I just need this, because the application runs as a Eclipse RCP and not in a container, nor Spring is used (yet), so I cannot inject an EntityManager-reference and have to handle it manually.
public class CustomerJpaTest {
private Customer testCustomer;
@Before
public void setUp() throws Exception {
JpaManager.getInstance().begin();
// create a new user for testing
CustomerJpaDao dao = new CustomerJpaDao();
testCustomer = new Customer();
testCustomer.setName("Someone");
dao.persist(testCustomer);
JpaManager.getInstance().commit();
}
@After
public void tearDown() throws Exception {
// remove previously created user
CustomerJpaDao dao = new CustomerJpaDao();
dao.remove(testCustomer);
JpaManager.getInstance().commit();
JpaManager.getInstance().dispose();
}
@Test
public void testCustomerSaving() throws Exception {
// not sure yet
}
@Test
public void testCustomerLoading() throws Exception {
ICustomerDao dao = new CustomerJpaDao();
Customer customer = dao.findByName("Someone");
assertEquals("Someone", customer.getName());
}
}
Since I am running on a real database, I am creating my object that I am going to test in the setUp method and remove it after the test(s) are done. And exactly here is my problem: this setUp and tearDown could be actually also some kind of tests (testCustomerSaving, testCustomerDelete), but when I have the tests running in a particular order then they wont be isolated (when saving fails, loading fails as well, and then deleting).
What is the right way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
setUp
中开始事务并在tearDown
中回滚事务。Begin a transaction in
setUp
and rollback the transaction intearDown
.