问题描述
所以我们收到了消息
Process finished with exit code 0
Empty test suite.
当尝试在我们的多模块项目上运行Junit5测试用例时,为了测试问题的根源,我决定从头开始尝试一个新项目(我使用的是IntelliJ IDEA 2018.2.4).
When trying to run Junit5 testscases on our multi module project, so for testing the sources of the issue, I decided to try a new project from scratch (I am using IntelliJ IDEA 2018.2.4).
以下简单的项目POM文件出现了相同的问题:
I am getting the same issue with the following simple project POM file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.org.bu.dep.app</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.0.5.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我尝试使用/不使用junit-vintage-engine同样的结果.
I tried with/without the junit-vintage-engine same results.
以下是测试类:
package com.org.bu.dep.app.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class DemoApplicationTests {
@Test
public void contextLoads() {
Assertions.assertTrue(true);
}
}
在运行"窗口中,显示为No tests were found
.
On the Run window, it says No tests were found
.
我尝试了许多变体,例如带/不带@SpringBootTest
注释,也尝试了@ExtendWith(SpringExtension.class)
,但无济于事.
I tried many variations such as with/without @SpringBootTest
annotation, I tried @ExtendWith(SpringExtension.class)
as well to no avail.
我已经在Google上搜索了,大多数结果/SackOverflow答案似乎来自2016/2017年,重点是较老版本的IntelliJ,该版本与Junit 5存在问题,似乎没有关系.
I already googled and most results/SackOverflow answers seem to be from 2016/2017 focused on older versions of IntelliJ which had issues with Junit 5, which do not seem to be related.
请问有什么建议吗?如果我们花太多时间将其恢复为4,我非常不希望这样做:)
Please, any advice?If we spend too much time on it will be forced to revert to 4, and I very much do not wish to do that :)
推荐答案
因此,在Spring Boot 2上使用Junit 5的有效方法是以下3种方法:
So, what works for Junit 5 with Spring Boot 2 is the following 3 TOGETHER:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
通过空项目进行测试和验证.
Tested and verified with empty project.
请注意,@ Michael Legart的答案建议添加
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
Maven测试(和Jenkins)需要检测和执行测试!
Is needed for maven test (and Jenkins) to detect and execute the tests!
这篇关于找不到测试-在裸骨Spring Boot Maven项目上运行jUnit 5测试用例时,测试套件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!