|
9 | 9 | import org.junit.jupiter.api.BeforeAll; |
10 | 10 | import org.junit.jupiter.api.DisplayName; |
11 | 11 | import org.junit.jupiter.api.Test; |
| 12 | +import org.awaitility.Awaitility; |
12 | 13 |
|
| 14 | +import java.time.Duration; |
13 | 15 | import java.util.concurrent.ExecutionException; |
14 | 16 |
|
15 | 17 | import static com.github.tomakehurst.wiremock.client.WireMock.*; |
@@ -70,34 +72,37 @@ void checkReceivedMessagesAfterSendingAMessageToServer() throws InterruptedExcep |
70 | 72 | model.setMessageToSend("Hello World"); |
71 | 73 | //Act - When |
72 | 74 |
|
73 | | - model.sendMessage(); |
74 | | - |
75 | | - model.receiveMessage(); |
76 | | - |
77 | | - Thread.sleep(1000); |
| 75 | + model.sendMessage().join(); |
78 | 76 |
|
79 | | - var messageList = model.getMessages(); |
80 | 77 | //Assert |
81 | | - assertThat(messageList).extracting(NtfyMessageDto::message).contains("Hello World"); |
| 78 | + Awaitility.await() |
| 79 | + .atMost(Duration.ofSeconds(5)) |
| 80 | + .pollInterval(Duration.ofMillis(100)) |
| 81 | + .untilAsserted(() -> { |
| 82 | + assertThat(model.getMessages()).isNotEmpty(); |
| 83 | + assertThat(model.getMessages().getLast().message()).contains("Hello World"); |
| 84 | + }); |
82 | 85 | } |
83 | 86 |
|
84 | 87 | @Test |
85 | 88 | void checkReceivedMessagesAfterSendingAMessageToAFakeServer(WireMockRuntimeInfo wmRunTimeInfo) throws InterruptedException { |
86 | 89 | // Arrange |
87 | 90 | var conImp = new NtfyConnectionImpl("http://localhost:" + wmRunTimeInfo.getHttpPort()); |
88 | 91 |
|
89 | | - //stubFor(get("/mytopic/json").willReturn(ok("{\"message\":\"hej\"}"))); |
90 | 92 | stubFor(get("/mytopic/json") |
91 | 93 | .willReturn(aResponse() |
92 | 94 | .withHeader("Content-type", "application/json") |
93 | 95 | .withBody("{\"event\": \"message\",\"message\": \"Hello World\", \"time\": \"12314244\"}"))); |
94 | 96 |
|
95 | 97 | var model = new HelloModel(conImp); |
96 | 98 |
|
97 | | - Thread.sleep(1000); |
98 | | - |
99 | | - assertThat(model.getMessages().getLast().message()).isEqualTo("Hello World"); |
100 | | - |
| 99 | + Awaitility.await() |
| 100 | + .atMost(Duration.ofSeconds(4)) |
| 101 | + .pollInterval(Duration.ofMillis(100)) // Kolla var 100:e ms |
| 102 | + .untilAsserted(() -> { |
| 103 | + assertThat(model.getMessages()).isNotEmpty(); |
| 104 | + assertThat(model.getMessages().getLast().message()).isEqualTo("Hello World"); |
| 105 | + }); |
101 | 106 | } |
102 | 107 |
|
103 | 108 | // Test som skickar in ett fake:at meddelande via record och kollar att meddelandet finns i observablelistan |
|
0 commit comments