【问题标题】:Maven does not run @BeforeEach Methods while runningMaven 在运行时不运行 @BeforeEach 方法
【发布时间】:2018-12-25 05:07:03
【问题描述】:

我有一个测试类让我们称之为TestSomething,一个测试对象让我们称之为SomeObject

现在我在每个新的 Single Test 中都需要这个对象,这意味着我的代码中有一个 @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);
    }

来自测试模块的 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>

不确定它是否相关,但对象SomeObject 位于单独的模块中,并且测试模块对该模块具有依赖关系,范围为test。 (我也试过providedcompile

所以现在如果我在 InteliJ 中运行这个测试,它们就可以正常工作。但是现在如果我尝试构建我的项目测试失败,因为someObjectnull

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

【问题讨论】:

  • 显示你的 Maven 配置。也许,您没有按照应有的方式配置 maven-surefire-plugin。请在测试类中显示您的导入。
  • 添加了你所说的所有内容,但没有特别配置surefile插件
  • 所以问题就在那里。看看:junit.org/junit5/docs/current/user-guide/…
  • 当您忘记将 junit-jupiter-engine 工件添加为依赖项时,surefire 插件仍然不会拾取 @BeforeEach 带注释的方法。当您添加了 junit-jupiter-api 工件时,IDE 是内容,但是当您也未指定引擎时,surefire 插件将恢复为 junit3 运行器。 Surefire 仍然会运行您的测试,即使 @Test 注释来自 org.junit.jupiter.api 包。但是,@BeforeEach(和@AfterEach 等)注解的方法不会被执行。

标签: java maven junit maven-surefire-plugin junit5


【解决方案1】:

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

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

默认情况下不存在。
因此,要启用它,您必须配置运行单元测试的 maven-surefire-plugin,如 Jupiter documentation 中所述:

更新(2020 年 10 月 28 日):

从 2.22.0 版开始,您只需指定所需 junit 引擎的测试依赖项。否则,也会导致问题中描述的行为。

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

保留原始答案作为参考,在 2.22.0 版本之前解决方案是:

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

无论什么问题都不一定容易发现,因为 Maven 使用了一个能够运行 Jupiter 测试但无法执行钩子方法的运行器...

作为提示:要知道 JUnit 5 运行器是否已启动,您可以使用详细标志执行测试,例如:mvn test -X
如果使用 Jupiter runner,您应该会找到如下所示的线条:

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

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

【讨论】:

  • 解决了这个问题,谢谢:D 我只需要将它添加到我的测试模块的 pom 中。
  • 不客气 :) JUnit 5 还不是标准。所以maven默认不包含它。但几年后,我希望它应该改变!我更新了答案。这可能会有所帮助。
  • 时光飞逝...Maven Surefire 2.22.0 现在支持开箱即用的 JUnit 5。
  • 只是想补充一点,如果您使用 Spring Boot,您必须从 spring-boot-starter-test 依赖项中排除 junit-vintage-engine 库。因为我们在测试中使用了一些 junit 4 注释,以某种方式使用 junit4 运行器而不是 junit 5 运行测试。在排除依赖关系并将所有 junit4 注释重构为 junit5 之后,它起作用了!
  • 正如@MarcPhilipp 已经提到的,2.22.0 版支持 JUnit,而不需要 junit-platform-surefire-provider。您仍然必须将测试依赖项包含到正确的测试引擎中。我相应地更新了答案。
【解决方案2】:

首先你需要添加一个这样的依赖:

<dependencies>
    [...]
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.2.0</version>
        <scope>test</scope>
    </dependency>
    [...]
</dependencies>

接下来就是使用最新版本的maven-surefire-plugin/maven-failsafe-plugin like this

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <excludes>
                    <exclude>some test to exclude here</exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

从 2.22.0 版开始,surefire 支持 JUnit 5...

【讨论】:

    【解决方案3】:

    maven-surefire-plugin 默认采用所有具有 *Test.java 模式的测试类,在您的情况下,您应该重命名该类 SomethingTest,它应该没问题

    https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

    【讨论】:

    猜你喜欢
    • 2019-03-04
    • 2023-03-07
    • 2021-07-08
    • 2016-01-23
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    相关资源
    最近更新 更多