From 0e6ab1b9299127395f1169c378334d5684f61414 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 5 Apr 2025 19:58:08 +0200 Subject: [PATCH] `ApplicationTests` verwendet die echte MVC-Implementierung MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * 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 | 5 ++ src/main/resources/logback.xml | 4 ++ .../java/de/juplo/kafka/ApplicationTests.java | 48 +++++++++---------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index bf964ed7..1daa4926 100644 --- a/pom.xml +++ b/pom.xml @@ -68,6 +68,11 @@ spring-kafka-test test + + com.jayway.jsonpath + json-path + test + diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 9c7af767..a4c51929 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -11,4 +11,8 @@ + + + + diff --git a/src/test/java/de/juplo/kafka/ApplicationTests.java b/src/test/java/de/juplo/kafka/ApplicationTests.java index 61197cbf..fadb8e52 100644 --- a/src/test/java/de/juplo/kafka/ApplicationTests.java +++ b/src/test/java/de/juplo/kafka/ApplicationTests.java @@ -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 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; } -- 2.39.5