I have a presentation class storing an XYChart.Series object and updating it by observing the model. The Series updating is done by using Platform.runLater(...)
I want to unit-test this, making sure the commands in runLater are performed correctly. How do I tell the unit-test to wait for the runLater commands to be done? Right now all I do is Thread.Sleep(...) on the test-thread to give the FXApplicationThread the time to complete, but that sounds stupid.
PlatformIO comes with its own tool called Unit Testing Engine to help you start testing as quickly as possible. Out-of-the-box support for a unit testing framework called Unity and a built-in test runner allows us to start writing tests without any preconfiguration. We just need to add new files with test cases to the test folder.
Java Platform.runLater - 30 examples found. These are the top rated real world Java examples of javafx.application.Platform.runLater extracted from open source projects. You can rate examples to help us improve the quality of examples.
To run a unit test as a 64-bit process 1 If your code or tests were compiled as 32-bit/x86, but you now want to run them as a 64-bit process, recompile them as... 2 Set the unit tests to run as a 64-bit process.#N#From the Visual Studio menu, choose Test, then choose Processor... More ...
In a nutshell, unit testing boils down to splitting the code into small units which can be tested in isolation to verify their behavior in different circumstances. The main benefit of thorough unit testing is looser coupling between software modules which implicitly leads to better software design.
The way I solved it is as follows.
1) Create a simple semaphore function like this:
public static void waitForRunLater() throws InterruptedException {
Semaphore semaphore = new Semaphore(0);
Platform.runLater(() -> semaphore.release());
semaphore.acquire();
}
2) Call waitForRunLater()
whenever you need to wait. Because Platform.runLater()
(according to the javadoc) execute runnables in the order they were submitted, you can just write within a test:
...
commandThatSpawnRunnablesInJavaFxThread(...)
waitForRunLater(...)
asserts(...)`
which works for simple tests
To have it more in AssertJ style syntax, you can do something like this:
@Test
public void test() throws InterruptedException {
// do test here
assertAfterJavaFxPlatformEventsAreDone(() -> {
// do assertions here
}
}
private void assertAfterJavaFxPlatformEventsAreDone(Runnable runnable) throws InterruptedException {
waitOnJavaFxPlatformEventsDone();
runnable.run();
}
private void waitOnJavaFxPlatformEventsDone() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
Platform.runLater(countDownLatch::countDown);
countDownLatch.await();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With