Polish
This commit is contained in:
@@ -97,6 +97,10 @@ import static org.mockito.Mockito.verifyNoInteractions;
|
||||
@SpringJUnitWebConfig
|
||||
public class MockMvcTesterIntegrationTests {
|
||||
|
||||
private static final MockMultipartFile file = new MockMultipartFile("file", "content.txt", null,
|
||||
"value".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
|
||||
private final MockMvcTester mvc;
|
||||
|
||||
MockMvcTesterIntegrationTests(WebApplicationContext wac) {
|
||||
@@ -106,9 +110,6 @@ public class MockMvcTesterIntegrationTests {
|
||||
@Nested
|
||||
class PerformTests {
|
||||
|
||||
private final MockMultipartFile file = new MockMultipartFile("file", "content.txt", null,
|
||||
"value".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
@Test
|
||||
void syncRequestWithDefaultExchange() {
|
||||
assertThat(mvc.get().uri("/greet")).hasStatusOk();
|
||||
@@ -123,7 +124,7 @@ public class MockMvcTesterIntegrationTests {
|
||||
@Test
|
||||
void asyncMultipartRequestWithDefaultExchange() {
|
||||
assertThat(mvc.post().uri("/multipart-streaming").multipart()
|
||||
.file(this.file).param("timeToWait", "100"))
|
||||
.file(file).param("timeToWait", "100"))
|
||||
.hasStatusOk().hasBodyTextEqualTo("name=Joe&file=content.txt");
|
||||
}
|
||||
|
||||
@@ -141,7 +142,7 @@ public class MockMvcTesterIntegrationTests {
|
||||
@Test
|
||||
void asyncMultipartRequestWitExplicitExchange() {
|
||||
assertThat(mvc.post().uri("/multipart-streaming").multipart()
|
||||
.file(this.file).param("timeToWait", "100").exchange())
|
||||
.file(file).param("timeToWait", "100").exchange())
|
||||
.hasStatusOk().hasBodyTextEqualTo("name=Joe&file=content.txt");
|
||||
}
|
||||
|
||||
@@ -161,7 +162,7 @@ public class MockMvcTesterIntegrationTests {
|
||||
@Test
|
||||
void asyncMultipartRequestWithExplicitExchangeAndEnoughTimeToWait() {
|
||||
assertThat(mvc.post().uri("/multipart-streaming").multipart()
|
||||
.file(this.file).param("timeToWait", "100").exchange(Duration.ofMillis(200)))
|
||||
.file(file).param("timeToWait", "100").exchange(Duration.ofMillis(200)))
|
||||
.hasStatusOk().hasBodyTextEqualTo("name=Joe&file=content.txt");
|
||||
}
|
||||
|
||||
@@ -176,7 +177,7 @@ public class MockMvcTesterIntegrationTests {
|
||||
@Test
|
||||
void asyncMultipartRequestWithExplicitExchangeAndNotEnoughTimeToWait() {
|
||||
MockMultipartMvcRequestBuilder builder = mvc.post().uri("/multipart-streaming").multipart()
|
||||
.file(this.file).param("timeToWait", "500");
|
||||
.file(file).param("timeToWait", "500");
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> builder.exchange(Duration.ofMillis(100)))
|
||||
.withMessageContaining("was not set during the specified timeToWait=100");
|
||||
@@ -192,11 +193,24 @@ public class MockMvcTesterIntegrationTests {
|
||||
.request().hasAsyncStarted(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasAsyncStartedForMultipartTrue() {
|
||||
assertThat(mvc.post().uri("/multipart-streaming").multipart()
|
||||
.file(file).param("timeToWait", "100").asyncExchange())
|
||||
.request().hasAsyncStarted(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasAsyncStartedFalse() {
|
||||
assertThat(mvc.get().uri("/greet").asyncExchange()).request().hasAsyncStarted(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasAsyncStartedForMultipartFalse() {
|
||||
assertThat(mvc.put().uri("/multipart-put").multipart().file(file).asyncExchange())
|
||||
.request().hasAsyncStarted(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void attributes() {
|
||||
assertThat(mvc.get().uri("/greet")).request().attributes()
|
||||
@@ -220,8 +234,7 @@ public class MockMvcTesterIntegrationTests {
|
||||
|
||||
@Test
|
||||
void multipartWithPut() {
|
||||
MockMultipartFile part = new MockMultipartFile("file", "content.txt", null, "value".getBytes(StandardCharsets.UTF_8));
|
||||
assertThat(mvc.put().uri("/multipart-put").multipart().file(part).file(JSON_PART_FILE))
|
||||
assertThat(mvc.put().uri("/multipart-put").multipart().file(file).file(JSON_PART_FILE))
|
||||
.hasStatusOk()
|
||||
.hasViewName("index")
|
||||
.model().contains(entry("name", "file"));
|
||||
|
||||
@@ -19,19 +19,25 @@ package org.springframework.test.web.servlet.assertj;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.ServletContext;
|
||||
import jakarta.servlet.ServletException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.cglib.core.internal.Function;
|
||||
import org.springframework.context.annotation.AnnotationConfigUtils;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.test.json.AbstractJsonContentAssert;
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester.MockMvcRequestBuilder;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -57,6 +63,8 @@ class MockMvcTesterTests {
|
||||
private static final MappingJackson2HttpMessageConverter jsonHttpMessageConverter =
|
||||
new MappingJackson2HttpMessageConverter(new ObjectMapper());
|
||||
|
||||
private final ServletContext servletContext = new MockServletContext();
|
||||
|
||||
|
||||
@Test
|
||||
void createShouldRejectNullMockMvc() {
|
||||
@@ -139,6 +147,67 @@ class MockMvcTesterTests {
|
||||
assertThat(result).hasFieldOrPropertyWithValue("mvcResult", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getConfiguresBuilder() {
|
||||
assertThat(createMockHttpServletRequest(tester -> tester.get().uri("/hello")))
|
||||
.satisfies(hasSettings(HttpMethod.GET, "/hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void headConfiguresBuilder() {
|
||||
assertThat(createMockHttpServletRequest(tester -> tester.head().uri("/download")))
|
||||
.satisfies(hasSettings(HttpMethod.HEAD, "/download"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void postConfiguresBuilder() {
|
||||
assertThat(createMockHttpServletRequest(tester -> tester.post().uri("/save")))
|
||||
.satisfies(hasSettings(HttpMethod.POST, "/save"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putConfiguresBuilder() {
|
||||
assertThat(createMockHttpServletRequest(tester -> tester.put().uri("/save")))
|
||||
.satisfies(hasSettings(HttpMethod.PUT, "/save"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void patchConfiguresBuilder() {
|
||||
assertThat(createMockHttpServletRequest(tester -> tester.patch().uri("/update")))
|
||||
.satisfies(hasSettings(HttpMethod.PATCH, "/update"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteConfiguresBuilder() {
|
||||
assertThat(createMockHttpServletRequest(tester -> tester.delete().uri("/users/42")))
|
||||
.satisfies(hasSettings(HttpMethod.DELETE, "/users/42"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionsConfiguresBuilder() {
|
||||
assertThat(createMockHttpServletRequest(tester -> tester.options().uri("/users")))
|
||||
.satisfies(hasSettings(HttpMethod.OPTIONS, "/users"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void methodConfiguresBuilderWithCustomMethod() {
|
||||
HttpMethod customMethod = HttpMethod.valueOf("CUSTOM");
|
||||
assertThat(createMockHttpServletRequest(tester -> tester.method(customMethod).uri("/hello")))
|
||||
.satisfies(hasSettings(customMethod, "/hello"));
|
||||
}
|
||||
|
||||
private MockHttpServletRequest createMockHttpServletRequest(Function<MockMvcTester, MockMvcRequestBuilder> builder) {
|
||||
MockMvcTester mockMvcTester = MockMvcTester.of(HelloController.class);
|
||||
return builder.apply(mockMvcTester).buildRequest(this.servletContext);
|
||||
}
|
||||
|
||||
private Consumer<MockHttpServletRequest> hasSettings(HttpMethod method, String uri) {
|
||||
return request -> {
|
||||
assertThat(request.getMethod()).isEqualTo(method.name());
|
||||
assertThat(request.getRequestURI()).isEqualTo(uri);
|
||||
};
|
||||
}
|
||||
|
||||
private GenericWebApplicationContext create(Class<?>... classes) {
|
||||
GenericWebApplicationContext applicationContext = new GenericWebApplicationContext(new MockServletContext());
|
||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(applicationContext);
|
||||
|
||||
Reference in New Issue
Block a user