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

问题描述

我有一个非常简单的JUnit测试类,除非我从pom中删除parent spring-boot-starter-parent,否则我将无法运行它,这对于我们的生产应用程序是不可能的。我们遇到的错误是没有执行测试,下面是带有父项的mvp,只要未注释掉该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文件中定义版本标签。