我正在用 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)
那么,我怎样才能做到这一点呢?