3

I am updating a test from JUnit 4 to JUnit 5 (Jupiter).

Among the ordinary annotation adaptations such as @BeforeEach I am using @ExtendWith(MockitoExtension.class) to run the @mocks.

The code is like this:

    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.mockito.Mock;
    import org.mockito.junit.jupiter.MockitoExtension;

    @ExtendWith(MockitoExtension.class)
    public class SomeTest {

        private static final String TEST = "test";

        @Mock
        RetailerService retailerService;
        private Delivery delivery;

        @BeforeEach
        public void setUp() {
            when(retailerService.getMessage(any(String.class))).thenReturn(TEST);
            delivery = new Delivery(retailerService);
        }

        @Test
        public void should_have_delivery() {
            assertEquals(getExpectedDeliveriesDTOs(), delivery.toDtos());
        }

    }

However when I run the test I am getting the following error:

java.lang.NoSuchMethodError: org.mockito.session.MockitoSessionBuilder.initMocks([Ljava/lang/Object;)Lorg/mockito/session/MockitoSessionBuilder;

I saw on this comment: https://stackoverflow.com/a/49655834/2182500 , that this error can be a consequence of having different versions of junit-jupiter-api as dependency in the projects POM and the one in the run scope used by mockito-junit-jupiter.

So I guaranteed to import the same version dependency for Jupiter, but I am still seeing the same error.

pom entries. at surefire level:

<build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
                            <skipTests>true</skipTests>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>org.junit.platform</groupId>
                                <artifactId>junit-platform-surefire-provider</artifactId>
                                <version>1.1.1</version>
                                <scope>test</scope>
                            </dependency>
                            <dependency>
                                <groupId>org.junit.jupiter</groupId>
                                <artifactId>junit-jupiter-engine</artifactId>
                                <version>5.1.0</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                </plugins>
            </build>

at JUnit level:

 <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>2.17.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.1.0</version>
            <scope>test</scope>
        </dependency>

Has anyone bumped into this before and can help? Thank you in advance.

Laguh
  • 621
  • 2
  • 10
  • 23

1 Answers1

4

Turns out this error was related to the Mockito dependencies versions. Thus, failling to initalize mocks.

The project I was working at had an old version of mockito-core artifact that did not have a related mockito-junit-jupiter version. the project needs to maintain mockito-coredue to Junit4 tests mock annotaions. Problem was solved by updating version of mockito core and using same version for mockito-junit-jupiter. Like this:

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>3.0.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>3.0.0</version>
        <scope>test</scope>
    </dependency>
Laguh
  • 621
  • 2
  • 10
  • 23