Question

I starting to use FEST to help me to perform unit test on my Java Swing GUI.

For now, I managed to get through the documentation (mostly deprecated) and help me by looking at the Javadoc and the code.

Right now I am stuck on a problem while using the NoExitSecurityManager. The documentation is quite out dated but we can understand the big lines of it.

I simply try to test if my "Quit" MenuItem is working well in my GUI. So, I need to block the System.exit(0) and map the exit status of the program to a JUnit test.

Here is a simplified code I use to perform the test (the tested class is GraphicalUserInterface).

import org.junit.Test;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import org.fest.swing.annotation.RunsInEDT;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.edt.GuiActionRunner;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.junit.testcase.FestSwingJUnitTestCase;
import org.fest.swing.security.NoExitSecurityManagerInstaller;


public class GraphicalUserInterfaceTest extends FestSwingJUnitTestCase {

    private static FrameFixture gui;
    private static NoExitSecurityManagerInstaller noExitSecurityManagerInstaller;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception { 
             NoExitSecurityManagerInstaller.installNoExitSecurityManager(new ExpectExitSuccess());
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception { 
             noExitSecurityManagerInstaller.uninstall();
    }

    @Override
    protected void onSetUp() {
            gui = new FrameFixture(robot(), createNewGUI());
            gui.show();
    }

    @RunsInEDT
    private GraphicalUserInterface createNewGUI() {
             return GuiActionRunner.execute(new GuiQuery<GraphicalUserInterface>() {
                    protected GraphicalUserInterface executeInEDT() {
                       return new GraphicalUserInterface();
                    }
         });
    }

    @Test
    public final void testFileMenuQuitMenuItem() {
           gui.menuItemWithPath("File", "Quit").click();
    }

}

The ExitCallHook are coded like this (you can guess the other one easily).

import static org.junit.Assert.assertTrue;

import org.fest.swing.security.ExitCallHook;

public final class ExpectExitSuccess implements ExitCallHook {

    @Override
    public void exitCalled(int status) {
           assertTrue(status == 0);
    }
}

All the tests are performed well and everything seems to be ok except that I get a java.lang.NullPointerException at the end.

So, I wonder what did I do wrong (or what can I improve to not get this nullpointer exception at the end of the test).

Was it helpful?

Solution

I found the solution in the code. In fact, the proper way to do it is the following:

 @Test
 public final void testFileMenuQuitMenuItem() {
     NoExitSecurityManagerInstaller noExitSecurityManagerInstaller = 
        NoExitSecurityManagerInstaller.installNoExitSecurityManager(new ExpectExitSuccess());

     gui.menuItemWithPath("File", "Quit").click();

     noExitSecurityManagerInstaller.uninstall();
 }

This way prevent to pollute each test with a NoExitSecurityManager.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top