Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit 5 truncates stacktraces and crash JVM in debug mode

Tags:

java

junit

junit5

I'm trying JUnit 5 now and I have two problems with it:

1) When in debug mode I have a problem with JVM crashes after beign idle for some time, i.e. when I go step by step without making stops longer then 3-5 seconds then everything's OK, but if I make a stop to make some evaluations or just because I needed to do smth else during debug, thus spending more then 10 - 15 seconds then JVM crashes when I try to make next step.

2) JUnit 5 truncates stacktraces leaving only one meaningless string (with the line of test file and not an actual line from where an exception was thrown). Here's an example of such output:

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

Absolutely no idea where this NPE occurred! And here's what JUnit 4 shows:

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

Everything's clear!

How to make JUnit 5 show same stacktraces as JUnit 4 and how to prevent JVM crashes in debug mode when using JUnit 5?

like image 273
mykola Avatar asked Nov 14 '17 10:11

mykola


1 Answers

"JUnit 5" does not truncate stack traces.

Here, Surefire does trim the stack trace because of the default setting: http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#trimStackTrace

Setting it to false like this:

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

should prevent the trimming.

like image 105
Sormuras Avatar answered Oct 06 '22 00:10

Sormuras