44import com .github .tomakehurst .wiremock .junit5 .WireMockRuntimeInfo ;
55import com .github .tomakehurst .wiremock .junit5 .WireMockTest ;
66import org .junit .jupiter .api .Test ;
7- import org .testfx .framework .junit5 .ApplicationTest ;
8- import javafx .application .Platform ;
9-
10- import java .util .concurrent .CountDownLatch ;
11- import java .util .concurrent .TimeUnit ;
7+ import org .awaitility .Awaitility ;
8+ import java .time .Duration ;
129
1310import static com .github .tomakehurst .wiremock .client .WireMock .*;
1411import static org .assertj .core .api .Assertions .assertThat ;
15- import static org .assertj .core .api .Assertions .fail ;
1612
1713@ WireMockTest
18- class ChatModelTest extends ApplicationTest {
14+ class ChatModelTest {
1915
2016 @ Test
2117 void sendMessageCallsConnectionWithMessagesToSend () {
@@ -50,6 +46,8 @@ void checkReceivedMessagesAfterSendingAMessageToAFakeServer(WireMockRuntimeInfo
5046 var conImp = new NtfyConnectionImpl ("http://localhost:" + wmRunTimeInfo .getHttpPort ());
5147
5248 String expectedMessage = "Async Data Received" ;
49+ // Använd System.currentTimeMillis() om din ChatModel skickar detta;
50+ // annars, använd ett statiskt värde för att vara säker.
5351 long expectedTimestamp = System .currentTimeMillis ();
5452
5553 stubFor (get ("/mytopic/json" )
@@ -60,22 +58,14 @@ void checkReceivedMessagesAfterSendingAMessageToAFakeServer(WireMockRuntimeInfo
6058 var model = new ChatModel (conImp );
6159 model .startReceiving ();
6260
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 ();
61+ // Awaitility: Väntar aktivt tills listan fylls.
62+ Awaitility .await ()
63+ .atMost (Duration .ofSeconds (10 ))
64+ .pollInterval (Duration .ofMillis (100 ))
65+ .until (() -> model .getMessages ().size () > 0 );
66+
67+ assertThat (model .getMessages ()).isNotEmpty ();
68+ assertThat (model .getMessages ().getLast ().content ()).isEqualTo (expectedMessage );
69+ assertThat (model .getMessages ().getLast ().timestamp ()).isEqualTo (expectedTimestamp );
8070 }
8171}
0 commit comments