Closed Bendat closed 5 years ago
Inside of overrride fun init() {...}
I would try a @Before
annotation for your setup and and @After
for the teardown.
Your message is saying that the stage it's trying to get in registerPrimaryStage
is not initialized. This means that the view was probably not added to the stage. For example:
class Test: ApplicationTest() {
lateinit var primaryStage: Stage
val view = TestView()
@Before
fun setupApplication() {
primaryStage = FxToolkit.registerPrimaryStage()
val fragment = find<Editor>(CatScheduleScope())
view.root.add(fragment.root)
// Stage objects must be constructed and modified on the JavaFX App Thread
interact {
primaryStage.scene = Scene(view.root)
primaryStage.show()
primaryStage.toFront()
}
}
...
}
Feel free to refer to this blurb on TestFX and TornadoFX for referencing! I know I had a tough time with this starting out and it feels like the set up for everyone is different.
H and Thanks @ahinchman1 however this solution didn't work for me.
What did work was to wrap components being tested in an App type. The Install Wizard looks like
class InstallWizard : Wizard() {
override val canFinish = allPagesComplete
override val canGoNext = currentPageComplete
private val controller = InstallWizardController()
val model by inject<InstallWizardViewModel>(params = mapOf("controller" to controller))
init {
add(RegistrationPage::class)
add(SetupPage::class)
}
}
Next I created a wrapper for it:
class WizardApp : App(InstallWizard::class) {
val root by inject<InstallWizard>()
}
Then in TestFX I load this app with setupFixture as follows (Groovy, Spock)
lass SetupPageSpec extends ApplicationSpec {
WizardApp app = null
Stage stage
Button finish
Button next
String password
File testDir = new File(new File(System.getProperty("user.home")), "sample-test")
private def faker = new Faker()
@Override
void start(Stage stage) {
app = new WizardApp()
FxToolkit.registerPrimaryStage()
FxToolkit.setupFixture {
this.stage = new Stage(StageStyle.UNIFIED)
app.start(stage)
stage.show()
}
}
Which allows dependency injection to work, but loses the ability to run a single test across multiple methods (the fixture is torn down after each method this way)
Hi,
I'm trying to write a test for my tornadofx application using TestFx. I'm trying to open a wizard by pressing a button, however I'm receiving the following exception:
The test is setup up as follows:
As an aside, onDock is not called either. It can be run on it's own using the intellij plugin just fine.
I'd appreciate any guidance