今天在写项目,进行一个方法测试时报错:Error running 'JWTTest.testCreateJWT': Failed to resolve org.junit.platform:junit-platform-launcher:1.10.3
问题原因一:没有导入Junit的依赖:
在使用Spring Initializr创建项目时,一般会自动导入Junit的依赖,看pom.xml中有没有,没有就导入:
<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>
问题原因二:只导入了Junit的依赖,没有导入junit-platform-launcher
依赖;
再导入下面这个测试运行依赖即可成功:
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
具体来说,junit-platform-launcher
的作用包括:
-
发现和执行测试:它负责从测试计划(test plans)中发现和执行测试。这些测试可以是使用 JUnit Jupiter、JUnit Vintage(用于运行旧版本的 JUnit 测试)或其他任何遵循 JUnit Platform 测试引擎 API 编写的测试。
-
支持 IDE 和构建工具:它提供了与 IDE(如 IntelliJ IDEA、Eclipse)和构建工具(如 Maven、Gradle)的集成,使得这些工具能够利用 JUnit Platform 的功能来发现和执行测试。
此外,JUnit 5 还提供了其他相关的依赖,如 junit-jupiter-engine
(JUnit Jupiter 测试引擎实现)和 junit-vintage-engine
(JUnit Vintage 测试引擎实现,用于运行旧版本的 JUnit 测试)。这些依赖可以根据项目的需要进行选择和添加。