我当前正在升级项目以使用JUnit5。 我设法从jupiter和mockito扩展导入了必要的依赖关系,以使所有带有模拟的单元测试(Junit 4和5)在IntelliJ中都能正常运行。
目前我的解决方案有:
<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
但是,当我通过Maven运行测试时,JUnit5测试将被忽略。
我在此博客上看到:https://dev.to/martinbelev/how-to-enable-junit-5-in-new-spring-boot-project-29a8,spring-boot-starter-test
导入了JUnit4,因此我必须从此依赖项中排除junit
。 但是 spring-boot-starter-test
依赖项是从父pom导入的,因此添加此排除项:
<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>
无效,有效依赖项org.springframework.boot
是在较早版本的父pom上定义的依赖项。
有人知道我该如何解决吗?预先谢谢你!
编辑:
测试类为:
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());
}
}
答案 0 :(得分:0)
首先-请验证您的测试班级名称:
如果是,则尝试将junit-platform-surefire-provider
和junit-jupiter-engine
依赖项添加到maven-surefire-plugin
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</plugin>