I'm trying to work out from the examples and docs ...
# tornadofx
i
I'm trying to work out from the examples and docs I can find here and online how to test a TornadoFX
View
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.
Copy code
@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)
	}
}
Copy code
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)
I poked around the TestFX source and TornadoFX tests (thanks for that pointer, @edvin), and came up with this change to the scene initialization:
stage.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:
Copy code
@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"
		}
	}
}