小编kir*_*l-a的帖子

Jenkins 核心升级后 Mockito 参数匹配器不正确

我们有 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)

java groovy matcher mockito jenkins

5
推荐指数
1
解决办法
383
查看次数

标签 统计

groovy ×1

java ×1

jenkins ×1

matcher ×1

mockito ×1