开发者

How to test a scenario that calls System.exit() in Java? [duplicate]

This question already has answers here: Closed 11 years ago.

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:

  1. Suspend the exiting behavior for the sake of this test.
  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜