I am writing unit tests for my project, but facing some difficulties when calling methods, which work with database. Currently I want to check a method that gets the list of publications that are of users interest, but I am getting 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 is on the line with int res = pManager.getInterestPubs(2).size();
. What am I doing wrong way?
I have found a solution. So the issue was in the constructor - it was not initializing in @Before
annotation, but when I put it inside the test, everything worked out fine.
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.