您的当前位置:首页正文

mvn打包报错,程序包org.junit.jupiter.api不存在

2024-11-25 来源:个人技术集锦

mvn 打包报错,具体报错如下:

Error:(3, 29) java: 程序包org.junit.jupiter.api不存在
Error:(9, 6) java: 找不到符号
  符号:   类 Test
  位置: 类 com.test.xxx.XxxApplicationTests

看下pom.xml文件中 spring-boot-starter-test 依赖是否排除了 junit-vintage-engine,如下:

       <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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

重新编译项目,发现导入包 Test 报错,如下:

Error:(3, 29) java: 程序包org.junit.jupiter.api不存在
Error:(9, 6) java: 找不到符号
  符号:   类 Test
  位置: 类 xxx

将报错的import 删除, 重新导包,再次重新编译项目 不在报错

mvn clean package 重新打包项目
又报错

java.lang.Exception: The class com.xxx.xxx.XxxApplicationTests is not public.
java.lang.Exception: Test class should have exactly one public constructor

将上面项目报错的 Test类 改为public, 以及测试方法 contextLoads 也改为 public

@SpringBootTest
public class XxxApplicationTests {

    @Test
    public void contextLoads() {
    }

}

重新打包 mvn clean package ,编译成功

显示全文