Monday, 27 April 2020

CheckExitCalled exception

Exception:
org.junit.contrib.java.lang.system.internal.CheckExitCalled: Tried to exit with status 0.
at org.junit.contrib.java.lang.system.internal.NoExitSecurityManager.checkExit(NoExitSecurityManager.java:24)
at java.base/java.lang.Runtime.exit(Runtime.java:111)
at java.base/java.lang.System.exit(System.java:1785)
at example.InputStats.countInput(InputStats.java:23)
at example.InputStatsTest.testCountInput2(InputStatsTest.java:60)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.contrib.java.lang.system.ExpectedSystemExit$1.evaluate(ExpectedSystemExit.java:130)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.contrib.java.lang.system.internal.LogPrintStream$1$1.evaluate(LogPrintStream.java:30)
at org.junit.contrib.java.lang.system.internal.PrintStreamHandler$3.evaluate(PrintStreamHandler.java:48)
at org.junit.contrib.java.lang.system.internal.LogPrintStream$1.evaluate(LogPrintStream.java:26)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.lang.Thread.run(Thread.java:830)
This exception is thrown when multiple Unit tests are using System Rules and are run in parallel [source]. However, there is also another situation, which causes this exception, which is the location of the exit statement:

public class InputStats {
public void countInput() {
try {
System.exit(0);
}catch(Exception e) {
e.printStackTrace();
}
}
}
view raw InputStats hosted with ❤ by GitHub

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
public class InputStatsTest {
InputStats stats;
@Rule
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
@Before
public void setUp() throws Exception {
stats = new InputStats();
}
@Test
public void testCountInput2() {
exit.expectSystemExit();
stats.countInput();
}
}
view raw InputStatsTest hosted with ❤ by GitHub

The solution is to move the exit statement away from the try-catch block.

No comments:

Post a Comment