I need to write a JUnit test for exiting out of a program with WINDOW_CLOSING an
ID: 3664375 • Letter: I
Question
I need to write a JUnit test for exiting out of a program with WINDOW_CLOSING and minimize with WINDOW_ICONIFIED. I have no idea as to how to do this. I have posted my method for these below. The doExit() in the CLosing, just brings up a pop-up to confirm that you are exiting the program. Any ideas as to how to code this would be great. I am lost. I know a lot is missing here my class the code below is found in is called AppFrame.
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
doExit();
}
else if ((e.getID() == WindowEvent.WINDOW_ICONIFIED)) {
super.processWindowEvent(new WindowEvent(this,
WindowEvent.WINDOW_CLOSING));
}
else
super.processWindowEvent(e);
}
Explanation / Answer
public class NoExitTestCase extends TestCase { protected static class ExitException extends SecurityException { public final int status; public ExitException(int status) { super("There is no escape!"); this.status = status; } } private static class NoExitSecurityManager extends SecurityManager { @Override public void checkPermission(Permission perm) { // allow anything. } @Override public void checkPermission(Permission perm, Object context) { // allow anything. } @Override public void checkExit(int status) { super.checkExit(status); throw new ExitException(status); } } @Override protected void setUp() throws Exception { super.setUp(); System.setSecurityManager(new NoExitSecurityManager()); } @Override protected void tearDown() throws Exception { System.setSecurityManager(null); // or save and restore original super.tearDown(); } public void testNoExit() throws Exception { System.out.println("Printing works"); } public void testExit() throws Exception { try { System.exit(42); } catch (ExitException e) { assertEquals("Exit status", 42, e.status); } } }