Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for Platform.RunLater in a unit test

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.

like image 391
CarrKnight Avatar asked Apr 02 '14 19:04

CarrKnight


People also ask

How to use unit testing engine in platformio?

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.

How many examples of JavaFX Application Platform runlater are there?

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.

How to run a unit test as a 64-bit process?

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 ...

What is unit testing in software testing?

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.


2 Answers

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

like image 197
CarrKnight Avatar answered Oct 01 '22 23:10

CarrKnight


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();
    }
}
like image 29
Wim Deblauwe Avatar answered Oct 01 '22 23:10

Wim Deblauwe