2

When attempting to run a unit test for a GUI button using TestFX, I get an error message from the @Start class.

Unable to make public void com.candle.fileexplorertest.view.DirectoryButtonControllerTests.start(javafx.stage.Stage) accessible: module FileExplorer does not "exports com.candle.fileexplorertest.view" to module org.testfx.junit5.

The issue here is that I don't see how I can export test code to org.testfx.junit5 . My module-info.java file is located inside src/main/java , and therefore has no way of accessing a unit test package in src/test/java . I tried creating a second module-info.java for the test code so I could export the package to junit5, but that brought its own issues and did not seem to resolve the problem.

I have only experienced this issue with a modular JavaFX project. If I delete the module-info.java file, then the test runs fine.

The Unit Test Class

(Admittedly, this project includes additional classes that are not provided here. However, the problem I'm experiencing seems to only involve the unit test code.)

package com.candle.fileexplorertest.view;

(imports here) 

@ExtendWith(ApplicationExtension.class)
public class DirectoryButtonControllerTests {

    DirectoryButtonController testButton;
    DirectoryButtonViewModel viewModel;
    Image image;

    @Start
    public void start(Stage stage) {
        viewModel = mock(DirectoryButtonViewModel.class);
        image = new Image("/com/candle/fileexplorer/images/16/Folder.png");
        testButton = new DirectoryButtonController(viewModel, image);

        Parent sceneRoot = new StackPane(testButton);
        Scene scene = new Scene(sceneRoot);
        stage.setScene(scene);
        stage.show();
        stage.toFront();
    }

    @Test
    public void goToDirectory_shouldTellViewModel_whenButtonClicked(FxRobot robot) {
        robot.clickOn(testButton);
        verify(viewModel).goToDirectory();
    }
}

Module-Info.java

module FileExplorer {
    requires javafx.controls;
    requires javafx.fxml;
    requires javafx.graphics;

    exports com.candle.fileexplorer;
    
    opens com.candle.fileexplorer to javafx.graphics;
    opens com.candle.fileexplorer.view to javafx.fxml;

}

Is there a way to solve this without switching to a non-modular project?

  • How are you running the test? – Slaw Jun 23 '22 at 02:24
  • You could break the encapsulation and [open the module](https://stackoverflow.com/questions/46482364/what-is-an-open-module-in-java-9-and-how-do-i-use-it) by placing the open keyword in front of the module name and removing the opens statements from the body of the module definition. I don't know if there is a better solution though. – jewelsea Jun 23 '22 at 07:10
  • If you use maven for the build, maven does some tricky things to [allow you to test your code without the test code being in a module](https://stackoverflow.com/a/46613986/1155209). I don't know about gradle though. – jewelsea Jun 23 '22 at 07:20
  • If the default behaviour is not enough perhaps the `--patch-module` option might be used to patch in the testfx jar. That option: "Overrides or augments a module with classes and resources in JAR files or directories.". – jewelsea Jun 23 '22 at 07:20
  • @jewelsea I tried opening the module, which seems to have replaced that error with another. `java.lang.IllegalAccessError: class org.testfx.toolkit.impl.ToolkitServiceImpl (in module org.testfx) cannot access class com.sun.javafx.application.ParametersImpl (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.application to module org.testfx` I checked TestFX's GitHub page and found [this issue](https://github.com/TestFX/TestFX/issues/638) regarding the `IllegalAccessError` , but the proposed solution did not appear to fix the problem. – cachandlerdev Jun 24 '22 at 00:26
  • @cachandlerdev check the command line used when the test is run, make sure it includes `--add-exports=javafx.graphics/com.sun.javafx.application=org.testfx`, though if you are using Maven, you might not need that, due to the info on how maven tests operate in a modular environment which is provided in the previous link. I don't think I can really help much more as TestFX is not something which I use. – jewelsea Jun 24 '22 at 00:38
  • @jewelsea I see. For the time being, I think I'll switch to a non-modular project, as that seems to be the easiest way to get around TestFX's errors. On a related note, do you use a different framework for testing JavaFX UI classes? I started using TestFX because it was mentioned on JavaFX's website, but given the difficulties that seem to arise when using it with modular projects, I'd be happy to know if there were other options. – cachandlerdev Jun 25 '22 at 02:03
  • I make UI code as simple and isolated as possible and manually test it. I use junit and sometimes spring tests for testing logic, services, server processes, database usage, etc. – jewelsea Jun 25 '22 at 03:15

0 Answers0