1

I'm wondering how I'm supposed to be testing contents of certain scenes in JavaFXML when using TestFX. Examples include these links: https://github.com/TestFX/TestFX/blob/master/subprojects/testfx-junit5/src/test/java/org/testfx/framework/junit5/ApplicationRuleTest.java

https://medium.com/@mglover/java-fx-testing-with-testfx-c3858b571320

The first link constructs the scene within the test class, and the latter uses a pre-defined scene stored in its own class. How am I supposed to do something similar to this when using JavaFXML instead of JavaFX where the scenes' structures are defined in an fxml file instead of java code?

ancd3ea4
  • 195
  • 1
  • 1
  • 14

1 Answers1

2

First step is giving your components fx:id-s in your fxml files, and then something like:

public class ChangeProfilesMenuControllerTest extends ApplicationTest {
    Pane mainroot;
    Stage mainstage;

    @Override
    public void start(Stage stage) throws IOException {
      mainroot = (Pane) FXMLLoader.load(Main.class.getResource("ChangeProfilesMenU.fxml"));
      mainstage = stage;
      stage.setScene(new Scene(mainroot));
      stage.show();
      stage.toFront();

   }

    @Before
    public void setUp() throws Exception{

    }

   @After
   public void tearDown () throws Exception {
     FxToolkit.hideStage();
     release(new KeyCode[]{});
     release(new MouseButton[]{});
   }

   @Test
   public void addingAndDeletingProfiles() {
      @SuppressWarnings("unchecked")
      ListView<String> listview = (ListView<String>) mainroot.lookup("#listview");
      clickOn("#textfield");
      write("This is a test");
      clickOn("#createnewprofile");
      ...
  }

If you want to acces your controller class instance:

    @Override
    public void start(Stage stage) throws IOException {
        this.mainstage = stage;
        FXMLLoader loader = new FXMLLoader(getClass().getResource("GameOn2.fxml"));
        this.mainroot = loader.load();
        this.controller = loader.getController();
        stage.setScene(new Scene(mainroot));
        stage.show();
        stage.toFront();
    }
  • This looks like the correct way for me to go. However, I'm receiving an error of: `java.lang.NullPointerException: Location is required.` on a line containing `mainRoot = (AnchorPane) FXMLLoader.load(Main.class.getResource("primary.fxml"));` – ancd3ea4 May 19 '20 at 19:50
  • 1
    @ancd3ea4 check out https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other – Slaw May 19 '20 at 20:33