I have been developing a command-line tool which calls System.exit()
(don't want to use exceptions instead of) on certain inputs.
I am familiar with Java: How to test methods that call System.exit()? and its the most elegant approach.
Unfortunately, it is not enough pure, due to I had to add the dependencies to system-rules, junit-interface
Is there any common pattern for dealing with System.exit
in specs2 which is more pure than my current approach which don't use specs2?
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
public class ConverterTest {
@Rule
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
@Test
public void emptyArgs() {
exit.expectSystemExit();
Converter.main(new String[]{});
}
@Test
public void missingOutArgument() {
exit.expectSystemExitWithStatus(1);
Converter.main(new String[]{"--in", "src/test/resources/078.xml.gz"});
}
}
First option: use some exception instead of
System.exit
.Second option: call application in separate thread and check return codes.
Third option: mock
System.exit
. There are many possibilities to do that, mentioned one is quite good.However, there is no
specs2
-specific pattern to work withSystem.exit
. Personally I'd suggest first or second options.If you really wish to go with a method using
System.exit()
, the simplest way to test it was actually called is to replace yourSecurityManager
with one that'll throw anExitException
(subclassingSecurityException
) whenSystem.exit()
is called:class SystemExitSpec
test ConverterSpec