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?