ianbrandt
05/12/2019, 4:50 AMView
with TestFX. I'm not clear on the current way to setup the view properly in a JUnit 5 test. Any help would be appreciated.
@ExtendWith(ApplicationExtension::class)
class MyViewTest {
@Start
fun start(stage: Stage) {
stage.scene = MyView().root.scene
stage.show()
}
@Test
fun `test my view`(robot: FxRobot) {
println(robot.lookup(".canvas").queryAs(Canvas::class.java).width)
}
}
class MyView : View() {
override val root = group {
canvas(800.0, 600.0)
}
}
java.lang.NullPointerException
at org.testfx.util.NodeQueryUtils.fromWindow(NodeQueryUtils.java:168)
[...]
at org.testfx.util.NodeQueryUtils.rootsOfWindows(NodeQueryUtils.java:56)
at org.testfx.service.finder.impl.NodeFinderImpl.rootsOfWindows(NodeFinderImpl.java:94)
at org.testfx.service.finder.impl.NodeFinderImpl.fromAll(NodeFinderImpl.java:59)
at org.testfx.service.finder.impl.NodeFinderImpl.lookup(NodeFinderImpl.java:44)
at org.testfx.api.FxRobot.lookup(FxRobot.java:186)
ianbrandt
05/13/2019, 12:15 AMstage.scene = Scene(MyView().root)
. That seems to do it.
I also had to give the canvas an ID, as CSS class lookup doesn't appear to work for Canvas nodes in JavaFX.
Working example:
@ExtendWith(ApplicationExtension::class)
class MyViewTest {
@Start
fun start(stage: Stage) {
stage.scene = Scene(MyView().root)
stage.show()
}
@Test
fun `test my view`(robot: FxRobot) {
val canvas = robot.lookup("#my-canvas").queryAs(Canvas::class.java)
assertThat(canvas?.width).isEqualTo(800.0)
}
}
class MyView : View() {
override val root = group {
canvas(800.0, 600.0) {
id = "my-canvas"
}
}
}