1

I am new to TestFX. I am wondering is TestFX only compatible with FXML? I can't find any information related on how to test UI without FXML using TestFX, if it is possible to test without FXML is it possible to share any information or give me a sample code to let me get started. Thanks!

blackhazelnut
  • 60
  • 1
  • 9
  • 1
    I haven't used TestFX (due in part to the complete lack of documentation) beyond playing with the Oracle Java Magazine article that was published a couple of months back but my guess is that it uses CSS lookups to find the elements defined in an fxml file. By default, the CSS id of an FXML-defined node is set to its `fx:id` attribute. Try setting the id on each item you want to use in the test code (i.e. `Button myButton = new Button("Click me"); button.setId("myButton");`. – James_D Mar 28 '16 at 01:31
  • i am having problem tat when i initialize my root, and having children in the other class, it always fail to run and return me null pointer exception. due to this i create a method in my children class to return the get instance, however it still remain the same. – blackhazelnut Apr 08 '16 at 12:22
  • have a look at the wiki of testFX - rather scarce but should get you going :) and no fxml needed, not even setting an id (provided there's only a single node of the given type - otherwise setting it in code as @James_D suggested - is working fine. – kleopatra Apr 20 '20 at 16:14

1 Answers1

0

You don't have to use FXML with TestFX. Simply extend

org.testfx.framework.junit5.ApplicationTest

in your test class. Override the start method, and set the scene of the stage argument. Now you can use ...

Node node = lookup( "#" + OBJECT_ID ).query();

Assuming the OBJECT_ID is present in the scene.

Mark
  • 136
  • 9