Please, consider this View:
public class View {
private final Label label = new Label("Foo");
private final Button button = new Button("Bar");
private final HBox hBox = new HBox(label, button);
...
}
Can I use this view in my integration tests without running FX platform (I have only JavaFX elements, that are not added to Scene
and Stage
)?
I tried it on Ubuntu and everything seems to work. But I am afraid, that for example, on Windows it will throw some errors.
Please, consider this View:
public class View {
private final Label label = new Label("Foo");
private final Button button = new Button("Bar");
private final HBox hBox = new HBox(label, button);
...
}
Can I use this view in my integration tests without running FX platform (I have only JavaFX elements, that are not added to Scene
and Stage
)?
I tried it on Ubuntu and everything seems to work. But I am afraid, that for example, on Windows it will throw some errors.
Share Improve this question asked Feb 6 at 13:48 SilverCubeSilverCube 5222 silver badges11 bronze badges1 Answer
Reset to default 5Many JavaFX components can be created (and tested) without starting the JavaFX Platform
You can create most JavaFX components off of the JavaFX thread without a scene or stage, which might be sufficient for some tests.
You can only get a JavaFX thread if the JavaFX platform is started.
Certain components such as WebView, won't work unless the JavaFX Platform is running. They are documented like this:
WebView objects must be created and accessed solely from the FX thread.
Another example of a JavaFX component requiring a JavaFX thread is an Animation
(including Timelines and Transitions):
The animation runs on the JavaFX Application Thread.
Test framework suggestions
You might want to consider a framework like TestFX which is specifically built to test JavaFX. I have not used it, so not a direct recommendation, but something for you to consider. Unfortunately, for the TextFX framework, the documentation currently states the framework "has only legacy support", so it may not be guaranteed to continue to operate on future JavaFX versions.
The other option is to look at the test code for OpenJFX and follow similar principles when testing your code. A quick scan shows it is split, with some functional tests requiring the full JavaFX runtime. Other tests like this ButtonTest will use a stub JavaFX Toolkit. Some of the code in the OpenJFX tests is com.sun code, so it is not public API, but might still be OK to use in tests if you try it.
FAQ
I tried it on Ubuntu and everything seems to work. But I am afraid, that for example, on Windows it will throw some errors.
If the test works fine on one platform, it will likely also pass and work the same way on another platform. In general, JavaFX is pretty good at cross-platform support. If in doubt, obtain the other platform, and build and test your software on it.
Why might I want to test some JavaFX code with the JavaFX Platform running if the components can be created without starting the JavaFX Platform?
For some tests, even for components that can be created off of the JavaFX thread, starting the JavaFX platform before testing might result in a more robust integration test.
For example, some JavaFX code is not executed unless a skin is attached to a control. Attaching a skin may happen lazily the first time a layout pass is generated on the code and CSS processed, which will occur when a control is displayed in a Scene on a Stage, or a manual layout pass is performed.