Fix tests for Spring Boot 4 and test config

This commit is contained in:
root
2026-02-02 15:47:40 +01:00
parent 9259fa6ca9
commit 4d794afc7d
3 changed files with 33 additions and 6 deletions
+2
View File
@@ -26,6 +26,8 @@ dependencies {
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0' 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-starter-test'
testImplementation 'org.springframework.boot:spring-boot-test'
testImplementation 'org.springframework.boot:spring-boot-test-autoconfigure'
} }
tasks.named('test') { tasks.named('test') {
@@ -3,7 +3,12 @@ package de.dev089.eventproducer;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest @SpringBootTest(properties = {
"RABBITMQ_HOST=localhost",
"RABBITMQ_PORT=5672",
"RABBITMQ_USERNAME=guest",
"RABBITMQ_PASSWORD=guest"
})
class EventProducerApplicationTests { class EventProducerApplicationTests {
@Test @Test
@@ -1,13 +1,18 @@
package de.dev089.eventproducer.web; package de.dev089.eventproducer.web;
import de.dev089.eventproducer.producer.HealthRequestProducer; import de.dev089.eventproducer.producer.HealthRequestProducer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired; 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.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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; 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.ArgumentMatchers.any;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@@ -20,14 +25,29 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
"RABBITMQ_USERNAME=guest", "RABBITMQ_USERNAME=guest",
"RABBITMQ_PASSWORD=guest" "RABBITMQ_PASSWORD=guest"
}) })
@AutoConfigureMockMvc
class RequestProducerControllerIT { class RequestProducerControllerIT {
@Autowired @Autowired
private WebApplicationContext context;
@Autowired
private HealthRequestProducer healthRequestProducer;
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean @BeforeEach
private HealthRequestProducer healthRequestProducer; void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@TestConfiguration
static class TestConfig {
@Bean
@Primary
HealthRequestProducer healthRequestProducer() {
return Mockito.mock(HealthRequestProducer.class);
}
}
@Test @Test
void postPopulate_acceptsValidRequest() throws Exception { void postPopulate_acceptsValidRequest() throws Exception {