From 4d794afc7d6353880bb3ee967a71b41f6f7d998c Mon Sep 17 00:00:00 2001 From: root Date: Mon, 2 Feb 2026 15:47:40 +0100 Subject: [PATCH] Fix tests for Spring Boot 4 and test config --- build.gradle | 2 ++ .../EventProducerApplicationTests.java | 7 ++++- .../web/RequestProducerControllerIT.java | 30 +++++++++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 43997cc..9df8580 100644 --- a/build.gradle +++ b/build.gradle @@ -26,6 +26,8 @@ dependencies { implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'org.springframework.boot:spring-boot-test' + testImplementation 'org.springframework.boot:spring-boot-test-autoconfigure' } tasks.named('test') { diff --git a/src/test/java/de/dev089/eventproducer/EventProducerApplicationTests.java b/src/test/java/de/dev089/eventproducer/EventProducerApplicationTests.java index 4f07b79..408d850 100644 --- a/src/test/java/de/dev089/eventproducer/EventProducerApplicationTests.java +++ b/src/test/java/de/dev089/eventproducer/EventProducerApplicationTests.java @@ -3,7 +3,12 @@ package de.dev089.eventproducer; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; -@SpringBootTest +@SpringBootTest(properties = { + "RABBITMQ_HOST=localhost", + "RABBITMQ_PORT=5672", + "RABBITMQ_USERNAME=guest", + "RABBITMQ_PASSWORD=guest" +}) class EventProducerApplicationTests { @Test diff --git a/src/test/java/de/dev089/eventproducer/web/RequestProducerControllerIT.java b/src/test/java/de/dev089/eventproducer/web/RequestProducerControllerIT.java index abe48eb..56ab71c 100644 --- a/src/test/java/de/dev089/eventproducer/web/RequestProducerControllerIT.java +++ b/src/test/java/de/dev089/eventproducer/web/RequestProducerControllerIT.java @@ -1,13 +1,18 @@ package de.dev089.eventproducer.web; import de.dev089.eventproducer.producer.HealthRequestProducer; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; 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.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; +import org.springframework.boot.test.context.TestConfiguration; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verify; @@ -20,14 +25,29 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. "RABBITMQ_USERNAME=guest", "RABBITMQ_PASSWORD=guest" }) -@AutoConfigureMockMvc class RequestProducerControllerIT { @Autowired + private WebApplicationContext context; + + @Autowired + private HealthRequestProducer healthRequestProducer; + private MockMvc mockMvc; - @MockBean - private HealthRequestProducer healthRequestProducer; + @BeforeEach + void setUp() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); + } + + @TestConfiguration + static class TestConfig { + @Bean + @Primary + HealthRequestProducer healthRequestProducer() { + return Mockito.mock(HealthRequestProducer.class); + } + } @Test void postPopulate_acceptsValidRequest() throws Exception {