Wait for Platform.RunLater in a unit test

前端 未结 3 512
清酒与你
清酒与你 2021-01-13 06:02

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 wa

3条回答
  •  温柔的废话
    2021-01-13 06:40

    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();
        }
    }
    

提交回复
热议问题