Have been using the TestFx framework to test my JavaFx app. It runs fine on a test-method by test-method basis. But as soon as I try and run more than one test-method, or test-class at a time, I get this error:

Caused by: java.lang.IllegalStateException: Cannot set style once stage has been set visible

My test class looks like this:

public class TestExample extends ApplicationTest {

    @Override
    public void init() throws TimeoutException {
        System.setProperty("workingDir", "src/test/resources/test1");
    }

    @Override
    public void start(Stage stage) {
        final Main main = new Main();
        main.start(stage);
    }

    @Test
    public void testHungProcess_clientForciblyStopsProcess(){
        //....do some testing
    }
}

And my Main class, does set some styles on the stage. i.e.:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        //....other things
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        //....other things
    }
}

I am using TestFx v4.0.8-alpha, with the JavaFx that comes with Java 8.

Does anyone know of a way to run multiple consecutive tests without incurring this error?

Any help would be appreciated.

1

There are 1 best solutions below

0
On

The fact the TestFx uses one instance of Stage for all tests.

As possible solution is to check the style and init it only if needed:

    if (stage.getStyle() != StageStyle.UNDECORATED) {
        stage.initStyle(StageStyle.UNDECORATED);
    }

Related other question.