I am currently upgrading a project to use JUnit5. I managed to import the necessary dependencies from jupiter and mockito extensions, to make all unit test (both Junit 4 and 5) with mocks run fine in IntelliJ.
At the moment my working solution has:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito-core.version}</version>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
mockito-core.version = 3.0.0
junit.version = 5.2.0
However when i run tests via Maven, the JUnit5 tests are being ignored.
I saw on this blog: https://dev.to/martinbelev/how-to-enable-junit-5-in-new-spring-boot-project-29a8 that spring-boot-starter-test imports JUnit4, and therefore I have to exclude junit from this dependency. However spring-boot-starter-test dependency is being imported from a parent pom, so adding this exclusion:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.0.9.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
has no effect, the effective dependency org.springframework.boot is the one defined on the parent pom with an older version.
Does anyone know how I can solve this? Thank you in advance!
Edit:
test class is:
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());
}
}