Maven Surefire未运行JUnit 5测试

huangapple go评论75阅读模式
英文:

Maven Surefire not running JUnit 5 tests

问题

以下是翻译好的部分:

我一直在尝试使用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。

尽管我尝试了谷歌上找到的几乎所有修复方法,但大多数方法似乎都已过时。非常感谢任何帮助。

编辑:这里是我的测试示例:

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());
    }
}
英文:

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:

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


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());
    }
}

答案1

得分: 0

以下是翻译好的内容:

有两件事情。首先,您应该将 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] 检测到更改 - 重新编译模块!
[INFO] 编译 9 个源文件至 /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 使用或覆盖了已弃用的 API。
[WARNING] /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java:重新编译时使用 -Xlint:deprecation 以获取详细信息。
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ prog3-sose2020 ---
[INFO] 使用 'UTF-8' 编码复制过滤后的资源。
[INFO] 跳过不存在的 resourceDirectory /Users/khmarbaise/ws-git-so/prog3-sose2020/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ prog3-sose2020 ---
[INFO] 检测到更改 - 重新编译模块!
[INFO] 编译 3 个源文件至 /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]  测 试
[INFO] -------------------------------------------------------
[INFO] 正在运行 bankprojekt.GirokontoTest
[ERROR] 运行的测试数:2,失败:1,错误:0,跳过:0,耗时:0.087 秒 <<< 失败! - 在 bankprojekt.GirokontoTest 中
[ERROR] bankprojekt.GirokontoTest.abhebenMitWaehrungswechsel  耗时:0.052 秒  <<< 失败!
org.opentest4j.AssertionFailedError:期望值:<448.87081188037814>,实际值:<400.0>
    	at bankprojekt.GirokontoTest.abhebenMitWaehrungswechsel(GirokontoTest.java:31)

[INFO] 正在运行 bankprojekt.WaehrungTest
[INFO] 运行的测试数:3,失败:0,错误:0,跳过:0,耗时:0.005 秒 - 在 bankprojekt.WaehrungTest 中
Kunde Mustermann,Max zerstört
[INFO]
[INFO] 结果:
[INFO]
[ERROR] 失败:
[ERROR]   GirokontoTest.abhebenMitWaehrungswechsel:31 期望值:<448.87081188037814>,实际值:<400.0>
[INFO]
[ERROR] 运行的测试数:5,失败:1,错误:0,跳过:0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] 构建失败
[INFO] ------------------------------------------------------------------------
[INFO] 总耗时:3.792 秒
[INFO] 完成于:2020-09-14T17:50:43+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] 无法执行目标 org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M4:test (default-test) 项目 prog3-sose2020 上的测试:存在测试失败。
[ERROR]
[ERROR] 请参阅 /Users/khmarbaise/ws-git-so/prog3-sose2020/target/surefire-reports 以获取单个测试结果。
[ERROR] 请参阅转储文件(如果存在) [date].dump,[date]-jvmRun[N].dump 和 [date].dumpstream。
[ERROR] -&gt; [Help 1]
[ERROR]
[ERROR] 要查看错误的完整堆栈跟踪,请使用 -e 开关重新运行 Maven。
[ERROR] 使用 -X 开关重新运行 Maven,以启用完整的调试日志记录。
英文:

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 &#39;UTF-8&#39; 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 &lt;&lt;&lt; FAILURE! - in bankprojekt.GirokontoTest
[ERROR] bankprojekt.GirokontoTest.abhebenMitWaehrungswechsel  Time elapsed: 0.052 s  &lt;&lt;&lt; FAILURE!
org.opentest4j.AssertionFailedError: expected: &lt;448.87081188037814&gt; but was: &lt;400.0&gt;
	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: &lt;448.87081188037814&gt; but was: &lt;400.0&gt;
[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] -&gt; [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.

huangapple
  • 本文由 发表于 2020年9月14日 04:57:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63875520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定