Migrate MockMvc tests to MockMvcTester

See gh-41198
This commit is contained in:
Stéphane Nicoll
2024-07-03 12:54:38 +02:00
parent e5859aedaf
commit 156237227c
77 changed files with 1050 additions and 1239 deletions

View File

@@ -36,6 +36,7 @@ import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.mock.web.MockFilterChain;
@@ -43,15 +44,12 @@ import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.security.config.BeanIds;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Tests for {@link RemoteDevToolsAutoConfiguration}.
@@ -150,14 +148,10 @@ class RemoteDevToolsAutoConfigurationTests {
void securityConfigurationShouldAllowAccess() throws Exception {
this.context = getContext(() -> loadContext("spring.devtools.remote.secret:supersecret"));
DispatcherFilter filter = this.context.getBean(DispatcherFilter.class);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(springSecurity())
.addFilter(filter)
.build();
mockMvc
.perform(MockMvcRequestBuilders.get(DEFAULT_CONTEXT_PATH + "/restart")
.header(DEFAULT_SECRET_HEADER_NAME, "supersecret"))
.andExpect(status().isOk());
MockMvcTester mvc = MockMvcTester.from(this.context,
(builder) -> builder.apply(springSecurity()).addFilter(filter).build());
assertThat(mvc.get().uri(DEFAULT_CONTEXT_PATH + "/restart").header(DEFAULT_SECRET_HEADER_NAME, "supersecret"))
.hasStatusOk();
assertRestartInvoked(true);
assertThat(this.context.containsBean("devtoolsSecurityFilterChain")).isTrue();
}
@@ -167,14 +161,10 @@ class RemoteDevToolsAutoConfigurationTests {
this.context = getContext(() -> loadContext("spring.devtools.remote.secret:supersecret",
"server.servlet.context-path:/test", "spring.devtools.remote.context-path:/custom"));
DispatcherFilter filter = this.context.getBean(DispatcherFilter.class);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(springSecurity())
.addFilter(filter)
.build();
mockMvc
.perform(MockMvcRequestBuilders.get("/test/custom/restart")
.header(DEFAULT_SECRET_HEADER_NAME, "supersecret"))
.andExpect(status().isOk());
MockMvcTester mvc = MockMvcTester.from(this.context,
(builder) -> builder.apply(springSecurity()).addFilter(filter).build());
assertThat(mvc.get().uri("/test/custom/restart").header(DEFAULT_SECRET_HEADER_NAME, "supersecret"))
.hasStatusOk();
assertRestartInvoked(true);
}
@@ -183,11 +173,9 @@ class RemoteDevToolsAutoConfigurationTests {
this.context = getContext(() -> loadContext("spring.devtools.remote.secret:supersecret"));
DispatcherFilter filter = this.context.getBean(DispatcherFilter.class);
Filter securityFilterChain = this.context.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN, Filter.class);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.addFilter(securityFilterChain)
.addFilter(filter)
.build();
mockMvc.perform(MockMvcRequestBuilders.get("/my-path")).andExpect(status().isUnauthorized());
MockMvcTester mvc = MockMvcTester.from(this.context,
(builder) -> builder.addFilter(securityFilterChain).addFilter(filter).build());
assertThat(mvc.get().uri("/my-path")).hasStatus(HttpStatus.UNAUTHORIZED);
}
@Test