【问题标题】:lateinit property xxx has not been initialized with TestFX in Kotlinlateinit 属性 xxx 尚未在 Kotlin 中使用 TestFX 进行初始化
【发布时间】:2018-04-19 11:05:34
【问题描述】:

我正在使用 TestFX 测试 JavaFX。这是我的代码:

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()
    }
}

但是,它给了我这个:

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)

那么,我该怎么做呢?

【问题讨论】:

  • 我不确定这是否与您的问题相关,因为您没有指出您正在使用的 java 版本:在 java 9 中尝试加载资源(使用 Class::getResource)不包含该资源的模块将失败。如果资源位于当前模块之外,您将需要使用ClassLoader 来加载资源。看到这个问题:stackoverflow.com/q/45166757/2089675TestFX 也可能是个问题

标签: javafx junit kotlin testfx


【解决方案1】:

您的setUp 方法直接调用initialize,这是错误的:它应该只由FXMLLoader.load 方法because then it runs after it gets the chance to initialize the annotated fields 调用。

【讨论】:

  • 好的,但是如果我从setUp() 中删除initialize(),它会报告:lateinit property snackBar has not been initialized。那我该怎么办?
  • 我对 TestFX 了解不多,但您的测试看起来一点也不像 github.com/TestFX/TestFX 中的示例。经过一番思考,我不确定您的方法(在测试类本身中使用@FXML)是否可以工作。
猜你喜欢
  • 2020-04-14
  • 1970-01-01
  • 2022-11-22
  • 2021-10-13
  • 2022-07-05
  • 1970-01-01
  • 2017-03-03
  • 1970-01-01
  • 2023-03-25
相关资源
最近更新 更多