spring boot build package org.junit不存在

cha*_*sad 2 java spring unit-testing junit4 spring-boot

我在构建环境中构建Spring Boot应用程序时收到以下编译错误.但是,在我的机器上运行Junit测试或构建(打包)它时,我没有看到任何错误 -

src/main/java/com/abc/tests/AbcTest.java:包org.junit不存在

这是我的pom.xml -

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath />
</parent>
<dependencies>........
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

以下是我试过的步骤没有成功 -
1.在依赖部分中明确依赖junit 4.12以及[4.12].

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>  
</dependency> 
Run Code Online (Sandbox Code Playgroud)
  1. 添加带有和不带2.18.1版插件的maven-surefire-plugin
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <junitArtifactName> junit:junit:4.12</junitArtifactName>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以强制构建过程在pom.xml中使用4.12 junit版本而不是选择像3.8这样的旧版本?

谢谢

Mac*_*ski 7

你把测试放在了里面main/java而不是test/java:src/main/java/com/abc/tests/AbcTest.java

Junit依赖项具有,<scope>test</scope>因此它仅test/java在构建时间内的源内可见.