0

I'm tasked with setting up some JUnit tests for a utility app that is required to System.exit(1) under various circumstances, and System Rules is great for testing that. The app can also run in GUI mode and it pops a small JFrame. When it's running in the context of a JUnit test with the ExpectedSystemExit rule applied, as the JFrame is constructed, something surreptitiously exits the jvm. Turn off the ExpectedSystemExit rule, and the test runs as it should, popping the JFrame just fine, it's just I can no longer test for jvm exit()s.

A guess would be that the SecurityManager System Rules 1.9.0 puts in place is disallowing some permission(s). I'm still in the process of collecting more debug info. Until then, is there a known solution this issue?

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
dave thompson
  • 133
  • 3
  • 9
  • 1
    Are you invoking `setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)`? It calls `SecurityManager.checkExit` which System Rules interprets as `exit()`. – Piotr Praszmo Apr 23 '15 at 17:25
  • @Banthar Indeed, further debugging shows `setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)` is triggering the exit(). So, now what? – dave thompson Apr 23 '15 at 19:08

1 Answers1

0

setDefaultCloseOperation calls SecurityManager.checkExit to verify exit can be called. System Rules is based on SecurityManager and interprets that as call to exit. This will both cause your test to fail and make it impossible to construct your awt windows.

You have to create your windows and call setDefaultCloseOperation before ExpectedSystemExit registers SecurityManager. For example in @BeforeClass method:

@Rule
public ExpectedSystemExit exit = ExpectedSystemExit.none();

static JFrame frame;

@BeforeClass
public static void before() {
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

@Test
public void test() {
    exit.expectSystemExitWithStatus(0);
    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
Piotr Praszmo
  • 17,928
  • 1
  • 57
  • 65