feat: Refined `ChatBackendApplicationTest`
authorKai Moritz <kai@juplo.de>
Wed, 11 Jan 2023 20:55:07 +0000 (21:55 +0100)
committerKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 20:59:37 +0000 (21:59 +0100)
- Switched to a full server-startup (no MockMvc).
- Added some requests, that check the availability of the expected data.

pom.xml
src/test/java/de/juplo/kafka/chat/backend/ChatBackendApplicationTests.java
src/test/resources/data/618e89ae-fdc0-4c65-8055-f49908295e8f.json [deleted file]
src/test/resources/data/chatrooms.json [deleted file]
src/test/resources/data/files/618e89ae-fdc0-4c65-8055-f49908295e8f.json [new file with mode: 0644]
src/test/resources/data/files/chatrooms.json [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 30867b4..e10af5e 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-webflux</artifactId>
     </dependency>
-
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-actuator</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-devtools</artifactId>
       <artifactId>junit-jupiter</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.awaitility</groupId>
+      <artifactId>awaitility</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
index 9accd8a..9dac22e 100644 (file)
@@ -1,13 +1,54 @@
 package de.juplo.kafka.chat.backend;
 
 import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.web.server.LocalServerPort;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.reactive.server.WebTestClient;
+import org.testcontainers.shaded.org.awaitility.Awaitility;
 
-@SpringBootTest(properties = "chat.backend.storage-directory=target/test-classes/data/")
+import java.time.Duration;
+
+
+@SpringBootTest(
+               webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
+               properties = "chat.backend.storage-directory=target/test-classes/data/files")
 class ChatBackendApplicationTests
 {
+       @LocalServerPort
+       private int port;
+       @Autowired
+       private WebTestClient webTestClient;
+
        @Test
        void contextLoads()
        {
+               Awaitility
+                               .await()
+                               .atMost(Duration.ofSeconds(15))
+                               .untilAsserted(() ->
+                               {
+                                       webTestClient
+                                                       .get()
+                                                       .uri("http://localhost:{port}/actuator/health", port)
+                                                       .exchange()
+                                                       .expectStatus().isOk()
+                                                       .expectBody().jsonPath("$.status").isEqualTo("UP");
+                                       webTestClient
+                                                       .get()
+                                                       .uri("http://localhost:{port}/618e89ae-fdc0-4c65-8055-f49908295e8f", port)
+                                                       .accept(MediaType.APPLICATION_JSON)
+                                                       .exchange()
+                                                       .expectStatus().isOk()
+                                                       .expectBody().jsonPath("$.name").isEqualTo("Peter's Chat-Room");
+                                       webTestClient
+                                                       .get()
+                                                       .uri("http://localhost:{port}/4ace69b7-a79f-481b-ad0d-d756d60b66ec/ute/1478", port)
+                                                       .accept(MediaType.APPLICATION_JSON)
+                                                       .exchange()
+                                                       .expectStatus().isOk()
+                                                       .expectBody().jsonPath("$.text").isEqualTo("Nachricht Nr. 1478");
+                               });
        }
 }
diff --git a/src/test/resources/data/618e89ae-fdc0-4c65-8055-f49908295e8f.json b/src/test/resources/data/618e89ae-fdc0-4c65-8055-f49908295e8f.json
deleted file mode 100644 (file)
index 0c8abcb..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-[ {
-  "id" : 1477,
-  "serial" : 0,
-  "time" : "2023-01-08T00:10:08.817267716",
-  "user" : "ute",
-  "text" : "Nachricht Nr. 1477"
-}, {
-  "id" : 1478,
-  "serial" : 1,
-  "time" : "2023-01-08T00:10:10.03333094",
-  "user" : "ute",
-  "text" : "Nachricht Nr. 1478"
-}, {
-  "id" : 1479,
-  "serial" : 2,
-  "time" : "2023-01-08T00:10:11.22582036",
-  "user" : "ute",
-  "text" : "Nachricht Nr. 1479"
-} ]
diff --git a/src/test/resources/data/chatrooms.json b/src/test/resources/data/chatrooms.json
deleted file mode 100644 (file)
index 5b6c22b..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-[ {
-  "id" : "618e89ae-fdc0-4c65-8055-f49908295e8f",
-  "name" : "Peter's Chat-Room"
-} ]
\ No newline at end of file
diff --git a/src/test/resources/data/files/618e89ae-fdc0-4c65-8055-f49908295e8f.json b/src/test/resources/data/files/618e89ae-fdc0-4c65-8055-f49908295e8f.json
new file mode 100644 (file)
index 0000000..0c8abcb
--- /dev/null
@@ -0,0 +1,19 @@
+[ {
+  "id" : 1477,
+  "serial" : 0,
+  "time" : "2023-01-08T00:10:08.817267716",
+  "user" : "ute",
+  "text" : "Nachricht Nr. 1477"
+}, {
+  "id" : 1478,
+  "serial" : 1,
+  "time" : "2023-01-08T00:10:10.03333094",
+  "user" : "ute",
+  "text" : "Nachricht Nr. 1478"
+}, {
+  "id" : 1479,
+  "serial" : 2,
+  "time" : "2023-01-08T00:10:11.22582036",
+  "user" : "ute",
+  "text" : "Nachricht Nr. 1479"
+} ]
diff --git a/src/test/resources/data/files/chatrooms.json b/src/test/resources/data/files/chatrooms.json
new file mode 100644 (file)
index 0000000..5b6c22b
--- /dev/null
@@ -0,0 +1,4 @@
+[ {
+  "id" : "618e89ae-fdc0-4c65-8055-f49908295e8f",
+  "name" : "Peter's Chat-Room"
+} ]
\ No newline at end of file