How to test a scenario that calls System.exit() in Java? [duplicate]
Possible Duplicate:
Java: How to test methods that call System.exit()?
In a certain scenario, I want to test that the application makes a function call to send an email, and then calls System.exit(0)
This is a Java application, and Mockito is being used for mocking.
My test currently looks something like this:
testSendsEmailInScenario() {
// set up
foo.bar(mock);
ArgumentCaptor<HashMap> argument = ArgumentCaptor.forClass(HashMap.class);
verify(mockEmailDeliveryManager).send(argument.capture());
Map<String,String> argMap = argument.getValue();
// Test that map contains the right stuff
}
So this doesn't work, because after the call to send
in the application, System.exit()
is called, which terminates the test without the test either succeeding or failing.
Since exit()
is a static method of System
, I can't mock it with Mockito. So how d开发者_运维百科o I:
- Suspend the exiting behavior for the sake of this test.
- Write a second test that will confirm that the exit happened. (A test that succeeds only if
System.exit()
is called, and fails otherwise.)
I use following very simple scenario: I use Groovy - MockFor which can guarantee for you that some method is called. I tried, it is working with even System classes.
精彩评论