]> juplo.de Git - demos/kafka/training/commitdiff
`ApplicationTests` verwendet die echte MVC-Implementierung
authorKai Moritz <kai@juplo.de>
Sat, 21 Mar 2026 12:38:38 +0000 (13:38 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 22 Mar 2026 20:47:38 +0000 (21:47 +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.

build.gradle
pom.xml
src/main/resources/logback.xml
src/test/java/de/juplo/kafka/ApplicationTests.java

index 9b93c7f58b7856c5a930fcea58a4c4ac00af2fc8..6fd9cc94dd2b5e2b4320a068e7de25c8668275c7 100644 (file)
@@ -38,6 +38,7 @@ dependencies {
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        testImplementation 'org.springframework.boot:spring-boot-starter-webmvc-test'
        testImplementation 'org.springframework.boot:spring-boot-starter-kafka-test'
+       testImplementation 'com.jayway.jsonpath:json-path'
        testCompileOnly 'org.projectlombok:lombok'
        testAnnotationProcessor 'org.projectlombok:lombok'
        testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
diff --git a/pom.xml b/pom.xml
index cf35ebd159edfb501b0933f74b4f959623f4bbd7..f47dafab2731d43a37df8b9210974a614a5a7863 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <artifactId>spring-boot-starter-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 0ad310b3a7cc9eb991bd7ed2f748003f1c591f9c..f666b8bd4424a2a14cf75d42ab9d5751c869d318 100644 (file)
@@ -1,48 +1,44 @@
 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.annotation.DirtiesContext;
-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",
+  })
 @DirtiesContext
+@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;
 }