1

Running some unit tests on class that contains a JavaFX alert, I implemented the following try-catch block:

    try{
    if(warning){
        //CONSIDER:  Make a generic alert call for any situation, pass args 
        Alert alert = new Alert(AlertType.WARNING);
        alert.setTitle("User Warning");
        alert.setHeaderText(null);
        alert.setContentText("The following elements were not found for the code you are outputting and are printed, as is"
                + "\n\n" + errorList
                + "\n\n(This should be updated to Z000 format) ");
        alert.showAndWait();
    }}
    //throws error when invoked from a non javaFX context
    catch(IllegalStateException e){        
        System.out.println("The following elements were not found for the code you are outputting and are printed, as is"
                + "/n/n" + errorList
                + "/n/n(This should be updated to Z000 format)");
    }

After making a top-level try-catch on the calling code that didn't fix it either.

Stack:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at javafx.scene.control.DialogPane.createContentLabel(DialogPane.java:166)
    at javafx.scene.control.DialogPane.<init>(DialogPane.java:217)
    at javafx.scene.control.Dialog.<init>(Dialog.java:478)
    at javafx.scene.control.Alert.<init>(Alert.java:245)
    at javafx.scene.control.Alert.<init>(Alert.java:223)
    at gov.ornl.nstd.datatools.OutputFormatter.verifyAndNorm(OutputFormatter.java:385)
    at gov.ornl.nstd.datatools.OutputFormatter.convert(OutputFormatter.java:218)
    at gov.ornl.nstd.datatools.Testing.TestAllOutputs.main(TestAllOutputs.java:49)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
    at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:550)
    at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
    at javafx.scene.control.Control.<clinit>(Control.java:87)
    ... 8 more

What are some ways to work around this?

What I'm getting from the error is that there is no JavaFX scene/stage to associate the Alert with, but I'm not sure how to deal with this.

Captain Prinny
  • 459
  • 8
  • 23
  • That'd be sound advice if the error wasn't a GUI element that populates just fine when called from the GUI, but isn't relevant to the unit tests. The error, as it stands, "toolkit not initialized" seems to be a JavaFX problem, which isn't being used in the tests, as is otherwise verified to work. – Captain Prinny Jun 26 '15 at 14:50

2 Answers2

1

JavaFX performs "hidden" initialization on start. Running Alert as unit test doesn't trigger initialization. The easiest ways to trigger it is have Application.launch() executed. Also there are other ways you can have a look at few links

Community
  • 1
  • 1
Sarfaraz Khan
  • 2,166
  • 2
  • 14
  • 29
  • I'm not trying to run Alert, it just exists in the code, because it's an alert to the users that there's a condition they should be aware of moving forward from that point. The unit tests are testing the rest of the code, and I would actually rather it not invoke without having to use a flag for it. However, to your point: in what context is Application.launch() executed from? the Alert or the unit test? – Captain Prinny Jun 26 '15 at 19:36
  • Don't know how you've design your unit test cases.It will be helpfull if you could explain more about it and provide some more code fragments.Regarding Application class checkout this doc https://docs.oracle.com/javafx/2/api/javafx/application/Application.html – Sarfaraz Khan Jun 27 '15 at 05:46
  • OutputFormatter has an Alert when certain conditions are detected in the output. The class does a lot of checking and normalization of data based on unit preferences. I'm running an exaustive test on all outputs, invoking the OutputFormatters primary method using all possible arguments the user could give it through a second class which instantiates an output formatter and just feeds it data with a bunch of nested loops. It may be better to have the output formatter throw an exception instead of catch it and have the GUI handle the alert, but it's currently internal to the formatting class. – Captain Prinny Jun 28 '15 at 04:20
  • Turns out you can just extend Application. It wont pop up the alert, but it wont crash. – Captain Prinny Jun 30 '15 at 17:59
1

Solving the problem is as simple as extending Application in the class that you're running your tests from.

This does not generate Alert boxes, but it avoids the crash.

EDIT: This leaves the alerts un-handled, will require a forced quit of the process.

Captain Prinny
  • 459
  • 8
  • 23