`ApplicationTests` verwendet die echte MVC-Implementierung
authorKai Moritz <kai@juplo.de>
Sat, 5 Apr 2025 17:58:08 +0000 (19:58 +0200)
committerKai Moritz <kai@juplo.de>
Wed, 21 May 2025 18:14:12 +0000 (20:14 +0200)
* Der Test verwendet jetzt nicht mehr `@AutoconfigureMockMvc`.
* Statdessen wird `RANDOM_PORT` und das `TestRestTemplate` verwendet.
* Außerdem:
** Logging für den Zookeeper- und den Broker-Prozess unterdrückt.
** `ApplicationTests` überarbeitet/aufgeräumt.

pom.xml
src/main/java/de/juplo/kafka/ApplicationController.java [deleted file]
src/main/java/de/juplo/kafka/DeadLetterConsumer.java
src/main/java/de/juplo/kafka/DeadLetterController.java [new file with mode: 0644]
src/main/resources/logback.xml
src/test/java/de/juplo/kafka/ApplicationTests.java

diff --git a/pom.xml b/pom.xml
index 21bd07f..7da3b49 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <artifactId>spring-kafka-test</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.jayway.jsonpath</groupId>
+      <artifactId>json-path</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/src/main/java/de/juplo/kafka/ApplicationController.java b/src/main/java/de/juplo/kafka/ApplicationController.java
deleted file mode 100644 (file)
index e526f82..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-package de.juplo.kafka;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RestController;
-import reactor.core.publisher.Mono;
-
-
-@RestController
-public class ApplicationController
-{
-  @Autowired
-  DeadLetterConsumer deadLetterConsumer;
-
-
-  @GetMapping(path = "/{partition}/{offset}")
-  public Mono<String> recordAtOffset(
-    @PathVariable int partition,
-    @PathVariable long offset)
-  {
-    return deadLetterConsumer.requestRecord(partition, offset);
-  }
-}
index 54192a5..8cd553e 100644 (file)
@@ -99,10 +99,7 @@ public class DeadLetterConsumer implements Runnable
 
               fetchRequest.future().complete(record.value());
               schedulePendingFetchRequest(record.partition()).ifPresentOrElse(
-                (nextFetchRequest) ->
-                {
-                  scheduleFetchRequest(nextFetchRequest);
-                },
+                (nextFetchRequest) -> scheduleFetchRequest(nextFetchRequest),
                 () ->
                 {
                   log.info("{} - no pending fetch-requests for {}", id, partition);
diff --git a/src/main/java/de/juplo/kafka/DeadLetterController.java b/src/main/java/de/juplo/kafka/DeadLetterController.java
new file mode 100644 (file)
index 0000000..e8da81c
--- /dev/null
@@ -0,0 +1,24 @@
+package de.juplo.kafka;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RestController;
+import reactor.core.publisher.Mono;
+
+
+@RestController
+public class DeadLetterController
+{
+  @Autowired
+  DeadLetterConsumer deadLetterConsumer;
+
+
+  @GetMapping(path = "/{partition}/{offset}")
+  public Mono<String> recordAtOffset(
+    @PathVariable int partition,
+    @PathVariable long offset)
+  {
+    return deadLetterConsumer.requestRecord(partition, offset);
+  }
+}
index 9c7af76..a4c5192 100644 (file)
@@ -11,4 +11,8 @@
     <appender-ref ref="STDOUT" />
   </root>
 
+  <!-- Suppress Zookeeper and Kafka Broker logs in tests -->
+  <logger name="org.apache.zookeeper" level="ERROR"/>
+  <logger name="kafka" level="ERROR"/>
+
 </configuration>
index ae119bf..0a51022 100644 (file)
@@ -1,46 +1,42 @@
 package de.juplo.kafka;
 
+import com.jayway.jsonpath.JsonPath;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.HttpStatusCode;
+import org.springframework.http.ResponseEntity;
 import org.springframework.kafka.test.context.EmbeddedKafka;
-import org.springframework.test.web.servlet.MockMvc;
 
-import java.time.Duration;
-
-import static de.juplo.kafka.ApplicationTests.PARTITIONS;
+import static de.juplo.kafka.ApplicationTests.NUM_PARTITIONS;
 import static de.juplo.kafka.ApplicationTests.TOPIC;
-import static org.awaitility.Awaitility.await;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.assertj.core.api.Assertions.assertThat;
 
 
 @SpringBootTest(
+  webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
   properties = {
     "juplo.bootstrap-server=${spring.embedded.kafka.brokers}",
-    "juplo.consumer.topic=" + TOPIC })
-@AutoConfigureMockMvc
-@EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
+    "juplo.consumer.topic=" + TOPIC,
+    "logging.level.de.juplo.kafka=TRACE",
+  })
+@EmbeddedKafka(topics = TOPIC, partitions = NUM_PARTITIONS)
 public class ApplicationTests
 {
-  static final String TOPIC = "FOO";
-  static final int PARTITIONS = 10;
-
-  @Autowired
-  MockMvc mockMvc;
-
-
-
   @Test
   public void testApplicationStartup()
   {
-    await("Application is healthy")
-      .atMost(Duration.ofSeconds(5))
-      .untilAsserted(() -> mockMvc
-        .perform(get("/actuator/health"))
-        .andExpect(status().isOk())
-        .andExpect(jsonPath("status").value("UP")));
+    ResponseEntity<String> response = restTemplate.getForEntity("/actuator/health", String.class);
+    assertThat(response.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(HttpStatus.OK.value()));
+    assertThat(JsonPath.parse(response.getBody()).read("$.status", String.class)).isEqualTo("UP");
   }
+
+
+  static final String TOPIC = "ExampleConsumerTest_TEST";
+  static final int NUM_PARTITIONS = 7;
+
+  @Autowired
+  TestRestTemplate restTemplate;
 }