Maven Surefire没有运行JUnit 5测试 [英] Maven Surefire not running JUnit 5 tests

查看:99
本文介绍了Maven Surefire没有运行JUnit 5测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Maven Surefire运行JUnit 5测试.但是,即使我确实在默认目录中也进行了测试,Surefire似乎也根本没有运行任何测试.

这是我得到的控制台输出: https://prnt.sc/ugo1xt

以下是pom.xml的相关部分:

  <dependencies>
    <dependency>
       <groupId>org.junit.jupiter</groupId>
       <artifactId>junit-jupiter</artifactId>
       <version>5.7.0-M1</version>
       <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${maven-surefire-plugin.version}</version>
      </plugin>
    </plugins>
  </build>


surefire版本为3.0.0-M4.

我已经尝试了几乎可以在Google上找到的所有修复程序,尽管其中大多数似乎已经过时了.任何帮助将不胜感激.

干杯!

这是我的测试示例:

package bankprojekt;

import bankprojekt.verarbeitung.*;

import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class GirokontoTest {
    Girokonto gk;

    @BeforeEach
    void setup(){
        gk = new Girokonto();
        gk.einzahlen(500);
    }

    @AfterEach
    void teardown(){
        gk = null;
    }

    @Test
    void abhebenMitWaehrungswechsel(){
        try{
            gk.abheben(195.583, Waehrung.BGN);
        }
        catch (Exception e) {
            System.out.println(e);
        }

        assertEquals(400, gk.getKontostand());
    }

    @Test
    void einzahlenMitWaehrungswechsel(){
        gk.einzahlen(195.583, Waehrung.BGN);

        assertEquals(600, gk.getKontostand());
    }
}

解决方案

有两件事.首先,您应该将jacoco依赖项升级到0.8.5,否则根据JDK14要求它将失败. (创建了对您存储库的拉取请求).如果正确配置了这些内容,结果将是这样:

我还建议将maven-compiler-plugin升级到最新版本以及所有其他插件.

如您所见,您的内部版本中有警告,应予以修复,并通过测试.

此构建也已通过普通命令行与JUnit-Jupiter 5.7.0一起运行,效果很好.

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ prog3-sose2020 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/classes
[WARNING] /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java: /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java uses or overrides a deprecated API.
[WARNING] /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java: Recompile with -Xlint:deprecation for details.
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ prog3-sose2020 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/khmarbaise/ws-git-so/prog3-sose2020/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ prog3-sose2020 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ prog3-sose2020 ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running bankprojekt.GirokontoTest
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.087 s <<< FAILURE! - in bankprojekt.GirokontoTest
[ERROR] bankprojekt.GirokontoTest.abhebenMitWaehrungswechsel  Time elapsed: 0.052 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: <448.87081188037814> but was: <400.0>
    at bankprojekt.GirokontoTest.abhebenMitWaehrungswechsel(GirokontoTest.java:31)

[INFO] Running bankprojekt.WaehrungTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 s - in bankprojekt.WaehrungTest
Kunde Mustermann, Max zerst�rt
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   GirokontoTest.abhebenMitWaehrungswechsel:31 expected: <448.87081188037814> but was: <400.0>
[INFO] 
[ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.792 s
[INFO] Finished at: 2020-09-14T17:50:43+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M4:test (default-test) on project prog3-sose2020: There are test failures.
[ERROR] 
[ERROR] Please refer to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

I've been trying to run JUnit 5 tests with Maven Surefire. However, it doesn't seem like Surefire is running any tests at all, even though I do have some, and in the default directory, too.

This is the console output I'm getting: https://prnt.sc/ugo1xt

Here are the relevant parts of the pom.xml:

  <dependencies>
    <dependency>
       <groupId>org.junit.jupiter</groupId>
       <artifactId>junit-jupiter</artifactId>
       <version>5.7.0-M1</version>
       <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${maven-surefire-plugin.version}</version>
      </plugin>
    </plugins>
  </build>


The surefire version is 3.0.0-M4.

I've tried pretty much any fix I could find on Google, although most of them seemed to be outdated. Any help would be greatly appreciated.

Cheers!

EDIT: Here's an example of my tests:

package bankprojekt;

import bankprojekt.verarbeitung.*;

import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class GirokontoTest {
    Girokonto gk;

    @BeforeEach
    void setup(){
        gk = new Girokonto();
        gk.einzahlen(500);
    }

    @AfterEach
    void teardown(){
        gk = null;
    }

    @Test
    void abhebenMitWaehrungswechsel(){
        try{
            gk.abheben(195.583, Waehrung.BGN);
        }
        catch (Exception e) {
            System.out.println(e);
        }

        assertEquals(400, gk.getKontostand());
    }

    @Test
    void einzahlenMitWaehrungswechsel(){
        gk.einzahlen(195.583, Waehrung.BGN);

        assertEquals(600, gk.getKontostand());
    }
}

解决方案

There are two things. First you should upgrade jacoco dependency to 0.8.5 otherwise it fails based on the JDK14 requirement. (Created an pull request to your repository). If those things are configured appropriately the result is this:

I would also recommend to upgrade maven-compiler-plugin to most recent version and also all other plugins.

As you can see you have WARNING's in your build which should be fixed and failing tests.

This build has been run on plain command line also with JUnit-Jupiter 5.7.0 which works perfectly.

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ prog3-sose2020 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/classes
[WARNING] /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java: /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java uses or overrides a deprecated API.
[WARNING] /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java: Recompile with -Xlint:deprecation for details.
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ prog3-sose2020 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/khmarbaise/ws-git-so/prog3-sose2020/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ prog3-sose2020 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ prog3-sose2020 ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running bankprojekt.GirokontoTest
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.087 s <<< FAILURE! - in bankprojekt.GirokontoTest
[ERROR] bankprojekt.GirokontoTest.abhebenMitWaehrungswechsel  Time elapsed: 0.052 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: <448.87081188037814> but was: <400.0>
    at bankprojekt.GirokontoTest.abhebenMitWaehrungswechsel(GirokontoTest.java:31)

[INFO] Running bankprojekt.WaehrungTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 s - in bankprojekt.WaehrungTest
Kunde Mustermann, Max zerst�rt
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   GirokontoTest.abhebenMitWaehrungswechsel:31 expected: <448.87081188037814> but was: <400.0>
[INFO] 
[ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.792 s
[INFO] Finished at: 2020-09-14T17:50:43+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M4:test (default-test) on project prog3-sose2020: There are test failures.
[ERROR] 
[ERROR] Please refer to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

这篇关于Maven Surefire没有运行JUnit 5测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆