有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java测试在JUnit4下运行,但不是在JUnit5下运行-编译干净,但执行0测试

任何人都可以很容易地在几分钟内重现这个问题

基本Mavenquickstart项目

通过IntelliJ 2018.3和Maven 3.6.0,我使用Maven原型^{}版本1.4创建了一个全新的项目

enter image description here

爪哇11

在新项目的POM文件中,我将maven.compiler.sourcemaven.compiler.target的属性从1.7更改为11,将我当前使用的Java 11.0.2的属性从Azul Systems更改为Zulu

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>11</maven.compiler.source>
  <maven.compiler.target>11</maven.compiler.target>
</properties>

在IntelliJ的Maven面板上,我运行cleaninstall生命周期事件

enter image description here

JUnit4中的测试运行

作为install的一部分,将运行测试。这个quickstart原型带有一个断言true的测试

enter image description here

结果显示在IntelliJ的Run面板中

[INFO] Running work.basil.example.AppTest

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.026 s - in work.basil.example.AppTest

所以我知道测试已经执行了

JUnit 5,而不是4

这一切都很好。现在让我们升级到JUnit5,看看问题所在

在POM中,我将JUnit依赖项更改为:

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
</dependencies>

……为此:

<dependencies>
  <!--JUnit 5-->
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.3.2</version>
    <scope>test</scope>
  </dependency>
</dependencies>

Jupiter进口(无复古测试)

编译器抱怨我的AppTest.java文件。因此,我将那里的import语句更改为使用jupiter包。我只想在新的greedfield项目中运行JUnit5测试,而不需要老式的JUnit4测试。因此,导入会从以下方面发生变化:

import static org.junit.Assert.assertTrue;
import org.junit.Test;

……为此:

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

然后我执行Maven>Lifecycle>clean&install

…瞧,问题是:我们的测试没有执行。IntelliJ的{}小组报告:

[INFO] Running work.basil.example.AppTest

[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 s - in work.basil.example.AppTest

➥ 为什么JUnit5不能运行与JUnit4非常愉快地运行的测试相同的测试

更新surefire插件

我怀疑Maven Surefire Plugin需要更新。因此,在POM中,我改变了这一点:

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.22.1</version>
</plugin>

……为此:

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M3</version>
</plugin>

另一个{}&install。但没有更好,仍然运行0个测试

[INFO] Running work.basil.example.AppTest

[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 s - in work.basil.example.AppTest

全聚甲醛

这是我的整个POM文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>work.basil.example</groupId>
  <artifactId>tester</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>tester</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

  <dependencies>
    <!--JUnit 5-->
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.3.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.0.0-M3</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

JUnit库

在完成Mavenclean&install,出现两个JUnit库:junit-jupiter-apijunit-platform-commons

enter image description here

JUnit5的其他版本

我在junit-jupiter-api依赖项中尝试了以下版本:

  • 5.0.0-M1
  • 5.1.1
  • 5.3.0
  • 5.3.2
  • 5.4.0-M1

每次尝试时,我都运行一个Maven{}&install。再好不过了。这些版本中的每一个都报告了Tests run: 0

不要责怪maven-archetype-quickstart

事实上,我在一个完全不同的Maven原型的项目中发现了这个问题

为了解决这个问题,我尝试了一个新的项目,使用非常简单的maven-archetype-quickstart。我发现了完全相同的行为:一切都在编译,测试工具正在运行,但JUnit5下没有执行任何测试


共 (1) 个答案

  1. # 1 楼答案

    tl;博士

    对于JUnit5版本5.4.0-M1或更高版本,在POM中指定新的单个Maven工件^{}“聚合器”

    <! JUnit 5 >
    <!  https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter  >
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.4.0-M1</version>
    </dependency>
    

    对于早期版本,至少指定这两个工件:^{}&^{}

    JUnit5支持多种测试框架

    据我所知,JUnit5已经被重新设计成多个测试框架的枷锁。这些测试系统包括JUnit4“老式”测试、新的JUnit5测试(测试的新语法、新的注释和方法)以及其他测试系统,如SpecsySpekCucumber、Drools场景、jqwikmore that implement接口

    显然junit-jupiter-api工件只是外轭。您还必须指定一个或多个^{}实现来实际运行测试。例如,要运行老式JUnit4测试,需要^{}实现;要运行JUnit5测试,需要^{}实现

    因此,要运行JUnit5测试,必须在Maven POM中使用junit-jupiter-engine工件指定JupiterTestEngine实现

    请参阅JUnit5手册,特别是Configuring Test Engines一节

    参见Marc Philipp的this presentation,图中显示了JUnit 5作为一个平台,具有(a)IDE/构建工具的核心,以及(B)程序员编写测试的可插入测试编写框架

    junit-jupiter-engine

    this sample上所示,为JUNit Jupiter Engine添加第二个与JUnit相关的依赖项。{a17}简单地说:“JUnit Jupiter测试引擎实现,仅在运行时需要。”

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.0-M1</version>
        <scope>test</scope>
    </dependency>
    

    只需将一个依赖项添加到问题中所示的项目中,测试就会运行

    [INFO] Running work.basil.example.AppTest

    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 s - in work.basil.example.AppTest


    junit-jupiter-params

    该示例还显示了第三个JUnit依赖项,用于JUnit Jupiter Params。虽然不需要进行示例测试运行,但它可以用于其他目的。显然与Parameterized Tests有关

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.4.0-M1</version>
        <scope>test</scope>
    </dependency>
    

    这使得总共有3个JUnit依赖项

    <! JUnit 5 >
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.4.0-M1</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.4.0-M1</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.0-M1</version>
        <scope>test</scope>
    </dependency>
    

    您的相同POM文件,现在已更新为所有3个JUnit依赖项

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>work.basil.example</groupId>
        <artifactId>tester</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <name>tester</name>
        <!  FIXME change it to the project's website  >
        <url>http://www.example.com</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>11</maven.compiler.source>
            <maven.compiler.target>11</maven.compiler.target>
        </properties>
    
        <dependencies>
    
            <! JUnit 5 >
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.4.0-M1</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-params</artifactId>
                <version>5.4.0-M1</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>5.4.0-M1</version>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <pluginManagement><!  lock down plugins versions to avoid using Maven defaults (may be moved to parent pom)  >
                <plugins>
                    <!  clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle  >
                    <plugin>
                        <artifactId>maven-clean-plugin</artifactId>
                        <version>3.1.0</version>
                    </plugin>
                    <!  default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging  >
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>3.0.2</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.8.0</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>3.0.0-M3</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <version>3.0.2</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-install-plugin</artifactId>
                        <version>2.5.2</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-deploy-plugin</artifactId>
                        <version>2.8.2</version>
                    </plugin>
                    <!  site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle  >
                    <plugin>
                        <artifactId>maven-site-plugin</artifactId>
                        <version>3.7.1</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-project-info-reports-plugin</artifactId>
                        <version>3.0.0</version>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>
    

    junit-jupiter伪影

    JUnit 5的5.4.0版本带来了一个新的Maven工件,^{},标题为JUnit Jupiter(聚合器)。“aggregator*一词显然是指它将一些常用的JUnit5构件捆绑在Maven中,以方便我们的编程

    在POM中添加这一个dependency可以在项目中获得8个库

    <! JUnit 5 >
    <!  https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter  >
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.4.0-M1</version>
    </dependency>
    

    Screenshot of IntelliJ panes for project structure and POM file contents, where a single dependency for <code>junit-jupiter</code> adds eight libraries to your project, all you need to run JUnit 5 tests.