Class ExpectedSystemExit
java.lang.Object
org.junit.contrib.java.lang.system.ExpectedSystemExit
- All Implemented Interfaces:
org.junit.rules.TestRule
The
ExpectedSystemExit
allows in-test specification of expected
System.exit(...)
calls.
If your code calls System.exit(),
then your test stops and doesn't
finish. The ExpectedSystemExit
rule allows in-test specification of
expected System.exit()
calls. Furthermore you cannot use JUnit's
assert methods because of the abnormal termination of your code. As a
substitute you can provide an Assertion
object to the
ExpectedSystemExit
rule.
Some care must be taken if your system under test creates a new thread and
this thread calls System.exit()
. In this case you have to ensure that
the test does not finish before System.exit()
is called.
public class AppWithExit { public static String message; public static int doSomethingAndExit() { message = "exit ..."; System.exit(1); } public static int doNothing() { } }
public void AppWithExitTest { @Rule public final ExpectedSystemExit exit = ExpectedSystemExit.none(); @Test public void exits() { exit.expectSystemExit(); AppWithExit.doSomethingAndExit(); } @Test public void exitsWithStatusCode1() { exit.expectSystemExitWithStatus(1); AppWithExit.doSomethingAndExit(); } @Test public void writesMessage() { exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() { assertEquals("exit ...", AppWithExit.message); } }); AppWithExit.doSomethingAndExit(); } @Test public void systemExitWithStatusCode1() { exit.expectSystemExitWithStatus(1); AppWithExit.doSomethingAndExit(); } @Test public void noSystemExit() { AppWithExit.doNothing(); //passes } }
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Collection<Assertion>
private Integer
private boolean
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.junit.runners.model.Statement
apply
(org.junit.runners.model.Statement base, org.junit.runner.Description description) void
checkAssertionAfterwards
(Assertion assertion) private void
private void
private ProvideSecurityManager
private org.junit.runners.model.Statement
createStatement
(org.junit.runners.model.Statement base) void
void
expectSystemExitWithStatus
(int status) private void
private void
handleSystemExitWithStatus
(int status) static ExpectedSystemExit
none()
-
Field Details
-
assertions
-
expectExit
private boolean expectExit -
expectedStatus
-
-
Constructor Details
-
ExpectedSystemExit
private ExpectedSystemExit()
-
-
Method Details
-
none
-
expectSystemExitWithStatus
public void expectSystemExitWithStatus(int status) -
expectSystemExit
public void expectSystemExit() -
checkAssertionAfterwards
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description) - Specified by:
apply
in interfaceorg.junit.rules.TestRule
-
createNoExitSecurityManagerRule
-
createStatement
private org.junit.runners.model.Statement createStatement(org.junit.runners.model.Statement base) -
checkSystemExit
private void checkSystemExit() -
handleMissingSystemExit
private void handleMissingSystemExit() -
handleSystemExitWithStatus
private void handleSystemExitWithStatus(int status) -
checkAssertions
- Throws:
Exception
-