JUnit 5在调试模式下截断堆栈跟踪并使JVM崩溃

时间:2017-11-14 10:11:54

标签: java junit junit5

我现在正在尝试使用JUnit 5,我遇到了两个问题:

1)当处于调试模式时,我在闲置一段时间后遇到JVM崩溃的问题,即当我一步一步地走,没有停止时间超过3-5秒然后一切都没问题,但如果我停止进行一些评估,或者仅仅因为我需要在调试期间执行其他操作,因此花费超过10-15秒然后当我尝试进行下一步时JVM崩溃。

2)JUnit 5截断堆栈跟踪只留下一个无意义的字符串(使用测试文件行,而不是抛出异常的实际行)。以下是此类输出的示例:

Running ru.ahml.ficb.generators.GeneratorTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.418 sec <<< FAILURE! - in ru.ahml.ficb.generators.GeneratorTest
test  Time elapsed: 3.359 sec  <<< ERROR!
java.lang.NullPointerException
    at ru.ahml.ficb.generators.GeneratorTest.test(GeneratorTest.java:44)


Results :

Tests in error:
  GeneratorTest.test:44 » NullPointer

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

绝对不知道这个NPE发生在哪里! 以下是JUnit 4所展示的内容:

Running ru.ahml.ficb.generators.GeneratorTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.541 sec <<< FAILURE!
test(ru.ahml.ficb.generators.GeneratorTest)  Time elapsed: 3.4 sec  <<< ERROR!
java.lang.NullPointerException
    at ru.ahml.ficb.excel.generators.ordersgpb.Generator.fillTable(Generator.java:268)
    at ru.ahml.ficb.excel.generators.ordersgpb.Generator.fillSheet(Generator.java:122)
    at ru.ahml.ficb.excel.generators.ordersgpb.Generator.generate(Generator.java:64)
    at ru.ahml.ficb.generators.GeneratorTest.test(GeneratorTest.java:44)
    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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    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.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Tests in error: 
  test(ru.ahml.ficb.generators.GeneratorTest)

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

一切都很清楚!

如何使JUnit 5显示与JUnit 4相同的堆栈跟踪以及如何在使用JUnit 5时防止JVM在调试模式下崩溃?

1 个答案:

答案 0 :(得分:3)

“JUnit 5”不会截断堆栈跟踪。

此处,由于默认设置,Surefire会修剪堆栈跟踪:http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#trimStackTrace

将此设置为false,如下所示:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <trimStackTrace>false</trimStackTrace>
  </configuration>
</plugin>

应该防止修剪。