[英]Testing persistence methods - JUnit
我正在为我的项目编写单元测试,但在调用与数据库一起使用的方法时遇到一些困难。 目前我想检查一个获取用户感兴趣的出版物列表的方法,但是我得到了NullPointerException
:
public class TestPubManager {
private PubManager pFunc;
@Before
public void initialize() {
EntityManager manager = PersistenceManager.INSTANCE.getEntityManager();
em.getTransaction().begin();
PubManager pManager = new PubManager(manager);
}
@Test
public void testGetInterestPubs() {
int res = pManager.getInterestPubs(2).size();
assertEquals(20, res);
}
}
NullPointerException与int res = pManager.getInterestPubs(2).size();
。 我做错了什么?
我找到了解决方案。 所以问题出在构造函数中 - 它没有在@Before
注释中初始化,但是当我把它放在测试中时,一切都很好。
public class TestPubManager {
private PubManager pFunc;
EntityManager manager = PersistenceManager.INSTANCE.getEntityManager();
@Before
public void initialize() {
em.getTransaction().begin();
}
@Test
public void testGetInterestPubs() {
PubManager pManager = new PubManager(manager);
int res = pManager.getInterestPubs(2).size();
assertEquals(20, res);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.