在一个构建环境中,我总是不一致地获得UnfinishedStubbingException,而在另一个构建环境中却不一致,并且我已经没有足够的理由了。
这是有问题的代码:
private Answer<Session> answerWithRealSession(String sessionId) {
return new Answer<Session>() {
@Override
public Session answer(InvocationOnMock invocation) throws Throwable {
return sessionManager.getSession(sessionId);
}
};
}
@Test
public void testMethod() throws Exception {
OperatorMessenger mockMessenger = mock(RestOperatorMessenger.class);
validateMockitoUsage();
Answer<Session> answer = answerWithRealSession("some string");
validateMockitoUsage();
doAnswer(answer).when(mockMessenger).getSession();
validateMockitoUsage();
}
}
(如您所见,我试图通过在单独的行中将Answer分配给变量,并添加validateMockitoUsage()调用来消除此错误的某些可能原因。)
在我的本地开发机器上,它运行良好,但是在自动构建中,Mockito表示:
Unfinished stubbing detected here:
-> at testMethod(ReconnectSessionFTest.java:525)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
525行是doAnswer(...行。
我之前曾获得过神秘的UnfinishedStubbingExceptions,但事实证明,我一直在调用某种方法,该方法在同一表达式的一个调用中执行了Mockito的填充,我现在知道这是行不通的,因为Mockito基于跟踪方法调用的顺序。但这似乎不可能是这种情况……
打开任何建议。