为什么我会得到 NullPointerException?
这是我的代码:
@Stateless
@LocalBean
class SomeDao {
@PersistenceContext(unitName = "some-value")
private EntityManager entityManager;
public EntityManager getEntityManager() {
return this.entityManager;
}
public long getNextId() {
long someLongValue = getEntityManager().someMethod();
//some code
return someLongValue;
}
}
class SomeTest() {
@Spy
private SomeDao dao = new SomeDao();
@Test
public void someTestMethod() {
MockitoAnnotations.initMocks(this);
when(dao.getNextId()).thenReturn(10L);
}
}
当我运行测试时,出现以下异常: java.lang.NullPointerException at com.some.api.some.package.dao.SomeDao.getNextId(SomeDao.java:13) ...
稍后我想添加新的类来模拟,getNextId 方法将在其中一个中调用。
谢谢!