首页 > 编程技术 > java

spring boot org.junit.jupiter.api不存在的解决

发布时间:2021-9-18 12:01

org.junit.jupiter.api不存在

解决方案

spring boot2.2之后和之前的测试类编写方法不一致

修改为:

// A code block
import org.junit.Test;import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class CloudstudyApplicationTests {
    @Test
    void contextLoads() {
    }
}

新建Springboot项目默认test包下的测试类报错缺少org.junit.jupiter.api

在springboot项目中碰到一个问题,记录一下:新建了一个普通的Maven项目A,其pom.xml继承parent为:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/>
</parent>

然后在其下新建ModuleB(Springboot项目),奇怪的是新建的Module中的依赖导入后,编译报错(其实使用Maven的install也会报错)

报错如下

显示org.junit.jupiter.api不存在,很明显是缺少对应的依赖。

但是为什么会缺少junit的依赖呢,之前单独建springboot项目时都没有这个问题啊,虽然加上对应的依赖可以解决问题,但还是想找到原因。。。网上找了好久,总算找到满意的答案了。

原因分析

spring boot 2.2 之前使用的是 Junit4 而后续的使用的是Junit5,导致缺少包。我新建的父项目A是用的2.0.3版本的,而后面建的ModuleB选的是2.2.4版本。

解决方案

知道原因解决方法就好说了,升级A的parent为2.2以上版本,test也改为对应的版本。也可直接改ModuleB的test版本为parent对应的2.2以上的版本。都能解决,我当时是简单粗暴的直接加上junit5的依赖。。。如下,,

以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。

标签:[!--infotagslink--]

您可能感兴趣的文章: