Java 测试持久性方法-JUnit
我正在为我的项目编写单元测试,但在调用与数据库一起工作的方法时遇到了一些困难。目前,我想检查一个方法,该方法获取用户感兴趣的发布列表,但我得到的是Java 测试持久性方法-JUnit,java,junit,Java,Junit,我正在为我的项目编写单元测试,但在调用与数据库一起工作的方法时遇到了一些困难。目前,我想检查一个方法,该方法获取用户感兴趣的发布列表,但我得到的是NullPointerException: public class TestPubManager { private PubManager pFunc; @Before public void initialize() { EntityManager manager = PersistenceManager.
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()在同一行代码>。我做错了什么?我找到了解决办法。所以问题出在构造函数中-在注释之前,它没有在@中初始化,但是当我将它放在测试中时,一切正常
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);
}
}
我找到了解决办法。所以问题出在构造函数中-在注释之前,它没有在@中初始化,但是当我将它放在测试中时,一切正常
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);
}
}