44import com .github .tomakehurst .wiremock .junit5 .WireMockRuntimeInfo ;
55import com .github .tomakehurst .wiremock .junit5 .WireMockTest ;
66import org .junit .jupiter .api .Test ;
7- import org .awaitility .Awaitility ;
87import org .testfx .framework .junit5 .ApplicationTest ;
98import javafx .application .Platform ;
109
11- import java .time .Duration ;
10+ import java .util .concurrent .CountDownLatch ;
11+ import java .util .concurrent .TimeUnit ;
1212
1313import static com .github .tomakehurst .wiremock .client .WireMock .*;
1414import static org .assertj .core .api .Assertions .assertThat ;
15+ import static org .assertj .core .api .Assertions .fail ;
1516
1617@ WireMockTest
1718class ChatModelTest extends ApplicationTest {
@@ -45,7 +46,7 @@ void sendMessageToFakeServer_viaNtfyConnectionImpl(WireMockRuntimeInfo wmRunTime
4546
4647
4748 @ Test
48- void checkReceivedMessagesAfterSendingAMessageToAFakeServer (WireMockRuntimeInfo wmRunTimeInfo ) {
49+ void checkReceivedMessagesAfterSendingAMessageToAFakeServer (WireMockRuntimeInfo wmRunTimeInfo ) throws InterruptedException {
4950 var conImp = new NtfyConnectionImpl ("http://localhost:" + wmRunTimeInfo .getHttpPort ());
5051
5152 String expectedMessage = "Async Data Received" ;
@@ -59,15 +60,22 @@ void checkReceivedMessagesAfterSendingAMessageToAFakeServer(WireMockRuntimeInfo
5960 var model = new ChatModel (conImp );
6061 model .startReceiving ();
6162
62- Awaitility .await ()
63- .atMost (Duration .ofSeconds (4 ))
64- .pollInterval (Duration .ofMillis (100 ))
65- .untilAsserted (() -> {
66- Platform .runLater (() -> {
67- assertThat (model .getMessages ()).isNotEmpty ();
68- assertThat (model .getMessages ().getLast ().content ()).isEqualTo (expectedMessage );
69- assertThat (model .getMessages ().getLast ().timestamp ()).isEqualTo (expectedTimestamp );
70- });
71- });
63+ CountDownLatch latch = new CountDownLatch (1 );
64+
65+ Platform .runLater (() -> {
66+ try {
67+ assertThat (model .getMessages ()).isNotEmpty ();
68+ assertThat (model .getMessages ().getLast ().content ()).isEqualTo (expectedMessage );
69+ assertThat (model .getMessages ().getLast ().timestamp ()).isEqualTo (expectedTimestamp );
70+ } catch (AssertionError e ) {
71+ fail ("Assertion failed inside Platform.runLater: " + e .getMessage ());
72+ } finally {
73+ latch .countDown ();
74+ }
75+ });
76+
77+ assertThat (latch .await (5 , TimeUnit .SECONDS ))
78+ .as ("Timed out waiting for JavaFX thread to execute assertions." )
79+ .isTrue ();
7280 }
7381}
0 commit comments