我不知道为什么在下面标记了NPE的原因。我嘲笑了对private
方法getLastAcceptedVersion
的调用和对queryDao.getStatus
的公开调用。另外,此刻我仅了解Java 8 lamda,我想这与我要去的地方有关。
public class myClass {
//members
public Optional<Object> getLastAccepted(Long id, Integer version) {
return Optional.ofNullable(getLastAcceptedVersion(id, version))
.map(v -> queryDao.getStatus(id, v)); ------ NULLPOINTEREXCEPTION
}
}
还有我的测试用例:
private MyClass myClass = PowerMockito.spy(new MyClass());
private QueryDao queryDao = mock(QueryDao.class);
private Object obj = mock(Object.class);
@Test
public void testGetLastAccepted() throws Exception{
PowerMockito.doReturn(1).when(myClass, "getLastAcceptedVersion" , anyLong(), anyInt());
when(queryDao.getRegulatoryReportingStatusbyTradeId(anyLong(), anyInt())).thenReturn(obj);
assertNotNull(myClass.getLastAccepted(anyLong(), anyInt()));
}
Stacktrace:
java.lang.NullPointerException
at dao.MyClass.lambda$getLastAccepted$0(myClass .java:57)
at java.util.Optional.map(Optional.java:215)
at dao.MyClass.getLastAccepted(myClass .java:57)