简体   繁体   中英

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

I have a problem with using mockito-junit-jupiter and mockito-inline together in my groovy code, for testing static classes and methods. Hopefully someone can help me. My current situation is: I am using Groovy, Junit5 and Mockito. The important parts in my pom.xml are the following:

     <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>

My Tests are working fine. Now I heard, that I can write tests for static classes and methods. Therefor I read a few posts and in all of them they tell me, that I need to add following dependency on top in my pom and i can test statics:

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

When I do that, I can use Mockito.mockStatic in new tests, but now all my other tests, where I use Mockito are broken, as following error message appears: org.mockito.exceptions.misusing.InvalidUseOfMatchersException

But these lines in which I allegedly use wrong argument matchers, I didnt change and they still look totally fine for me. For example: Mockito.when(pipelineConfigLoaderMock.loadResource(Mockito.any(ResourceDefinition.class))).thenThrow(new LoadingResourceFailedException("Mocked Exception"))

Can someone tell me how to use mockito-junit-jupiter along with mockito-inline in groovy? Do i use wrong dependencies or wrong versions which are not compatible together? The exception InvalidUseOfMatchersException , that I receive here, doesn't seem to show the real problem. I already tried groovy 2.5.14 as well, but nothing changed.

Thx in advance

Edit:

Here is a example of an unit-test, which works before I added 'mockito-inline' to my 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")
  }

After adding the dependency to the pom I receive following error:

    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

But adding mockito-inline to my pom, enables me to write an unit-test for static classes, that works, for example:

@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)
    }
}

Without 'mockito-inline' dependency I would receive following error:

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.

I hope these examples are helpful to explain my problem.

Best regards

It is too late for your problem but still wanted to put this answer here so that someone else like me might find an answer for their problem.

According to Mockito documentation, Mockito-core and Mockito-inline cannot co-exist in a project. Your mockito-junit-jupiter dependency already depends on mockito-core, thus indirectly causing Mockito-core and Mockito-inline co-exist. If you can eliminate mockito-junit-jupiter dependency and leave only mockito-inline, hopefully it will solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM