使用 AssertJ 在没有测试失败的情况下在测试后关闭应用程序

Close application after test without test failure using AssertJ

我使用 AssertJ 来测试我的 swing 应用程序。我必须在测试后和下一次测试之前关闭我的应用程序,这将再次启动我的应用程序。但是,当我调用 frame.close();System.exit(0); 时,测试崩溃,退出代码为 0。我尝试在测试前使用此代码:

ApplicationLauncher.application(App.class).start();
    pause(8000);
    frame = WindowFinder.findFrame(FrameMatcher.withTitle("Application")).using(robot());
    try {
        frame.close();
    } catch (Exception e){
        e.printStackTrace();
    }
    ApplicationLauncher.application(App.class).start();
    pause(8000);
    frame = WindowFinder.findFrame(FrameMatcher.withTitle("Application")).using(robot());

但是,在 frame.close(); 之后,我遇到了同样的问题。可能有人知道该怎么做吗?有什么建议吗?

作为答案,我认为使用丛林脚本更好。我写了 bush 脚本,它从应用程序开始一个测试,并在逐行测试后关闭它。我认为这是最简单的 运行 在应用程序的新实例中测试用例并在测试后关闭应用程序。

#!/bin/bash
echo "try to start test"    
java -cp /home/gui-application.jar 
app.gui.tests.runners.firsttestcase.RunFirstCase

org.assertj.swing.security.NoExitSecurityManagerInstaller 可能是您问题的答案。

在 JUnit @BeforeClass 方法调用中:

// to prevent system.exit from causing issues
org.assertj.swing.security.NoExitSecurityManagerInstaller.installNoExitSecurityManager();

//Then start the appplication
org.assertj.swing.launcher.ApplicationLauncher.application("your.main.application.class").start();

在 JUnit @AfterClass 方法调用中:

//After exiting your application uninstall the no exit security manager
org.assertj.swing.security.NoExitSecurityManagerInstaller.installNoExitSecurityManager().uninstall();

还有一个异常提示我无法避免:

Application tried to terminate current JVM with status 0 (org.assertj.swing.security.ExitException)

但它不会导致您的测试失败并且可以忽略,我认为这是意料之中的,因为应用程序在 NoExitSecurityManagerInstaller 为 'enabled' 时退出。

希望对您有所帮助!