I am using OpenJFX 13 with a presentation model that has DoubleProperty
field that gets updated like this:
public class MyPresentationModel {
private DoubleProperty batteryLevel = new SimpleDoubleProperty<>(100.0);
public void setBatteryLevel(double value) {
Platform.runLater( () ->
batteryLevel.setValue(value) );
}
}
The Platform.runLater
is needed because the setter can be called from any thread.
If I now want to test this is a simple unit test (Using JUnit 5 with AssertJ), the test fails because the JavaFX toolkit is not initialized.
No problem, I add TestFX to my project and update the test to:
@ExtendWith(ApplicationExtension.class)
class MyPresentationModelTest {
@Test
void test() {
MyPresentationModel pm = new MyPresentationModel();
pm.setBatteryLevel(75.0);
assertThat(pm.batteryLevelProperty().get()).isCloseTo(75.0, Offset.offset(0.1));
}
}
Result:
java.lang.AssertionError:
Expecting:
<100.0>
to be close to:
<75.0>
by less than <0.1> but difference was <25.0>.
(a difference of exactly <0.1> being considered valid)
Which is logical given the fact that the update is done on the JavaFx thread.
So how do I wait until the JavaFx thread has updated the property so I can assert the value?