Surefire fails because of checking if System.exit() was called in one of junit tests
-
21-12-2019 - |
Question
To depict the problem I encountered, let's assume there is a dummy class:
import static java.lang.System.exit
class Example {
void methodGeneratingSystemExit1() {
exit 1
}
void methodGeneratingSystemExit2() {
exit 2
}
}
And a test against it:
import org.junit.Test
import org.junit.Rule
import org.junit.contrib.java.lang.system.ExpectedSystemExit
class ExampleTest {
@Rule
public final ExpectedSystemExit expectedSystemExit = ExpectedSystemExit.none()
@Test
void "System exits with code 1 when method1 is generated"() {
expectedSystemExit.expectSystemExitWithStatus(1)
methodGeneratingSystemExit1()
}
@Test
void "System exits with code 1 when method1 is generated"() {
expectedSystemExit.expectSystemExitWithStatus(2)
methodGeneratingSystemExit2()
}
}
As I said, it's just dummy example but still test does what is supposed o do - when System.exit() is called, test notices that and is all green. The problem is, I have a surefire plugin that gives me a message like:
ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.17:test (default-test) on project xtrakter: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.17:test failed: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
How can I suppress that error in surefire? Or any other workaround/solution to that problem?
Solution
Surefire does not support tests or any referenced libraries calling System.exit() at any time.
source : http://maven.apache.org/surefire/maven-surefire-plugin/faq.html#vm-termination
I think it's impossible to solve this problem, it's surefire core behaviour.
OTHER TIPS
You can test for System.exit by installing your own SecurityManager, implementing the method checkExit for the duration of the test. See Java: How to test methods that call System.exit()? for more details.