我对项目的测试很少,当我执行整个测试类时,我得到No tests were found
,但是当我独立测试每个方法时,它们仍然可以工作,并且我发现JUnit5配置不正确。
以下是我的罐子和pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>...</name>
<description>...</description>
<properties>
<java.version>8</java.version>
<junit-platform.version>5.6.2</junit-platform.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.2.32</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.3.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-platform.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import static org.junit.jupiter.api.Assertions.assertThrows;
@SpringBootTest
public class ApplicationTests {
private RestTemplate restTemplate = new RestTemplate();
private static String BASE_PATH = "http://localhost:8080/exams/";
@Test
public void testNotFoundStartExamException() {
final String url = BASE_PATH + "start/" + null + "/";
assertThrows(HttpClientErrorException.class, () -> restTemplate.getForEntity(url, String.class));
}
@Test
public void testNotFoundFinishExamException() {
final String url = BASE_PATH + "finish-page/" + null + "/";
assertThrows(HttpClientErrorException.NotFound.class, () -> restTemplate.getForEntity(url, String.class));
}
}
当我使用命令mvn test
时,会得到以下结果
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.137 s
[INFO] Finished at: 2020-05-19T08:18:07+03:00
[INFO] ------------------------------------------------------------------------
根据我需要的文档jupiter-engine, platform-launcher, vintage-engine
,我不确定该使用什么,为什么不起作用以及如何使其起作用。我在做什么错了?
答案 0 :(得分:1)
问题是由于重复/多个junit依赖关系被添加到pom.xml中引起的。如前所述,您已经拥有spring-boot-starter-test
依赖项,其中包括junit Jupiter(5.5.2
弹簧启动器的2.2.6.RELEASE
)依赖性。您已经手动添加了更高版本的junit(5.6.2)。
如果您查看Junit 5 User Guide,它会指定junit-jupiter-engine in test runtime scope
,但是由于您未指定任何作用域,因此会编译作用域,您可以通过检查有效Pom对其进行验证。
简单的修复方法是删除手动添加的junit依赖关系,然后运行mvn clean test
。您的测试应该被选中