使用 JUnit5 和 Spring-boot-starter-parent 时未执行任何测试

No Tests Were Executed when using JUnit5 and Spring-boot-starter-parent

我有一个非常简单的 JUnit 测试 class 我不能 运行 除非我从 pom 中删除 parent spring-boot-starter-parent,这对于我们的生产应用程序是不可能的。我们遇到的错误是没有执行任何测试,下面是带有父片段的 mvp,只要没有注释掉就会阻止测试。如果我能得到任何指导以了解如何解决这个问题,请。

<?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>
 
<!-- Piece to be disabled for tests to run -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

<groupId>com.hmhco</groupId>
<artifactId>crs-v2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>crs-v2</name>
<description>Content Recommendation Service V2</description>

<properties>
    <java.version>1.8</java.version>
    <junit.jupiter.version>5.6.2</junit.jupiter.version>
    <maven.surefire.version>3.0.0-M5</maven.surefire.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.version}</version>
        </plugin>
    </plugins>
</build>
import org.junit.jupiter.api.Test;

public class TestingTest {

@Test
public void checking() {
    System.out.println("checking------------");
}
}

谢谢。

问题与您尝试使用不同版本的 junit jupiter 有关。 spring-boot-parent (2.2.4.RELEASE) is 5.5.2预定义的版本。最简单的解决方案是删除 junit-jupiter 部分(版本标签)的版本并使用从 spring-boot-parent 继承的版本。我能给出的最佳建议是从使用较新版本的 Spring Boot(2.3.3.RELEASE 最新版本)开始。

如果你不能这样做,你必须使用 junit-bom 文件,而不是像这样:

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.6.2</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>

那么它应该可以正常工作。之后,您必须在 pom.xml 文件中使用依赖项而无需再定义版本标记。