Maven Surefire 与 JUnit 5 隐藏堆栈跟踪

Maven Surefire with JUnit 5 hiding stacktraces

如果我这样做:

mvn -X \
     -Dtest=MojoIntegrationTest \
     -Dsurefire.trimStackTrace=false \
     -Dsurefire.useFile=false  \
     clean test
@BeforeEach
public void setUp() {
    stuff();
}

@Test
public void testMyApp() {
    doApp();
}

stuff()doApp() 以某种方式爆炸,我没有得到完整的堆栈跟踪,无论是在控制台还是在文件中。 JUnit5 或 Maven 隐藏了它们:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.me.config.plugin.MojoIntegrationTest
Created C:\dev\workspace\gem-config-maven-plugin\target\test-classes\test-project\target
Tests run: 3, Failures: 3, Errors: 0, Skipped: 0, Time elapsed: 2.133 sec <<< FAILURE! - in com.me.config.plugin.MojoIntegrationTest
testFindCommonPropertiesFromDependency  Time elapsed: 1.6 sec  <<< FAILURE!
java.lang.NullPointerException
    at com.me.config.plugin.MojoIntegrationTest.setUp(MojoIntegrationTest.java:168)

testRetrievalOfConfigJar  Time elapsed: 0.283 sec  <<< FAILURE!
java.lang.NullPointerException
    at com.me.config.plugin.MojoIntegrationTest.setUp(MojoIntegrationTest.java:168)

testOutputPropertiesFilesForAllEnvironments  Time elapsed: 0.2 sec  <<< FAILURE!
java.lang.NullPointerException
    at com.me.config.plugin.MojoIntegrationTest.setUp(MojoIntegrationTest.java:168)


Results :

Failed tests: 
  MojoIntegrationTest.setUp:168 » NullPointer
  MojoIntegrationTest.setUp:168 » NullPointer
  MojoIntegrationTest.setUp:168 » NullPointer

Tests run: 3, Failures: 3, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.717 s
[INFO] Finished at: 2017-08-21T18:23:05+01:00
[INFO] Final Memory: 31M/389M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project gem-config-maven-plugin: There are test failures.
[ERROR] 
[ERROR] Please refer to C:\dev\workspace\gem-config-maven-plugin\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project gem-config-maven-plugin: There are test failures.

Please refer to C:\dev\workspace\gem-config-maven-plugin\target\surefire-reports for the individual test results.
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
Caused by: org.apache.maven.plugin.MojoFailureException: There are test failures.

除了这样做:

@BeforeEach
public void setUp() {
    try {
        stuff();
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
 }

我真的不喜欢,我怎样才能得到堆栈跟踪?

谢谢。

[更新 1]:来自我的 pom 的 surefire 配置:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                    <include>**/*Tests.java</include>
                </includes>
                <properties>
                    <excludeTags>integration</excludeTags>
                </properties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider
                    </artifactId>
                    <version>1.0.0-M5</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.0.0-M5</version>
                </dependency>
            </dependencies>
        </plugin>

和 JUnit5 依赖项:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.0.0-M5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>4.12.0-M5</version>
        <scope>test</scope>
    </dependency>

Maven surefire docs here

[更新 2]:最初这只是影响我的 @BeforeEach 例程,但现在我正在编写测试,我意识到它实际上无处不在,即 mvn junit5 隐藏了我所有的堆栈跟踪。

您必须使用 -DtrimStackTrace=false 而不是 -Dsurefire.trimStackTrace=false:

mvn clean test -DtrimStackTrace=false

详情见http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html