Update: Simplified test case... Continuing my Tor...
# tornadofx
i
Update: Simplified test case... Continuing my TornadoFX/TestFX exploration above, I'm now trying to get TestFX to fire a button action. When I run my test I see the view appear, and I see the TestFX robot moving the cursor to the view's button. I presume that it clicks on it as well, but the expected action doesn't fire. If I launch the app and click the button manually, I do get the expected result. My suspicion is that I'm still not properly initializing TornadoFX or TestFX in the
@Start
method of my test, but I'm not sure what else needs to be done. Any help would be most appreciated. https://github.com/ianbrandt/tornadofx-test/blob/master/src/test/kotlin/com/ianbrandt/test/MyViewTest.kt
a
You are close! What’s the error that you’re getting?
@ianbrandt If I had to guess, you need to wrap your interactions in your setup like
Copy code
@Start
fun start(stage: Stage) {
       interact {
           stage.scene = Scene(MyView().root)
	    stage.show()
       }
}
i
Thanks, @amanda.hinchman-dominguez. No error, other than the plain old assertion failure. I'm using the JUnit 5 extension, as opposed to extending
ApplicationTest
, so there is no
interact()
in scope. I tried this, but no change...
Copy code
@ExtendWith(ApplicationExtension::class)
class MyViewTest {

	private lateinit var robot: FxRobotInterface

	@Start
	fun start(stage: Stage) {
		robot.interact {
			stage.scene = Scene(MyView().root)
			stage.show()
		}
	}
...
I do think you're onto something regarding interaction, though. I'll keep experimenting.
a
Can you post the specific error?
a
Ah hahaha
Copy code
java.lang.AssertionError: Expected: Labeled has text "Success"
     but: was "Test"
Expected :Labeled has text "Success"
Actual   :"Test"
This means you caught a bug, it appears
Put debugging points at your first assert and the last
also a debugging point where the text is supposed to change
Sometimes your tests actually catch bugs 😆 we spend so much time fixing them to work we forget
i
Looks like you called it. I pulled the Java example form the TestFX site verbatim, and the button action doesn't fire with that either. That eliminates TornadoFX from the picture altogether. I'll take this up on the TestFX Gitter or issue tracker.
TestFX Gitter message for anyone that wants to follow along: https://gitter.im/TestFX/TestFX?at=5ceb78869d64e537bc1a45cb
a
When I say your test has caught a bug, I meant your code is not performing as expected. It's not an issue with TFX or TestFX - I would look at what you wrote and use a debugger to check what value your button has.
i
Hi @amanda.hinchman-dominguez, Thanks for the reply. The code is nothing more than:
Copy code
override val root = vbox {
	button(INITIAL_BUTTON_TEXT) {
		id = BUTTON_ID
		action {
			text = CHANGED_BUTTON_TEXT
		}
	}
}
As I mentioned:
If I launch the app and click the button manually, I do get the expected result.
So the code works fine run as an app, but not when invoked via the TestFX robot. The button text doesn't change. I set a breakpoint in the action lambda. It breaks when debugging the app, but not when debugging the test. Given that the example code on the TestFX page similarly fails, that leads me to conclude there is a bug in TestFX and not my code: https://github.com/TestFX/TestFX#junit-5-with-assertj-assertions In particular,
robot.clickOn(button)
does not result in the button action being invoked.
a
When you run your test, does it at least show the button with the first text?
i
It does, and that assertion succeeds. Then robot moves the cursor to the button, but the action doesn’t execute, so the text doesn’t change, and the second assertion fails.
If the robot is in fact clicking the button, it’s not resulting in invocation of the action.
a
hmm yeah that’s interesting - as much as I’d like to I can’t take a look myself - I’m still getting some issues with openJDK and JVM on my other project. And the text changes in the actual app?
i
No worries, I totally understand. Yep, the text changes when run as an app.
a
Curious! So here is what I’m wondering… is it possible that TestFX does not interpret
actions
since that is a TornadoFX DSL?
What if you tried putting that text change for the button in a separate function, and called the function from
action
?
I think it would be worth trying as an experiment
i
The thing is, if you look at the TestFX “JUnit 5 with AssertJ Assertions” example code from their Readme that I linked above, it doesn’t work as expected either. It’s essentially the same scenario, changing button text on click, and it’s 100% plain old JavaFX. As such, I’m confident I’ve eliminated TornadoFX as the cause of this issue. This is why I posted to the TestFX Gitter, also linked above. No reply there yet, but I’m keeping an eye on it.
👍 1
When running the test via Gradle from Terminal, which I hadn't tried previously, Mojave prompts to change the security settings, but not when run from IntelliJ for whatever reason.