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.