javatestingjunitjunit5testfx

How to launch application after each test Junit5 TestFX


I'm writing an application test with Junit5 and TestFX. My intention is that the main test class relaunches the application after each test. As far as I know, I shall use the annotation @BeforeEach, and it didn't work for me.

Here is my test class:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MainTest extends ApplicationTest implements FxRobotInterface {
Logger loggerGuiTesting = LoggerManager.getInstance().getLogger(LoggerType.GUI_TESTING);

@BeforeEach
@Override
public void start(final Stage stage) {
    StartMain.getInstance();
    this.loggerGuiTesting.log(Level.INFO, "Application starts!");
}

@AfterAll
public void endApplication() {
    new ExitGuiTest().run(); // That's my internal test framework
}

@Test
public void atestIfOpeningScreenIsThere() {
    verifyThat("#imageViewSplashScreenLogo", NodeMatchers.isNotNull());
    verifyThat("#progressBarSplashScreen", NodeMatchers.isNotNull());
    verifyThat("#labelSplashScreenVersion", NodeMatchers.isNotNull());
    verifyThat("#labelSplashScreenDate", NodeMatchers.isNotNull());
    this.loggerGuiTesting.log(Level.INFO, "testIfOpeningScreenIsThere, succeeded!");
}

@Test
public void btestIfRadioButtonOneExist() {
    assertThat("#sourcesOneRadioButton", is("#sourcesOneRadioButton"));
this.loggerGuiTesting.log(Level.INFO, "testIfRadioButtonOneExist, succeeded!");
    }

@Test
    public cnextTest() {
        new StartAnotherGuiTest().run();
        this.loggerGuiTesting.log(Level.INFO, "anotherTest, succeeded!");
    }

}

The question is: how can I relaunch the application after each test?


Solution

  • It is difficult to answer without taking a look at the StartMain class. It looks like you are using a singleton pattern there. If thats the case I would create a new method in StartMain that sets the singleton instance to null so when getInstance is called again, it has to be re-created:

    @After //This should be executed after each test
    public void destroyApp()
    {
        StartMain.getInstance().destroy();
    }