In this example, we will learn how to test methods that call System.exit() Java method? The library System Rules has a JUnit rule called ExpectedSystemExit. With this rule you are able to test code, that calls System.exit():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void MyTest { @Rule public final ExpectedSystemExit exit = ExpectedSystemExit.none(); @Test public void systemExitWithArbitraryStatusCode() { exit.expectSystemExit(); //the code under test, which calls System.exit(...); } @Test public void systemExitWithSelectedStatusCode0() { exit.expectSystemExitWithStatus(0); //the code under test, which calls System.exit(0); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.