等待单元测试中的Platform.RunLater [英] Wait for Platform.RunLater in a unit test

查看:93
本文介绍了等待单元测试中的Platform.RunLater的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表示形式类,该类存储一个XYChart.Series对象,并通过观察模型对其进行更新.通过使用Platform.runLater(...)

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

我要对此单元进行测试,确保runLater中的命令正确执行.如何告诉单元测试等待runLater命令完成? 现在我要做的就是在测试线程上使用Thread.Sleep(...),给FXApplicationThread时间来完成,但这听起来很愚蠢.

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.

推荐答案

我解决的方法如下.

1)创建一个简单的信号量函数,如下所示:

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)每当需要等待时,都呼叫waitForRunLater().因为Platform.runLater()(根据javadoc)按照提交的顺序执行可运行对象,所以您可以在测试中编写:

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(...)`

适用于简单测试

这篇关于等待单元测试中的Platform.RunLater的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆