Vadym
Vadym

Reputation: 163

Close application after test without test failure using AssertJ

I use AssertJ to test my swing application. I must to close my application after test and before the next test, which will start my application again. But, when I call frame.close(); or System.exit(0); the test crashes with exit code 0. I tried to use this code before test:

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());

But, after frame.close(); I have the same problem. May be anyone know how to do it? Any advice?

Upvotes: 1

Views: 494

Answers (2)

Shaun
Shaun

Reputation: 328

org.assertj.swing.security.NoExitSecurityManagerInstaller might be the answer to your issue.

In the JUnit @BeforeClass method call:

// 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();

In the JUnit @AfterClass method call:

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

There is still an exception shown that I am unable to avoid:

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

but it does not cause your tests to fail and can be ignored, and I think it is expected since the application exited while NoExitSecurityManagerInstaller was 'enabled'.

Hope this helps!

Upvotes: 1

Vadym
Vadym

Reputation: 163

As an answer I think is better to use bush scripting. I wrote bush script, that start one test from application and close it after testing line by line. I think it is the most simple to run testcase in new instance of application and close app after testing.

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

Upvotes: 0

Related Questions