如何在每次测试 Junit5 TestFX 后启动应用程序
How to launch application after each test Junit5 TestFX
我正在使用 Junit5 和 TestFX 编写应用程序测试。
我的意图是主测试 class 在每次测试后重新启动应用程序。据我所知,我应该使用注解@BeforeEach,但它对我不起作用。
这是我的测试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!");
}
}
问题是:如何在每次测试后重新启动应用程序?
不看 StartMain 就很难回答 class。看起来您在那里使用的是单例模式。如果是这种情况,我会在 StartMain 中创建一个新方法,将单例实例设置为 null,因此当再次调用 getInstance 时,必须重新创建它:
@After //This should be executed after each test
public void destroyApp()
{
StartMain.getInstance().destroy();
}
我正在使用 Junit5 和 TestFX 编写应用程序测试。 我的意图是主测试 class 在每次测试后重新启动应用程序。据我所知,我应该使用注解@BeforeEach,但它对我不起作用。
这是我的测试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!");
}
}
问题是:如何在每次测试后重新启动应用程序?
不看 StartMain 就很难回答 class。看起来您在那里使用的是单例模式。如果是这种情况,我会在 StartMain 中创建一个新方法,将单例实例设置为 null,因此当再次调用 getInstance 时,必须重新创建它:
@After //This should be executed after each test
public void destroyApp()
{
StartMain.getInstance().destroy();
}