2

I am testing JavaFX with TestFX. Here are my codes:

MainTest:

package com.sample;

+import xx;

public class MainTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("mainwindow.fxml"));
        primaryStage.setTitle("Test");
        primaryStage.setScene(new Scene(root, 909, 621));
        primaryStage.show();
        primaryStage.toFront();
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }
}

ControllerTest:

package com.sample

+import xx


class ControllerKTest {
    @FXML
    private lateinit var root: AnchorPane

    private lateinit var snackBar: JFXSnackbar

    @FXML
    fun initialize() {
        snackBarInitialize()
    }

    private fun snackBarInitialize() {
        snackBar = JFXSnackbar(root)
        snackBar.styleClass.add("jfx-snackbar-action")
        snackBar.stylesheets.add("css/jfoenix-components.css")
    }

    @Before
    fun setUp() {
        FxToolkit.registerPrimaryStage()
        FxToolkit.setupApplication(MainTest::class.java)
        initialize()
    }

    private var count = 0


    @Test
    fun onStartButtonMouseClicked() {
        if (count++ % 2 == 0) {
            snackBar.fireEvent(JFXSnackbar.SnackbarEvent("Toast Message $count"))
        } else {
            if (count % 4 == 0) {
                snackBar.fireEvent(JFXSnackbar.SnackbarEvent(
                    "Snackbar Message Persistant $count",
                    "CLOSE",
                    3000,
                    true
                ) { b -> snackBar.close() })
            } else {
                snackBar.fireEvent(JFXSnackbar.SnackbarEvent(
                    "Snackbar Message $count",
                    "UNDO",
                    3000,
                    false
                ) { b -> })
            }
        }
    }

    @After
    fun tearDown() {
        FxToolkit.cleanupStages()
    }
}

However, it gives me this:

kotlin.UninitializedPropertyAccessException: lateinit property root has not been initialized

at com.sample.ControllerKTest.snackBarInitialize(ControllerKTest.kt:55)
at com.sample.ControllerKTest.initialize(ControllerKTest.kt:51)
at com.sample.ControllerKTest.setUp(ControllerKTest.kt:64)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Then, how could I make this right?

  • I'm not sure if this is relevant to your question because you don't indicate what version of java you're using: In java 9 attempting to load a resource (using `Class::getResource`) from a module which does not contain that resource, will fail. You will need to use `ClassLoader` to load the resource if it is outside the current module. See this question: https://stackoverflow.com/q/45166757/2089675. It could also be an issue with `TestFX` – smac89 Oct 05 '19 at 17:53

1 Answers1

0

Your setUp method calls initialize directly, which is wrong: it should only be called by the FXMLLoader.load method because then it runs after it gets the chance to initialize the annotated fields.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • Okay, but it reports this: `lateinit property snackBar has not been initialized` if I remove `initialize()` from `setUp()`. What should I do then? – Gabriel Farnsworth Apr 19 '18 at 14:31
  • I don't know much about TestFX, but your test doesn't look at all like examples in https://github.com/TestFX/TestFX. And after some thinking I am not sure your approach (with using `@FXML` in the test class itself) can work at all. – Alexey Romanov Apr 19 '18 at 17:36