]> juplo.de Git - demos/kafka/training/commitdiff
`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>
Fri, 20 Mar 2026 15:20:41 +0000 (16:20 +0100)
* 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/resources/logback.xml
src/test/java/de/juplo/kafka/ApplicationTests.java

diff --git a/pom.xml b/pom.xml
index bf964ed757076e35dffbb51ed777fbd4abcf13c0..1daa492692485c03154c6d7dd3bef9136e320aec 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>
index 9c7af7672086263b9c78c7648f6f16d22ceef5b4..a4c51929e3caa178fe53bdf4cb5f5bb9cfba9c13 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 61197cbf1b82dc55d9c3123ab9a4ad38091b2e59..fadb8e52d923ced7b3c7c9e88aa0f0924d4649e7 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.resttestclient.TestRestTemplate;
 import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
+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;
 }