Maven在运行时不会运行@BeforeEach方法

Luc*_*uca 4 java junit maven maven-surefire-plugin junit5

我有一个测试类可以调用它TestSomething,一个测试对象可以调用它SomeObject

现在,我在每项新的单项测试中都需要该对象,这意味着我在我的代码中有一个@BeforeEach将该对象加载到字段中:

import me.test.SomeObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TestSomething {
    private SomeObject someObject;

    @BeforeEach
    public void load() {
        someObject = new SomeObject();
    }

    @Test
    public void test1() {
        boolean result = someObject.checkForSomething();
        Assertions.assertEquals(true, result);
    }

    @Test
    public void test2() {
        boolean result = someObject.checkForSomethingElse();
        Assertions.assertEquals(false, result);
    }
Run Code Online (Sandbox Code Playgroud)

来自测试模块的pom.xml:

<?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">

  <parent>
    <artifactId>test</artifactId>
    <groupId>me.test</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>


  <properties>
    <projectVersion>1.0.0</projectVersion>
    <maven.deploy.skip>false</maven.deploy.skip>
  </properties>


  <artifactId>Tests</artifactId>
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.0.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>me.test</groupId>
      <artifactId>project</artifactId>
      <version>1.0-SNAPSHOT</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

不知道它是否相关,但是对象SomeObject在单独的模块中,并且测试模块对具有Scope的模块有依赖性test。(我也尝试过providedcompile

因此,现在如果我在InteliJ中运行此测试,它们就可以正常工作。但是现在如果我尝试使用NullPointerExceptions来构建我的项目,则测试失败,因为someObjectnull

现在我的测试工作load()在每个测试中都调用方法,但这并不是我想要的。

dav*_*xxx 8

默认情况下,Maven不会使用Jupiter引擎运行测试,因为

为了使Maven Surefire完全运行任何测试,必须将TestEngine实现添加到运行时类路径中。

并且默认情况下不存在。
因此,要启用它,您必须按照Jupiter文档中的说明配置运行单元测试的maven-surefire-plugin :

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <dependencies>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-surefire-provider</artifactId>
                <version>1.2.0</version>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>5.2.0</version>
            </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

不管是什么问题,都不一定很容易发现,因为Maven使用了能够运行Jupiter测试但没有设法执行hook方法的运行程序。

作为提示:要知道是否启动了JUnit 5运行程序,可以用如下冗长的标志执行测试:mvn test -X
如果使用木星奔跑者,您应该找到类似于以下内容的行:

[DEBUG] Surefire报告目录:... \ target \ surefire-reports

[DEBUG]使用已配置的提供程序org.junit.platform.surefire.provider.JUnitPlatformProvider

  • 时光飞逝... Maven Surefire 2.22.0 现在支持开箱即用的 JUnit 5。 (3认同)

khm*_*ise 7

您需要做的第一件事是添加如下依赖项:

<dependencies>
    [...]
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>
    [...]
</dependencies>
Run Code Online (Sandbox Code Playgroud)

接下来是使用最新版本的maven-surefire-plugin/maven-failsafe-plugin ,如下所示

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <excludes>
                    <exclude>some test to exclude here</exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

从版本 2.22.2 开始,surefire 支持 JUnit 5...

更新:

  • 使用最新版本的 maven-surefire/failsafe-plugin