1

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 do 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.)
Community
  • 1
  • 1
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270

1 Answers1

1

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.

Milan Aleksić
  • 1,415
  • 15
  • 33