簡體   English   中英

為什么在 Groovy 中同時使用 mockito-junit-jupiter 和 mockito-inline 會導致 InvalidUseOfMatchersException?

[英]Why does using mockito-junit-jupiter and mockito-inline together in Groovy, results in InvalidUseOfMatchersException?

我在 groovy 代碼中同時使用mockito-junit-jupitermockito-inline來測試 static 類和方法時遇到問題。 希望有人可以幫助我。 我目前的情況是:我正在使用 Groovy、Junit5 和 Mockito。 我的 pom.xml 中的重要部分如下:

     <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.5.8</version>
        <type>pom</type>
        <exclusions>
            <exclusion>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-testng</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Test dependencies -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>${junit5.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.15.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>3.8.0</version>
        <scope>test</scope>
    </dependency>

我的測試工作正常。 現在我聽說,我可以為 static 類和方法編寫測試。 因此,我閱讀了一些帖子,他們都告訴我,我需要在我的 pom 中添加以下依賴項,我可以測試靜態:

      <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-inline</artifactId>
        <version>3.8.0</version>
        <scope>test</scope>
      </dependency>

當我這樣做時,我可以在新測試中使用Mockito.mockStatic ,但現在我使用 Mockito 的所有其他測試都被破壞了,因為出現以下錯誤消息: org.mockito.exceptions.misusing.InvalidUseOfMatchersException

但是據稱我使用了錯誤的參數匹配器的這些行,我沒有改變,它們對我來說仍然很好。 例如: Mockito.when(pipelineConfigLoaderMock.loadResource(Mockito.any(ResourceDefinition.class))).thenThrow(new LoadingResourceFailedException("Mocked Exception"))

有人能告訴我如何在 groovy 中使用mockito-junit-jupitermockito-inline嗎? 我是否使用了不兼容的錯誤依賴項或錯誤版本? 我在這里收到的異常InvalidUseOfMatchersException似乎並沒有顯示真正的問題。 我也已經嘗試過 groovy 2.5.14,但沒有任何改變。

提前謝謝

編輯:

這是一個單元測試的例子,它在我將“mockito-inline”添加到我的 pom 之前有效:

  @Test
  @DisplayName("getMyself - handle 404 NOT_FOUND")
  void testGetMyself_HandleNotFoundResponse() {
    when(simpleRestClientMock.execute(any(RequestEntity.class))).thenAnswer(new Answer<ResponseEntity>() {
      @Override
      ResponseEntity answer(InvocationOnMock invocation) {
        return new ResponseEntity("AnyResponse", HttpStatus.NOT_FOUND)
      }
    })

    assertThatThrownBy({ ->
      underTest.getMyself()
    }).isInstanceOf(RuntimeException.class).hasMessage("Unknown status: 404 NOT_FOUND")
  }

將依賴項添加到 pom 后,我收到以下錯誤:

    org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
    Invalid use of argument matchers!
    0 matchers expected, 1 recorded:
    -> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:55)
    
    This exception may occur if matchers are combined with raw values:
        //incorrect:
        someMethod(anyObject(), "raw String");
    When using matchers, all arguments have to be provided by matchers.
    For example:
        //correct:
        someMethod(anyObject(), eq("String by matcher"));
at com.xxx.xxx.cda.core.http.client.SimpleRestClient.getMetaClass(SimpleRestClient.groovy)
at com.xxx.xxx.cda.pipeline.adapter.JiraAdapterTest.testGetMyself_HandleNotFoundResponse

但是將mockito-inline添加到我的 pom 中,使我能夠為 static 類編寫單元測試,例如:

@Test
void approvalHelperThrowsTimeoutExceptionWithout() {
    ApprovalHelper underTest = new ApprovalHelper()
    underTest = new ApprovalHelper()
    OffsetDateTime startDate = OffsetDateTime.now()
    OffsetDateTime stopDate = startDate.minusHours(1)
    long incrementer = 0
    def closure = { Approval approval ->
        incrementer++
        return false
    }
   Mockito.mockStatic(ApprovalHelper).with {theMock ->
        theMock.when({ ->
            ApprovalHelper.createAndExecTimeoutCommand(Mockito.any(OffsetDateTime.class),
                Mockito.any(OffsetDateTime), Mockito.any(Closure.class)) })
            .thenThrow(new TimeoutException("Mocked Timeout Exception"))
        underTest.aroundApproval(TimeDependent.EMPTY, startDate, stopDate, closure)
        assertThat(incrementer).isEqualTo(0L)
    }
}

如果沒有“mockito-inline”依賴,我會收到以下錯誤:

org.mockito.exceptions.base.MockitoException: 
The used MockMaker SubclassByteBuddyMockMaker does not support the creation of static mocks

Mockito's inline mock maker supports static mocks based on the Instrumentation API.
You can simply enable this mock mode, by placing the 'mockito-inline' artifact where you are currently using 'mockito-core'.
Note that Mockito's inline mock maker is not supported on Android.

我希望這些例子有助於解釋我的問題。

此致

對於您的問題來說為時已晚,但仍想將這個答案放在這里,以便像我這樣的其他人可能會為他們的問題找到答案。

根據 Mockito 文檔,Mockito-core 和 Mockito-inline 不能在一個項目中共存。 您的 mockito-junit-jupiter 依賴已經依賴於 mockito-core,因此間接導致 Mockito-core 和 Mockito-inline 共存。 如果你可以消除 mockito-junit-jupiter 依賴,只留下 mockito-inline,希望它能解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM