Lambda表达式为NULLPOINTER

时间:2018-11-15 11:10:52

标签: java junit mockito powermockito

我不知道为什么在下面标记了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)

0 个答案:

没有答案