我们有 Jenkins 共享库项目,其中包含一些利用 Mockito 的单元测试。将 Jenkins-core 从版本 2.325 升级到 2.326 后,测试开始在以下行失败:
class DSLMock {
DSLMock() {
this.mock = mock(DSL.class)
-> when(mock.invokeMethod(eq("error"), any())).then(new Answer<String>() {
@Override
String answer(InvocationOnMock invocationOnMock) throws Throwable {
throw new AbortException((String) invocationOnMock.getArguments()[1][0])
}
})
...
Run Code Online (Sandbox Code Playgroud)
有错误:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced or misused argument matcher detected here:
-> at com.devops.jenkins.testing.DSLMock.<init>(DSLMock.groovy:66)
-> at com.devops.jenkins.testing.DSLMock.<init>(DSLMock.groovy:66)
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
This message may appear after an …Run Code Online (Sandbox Code Playgroud)