Polish
This commit is contained in:
parent
17abb4d25d
commit
1eaa9cd2f4
|
|
@ -61,8 +61,8 @@ import org.springframework.web.context.WebApplicationContext;
|
||||||
* builder -> builder.addFilters(filter).build());
|
* builder -> builder.addFilters(filter).build());
|
||||||
* </code></pre>
|
* </code></pre>
|
||||||
*
|
*
|
||||||
* <p>A tester can be created in standalone mode by providing the controller(s)
|
* <p>A tester can be created in standalone mode by providing the controller
|
||||||
* to include:<pre><code class="java">
|
* instances to include:<pre><code class="java">
|
||||||
* // Create an instance for PersonController
|
* // Create an instance for PersonController
|
||||||
* MockMvcTester mvc = MockMvcTester.of(new PersonController());
|
* MockMvcTester mvc = MockMvcTester.of(new PersonController());
|
||||||
* </code></pre>
|
* </code></pre>
|
||||||
|
|
@ -83,6 +83,16 @@ import org.springframework.web.context.WebApplicationContext;
|
||||||
* assertThat(result).body().isEmpty();
|
* assertThat(result).body().isEmpty();
|
||||||
* </code></pre>
|
* </code></pre>
|
||||||
*
|
*
|
||||||
|
* <p>If the request is processing asynchronously, {@code exchange} waits for
|
||||||
|
* its completion, either using the
|
||||||
|
* {@linkplain org.springframework.mock.web.MockAsyncContext#setTimeout default
|
||||||
|
* timeout} or a given one. If you prefer to get the result of an
|
||||||
|
* asynchronous request immediately, use {@code asyncExchange}:
|
||||||
|
* <pre><code class="java">
|
||||||
|
* // perform a POST on /save and assert an asynchronous request has started
|
||||||
|
* assertThat(mvc.post().uri("/save").asyncExchange()).request().hasAsyncStarted();
|
||||||
|
* </code></pre>
|
||||||
|
*
|
||||||
* <p>You can also perform requests using the static builders approach that
|
* <p>You can also perform requests using the static builders approach that
|
||||||
* {@link MockMvc} uses. For instance:<pre><code class="java">
|
* {@link MockMvc} uses. For instance:<pre><code class="java">
|
||||||
* // perform a GET on /hi and assert the response body is equal to Hello
|
* // perform a GET on /hi and assert the response body is equal to Hello
|
||||||
|
|
@ -90,6 +100,10 @@ import org.springframework.web.context.WebApplicationContext;
|
||||||
* .hasStatusOk().hasBodyTextEqualTo("Hello");
|
* .hasStatusOk().hasBodyTextEqualTo("Hello");
|
||||||
* </code></pre>
|
* </code></pre>
|
||||||
*
|
*
|
||||||
|
* <p>Use this approach if you have a custom {@link RequestBuilder} implementation
|
||||||
|
* that you'd like to integrate here. This approach is also invoking {@link MockMvc}
|
||||||
|
* without any additional processing of asynchronous requests.
|
||||||
|
*
|
||||||
* <p>One main difference between {@link MockMvc} and {@code MockMvcTester} is
|
* <p>One main difference between {@link MockMvc} and {@code MockMvcTester} is
|
||||||
* that an unresolved exception is not thrown directly when using
|
* that an unresolved exception is not thrown directly when using
|
||||||
* {@code MockMvcTester}. Rather an {@link MvcTestResult} is available with an
|
* {@code MockMvcTester}. Rather an {@link MvcTestResult} is available with an
|
||||||
|
|
@ -231,8 +245,10 @@ public final class MockMvcTester {
|
||||||
* Prepare an HTTP GET request.
|
* Prepare an HTTP GET request.
|
||||||
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
||||||
* assertions on the result. For multi-statements assertions, use
|
* assertions on the result. For multi-statements assertions, use
|
||||||
* {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
|
* {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
|
||||||
* result.
|
* result. To control the time to wait for asynchronous request to complete
|
||||||
|
* on a per-request basis, use
|
||||||
|
* {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
|
||||||
* @return a request builder for specifying the target URI
|
* @return a request builder for specifying the target URI
|
||||||
*/
|
*/
|
||||||
public MockMvcRequestBuilder get() {
|
public MockMvcRequestBuilder get() {
|
||||||
|
|
@ -243,8 +259,10 @@ public final class MockMvcTester {
|
||||||
* Prepare an HTTP HEAD request.
|
* Prepare an HTTP HEAD request.
|
||||||
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
||||||
* assertions on the result. For multi-statements assertions, use
|
* assertions on the result. For multi-statements assertions, use
|
||||||
* {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
|
* {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
|
||||||
* result.
|
* result. To control the time to wait for asynchronous request to complete
|
||||||
|
* on a per-request basis, use
|
||||||
|
* {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
|
||||||
* @return a request builder for specifying the target URI
|
* @return a request builder for specifying the target URI
|
||||||
*/
|
*/
|
||||||
public MockMvcRequestBuilder head() {
|
public MockMvcRequestBuilder head() {
|
||||||
|
|
@ -255,8 +273,10 @@ public final class MockMvcTester {
|
||||||
* Prepare an HTTP POST request.
|
* Prepare an HTTP POST request.
|
||||||
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
||||||
* assertions on the result. For multi-statements assertions, use
|
* assertions on the result. For multi-statements assertions, use
|
||||||
* {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
|
* {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
|
||||||
* result.
|
* result. To control the time to wait for asynchronous request to complete
|
||||||
|
* on a per-request basis, use
|
||||||
|
* {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
|
||||||
* @return a request builder for specifying the target URI
|
* @return a request builder for specifying the target URI
|
||||||
*/
|
*/
|
||||||
public MockMvcRequestBuilder post() {
|
public MockMvcRequestBuilder post() {
|
||||||
|
|
@ -267,8 +287,10 @@ public final class MockMvcTester {
|
||||||
* Prepare an HTTP PUT request.
|
* Prepare an HTTP PUT request.
|
||||||
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
||||||
* assertions on the result. For multi-statements assertions, use
|
* assertions on the result. For multi-statements assertions, use
|
||||||
* {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
|
* {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
|
||||||
* result.
|
* result. To control the time to wait for asynchronous request to complete
|
||||||
|
* on a per-request basis, use
|
||||||
|
* {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
|
||||||
* @return a request builder for specifying the target URI
|
* @return a request builder for specifying the target URI
|
||||||
*/
|
*/
|
||||||
public MockMvcRequestBuilder put() {
|
public MockMvcRequestBuilder put() {
|
||||||
|
|
@ -279,8 +301,10 @@ public final class MockMvcTester {
|
||||||
* Prepare an HTTP PATCH request.
|
* Prepare an HTTP PATCH request.
|
||||||
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
||||||
* assertions on the result. For multi-statements assertions, use
|
* assertions on the result. For multi-statements assertions, use
|
||||||
* {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
|
* {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
|
||||||
* result.
|
* result. To control the time to wait for asynchronous request to complete
|
||||||
|
* on a per-request basis, use
|
||||||
|
* {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
|
||||||
* @return a request builder for specifying the target URI
|
* @return a request builder for specifying the target URI
|
||||||
*/
|
*/
|
||||||
public MockMvcRequestBuilder patch() {
|
public MockMvcRequestBuilder patch() {
|
||||||
|
|
@ -291,8 +315,10 @@ public final class MockMvcTester {
|
||||||
* Prepare an HTTP DELETE request.
|
* Prepare an HTTP DELETE request.
|
||||||
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
||||||
* assertions on the result. For multi-statements assertions, use
|
* assertions on the result. For multi-statements assertions, use
|
||||||
* {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
|
* {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
|
||||||
* result.
|
* result. To control the time to wait for asynchronous request to complete
|
||||||
|
* on a per-request basis, use
|
||||||
|
* {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
|
||||||
* @return a request builder for specifying the target URI
|
* @return a request builder for specifying the target URI
|
||||||
*/
|
*/
|
||||||
public MockMvcRequestBuilder delete() {
|
public MockMvcRequestBuilder delete() {
|
||||||
|
|
@ -303,8 +329,10 @@ public final class MockMvcTester {
|
||||||
* Prepare an HTTP OPTIONS request.
|
* Prepare an HTTP OPTIONS request.
|
||||||
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
||||||
* assertions on the result. For multi-statements assertions, use
|
* assertions on the result. For multi-statements assertions, use
|
||||||
* {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
|
* {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
|
||||||
* result.
|
* result. To control the time to wait for asynchronous request to complete
|
||||||
|
* on a per-request basis, use
|
||||||
|
* {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
|
||||||
* @return a request builder for specifying the target URI
|
* @return a request builder for specifying the target URI
|
||||||
*/
|
*/
|
||||||
public MockMvcRequestBuilder options() {
|
public MockMvcRequestBuilder options() {
|
||||||
|
|
@ -315,8 +343,10 @@ public final class MockMvcTester {
|
||||||
* Prepare a request for the specified {@code HttpMethod}.
|
* Prepare a request for the specified {@code HttpMethod}.
|
||||||
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
* <p>The returned builder can be wrapped in {@code assertThat} to enable
|
||||||
* assertions on the result. For multi-statements assertions, use
|
* assertions on the result. For multi-statements assertions, use
|
||||||
* {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
|
* {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
|
||||||
* result.
|
* result. To control the time to wait for asynchronous request to complete
|
||||||
|
* on a per-request basis, use
|
||||||
|
* {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
|
||||||
* @return a request builder for specifying the target URI
|
* @return a request builder for specifying the target URI
|
||||||
*/
|
*/
|
||||||
public MockMvcRequestBuilder method(HttpMethod method) {
|
public MockMvcRequestBuilder method(HttpMethod method) {
|
||||||
|
|
@ -324,17 +354,13 @@ public final class MockMvcTester {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform a request using {@link MockMvcRequestBuilders} and return a
|
* Perform a request using the given {@link RequestBuilder} and return a
|
||||||
* {@link MvcTestResult result} that can be used with standard
|
* {@link MvcTestResult result} that can be used with standard
|
||||||
* {@link org.assertj.core.api.Assertions AssertJ} assertions.
|
* {@link org.assertj.core.api.Assertions AssertJ} assertions.
|
||||||
* <p>Use static methods of {@link MockMvcRequestBuilders} to prepare the
|
* <p>Use only this method if you need to provide a custom
|
||||||
* request, wrapping the invocation in {@code assertThat}. The following
|
* {@link RequestBuilder}. For regular cases, users should initiate the
|
||||||
* asserts that a {@linkplain MockMvcRequestBuilders#get(URI) GET} request
|
* configuration of the request using one of the methods available on
|
||||||
* against "/greet" has an HTTP status code 200 (OK) and a simple body:
|
* this instance, e.g. {@link #get()} for HTTP GET.
|
||||||
* <pre><code class="java">assertThat(mvc.perform(get("/greet")))
|
|
||||||
* .hasStatusOk()
|
|
||||||
* .body().asString().isEqualTo("Hello");
|
|
||||||
* </code></pre>
|
|
||||||
* <p>Contrary to {@link MockMvc#perform(RequestBuilder)}, this does not
|
* <p>Contrary to {@link MockMvc#perform(RequestBuilder)}, this does not
|
||||||
* throw an exception if the request fails with an unresolved exception.
|
* throw an exception if the request fails with an unresolved exception.
|
||||||
* Rather, the result provides the exception, if any. Assuming that a
|
* Rather, the result provides the exception, if any. Assuming that a
|
||||||
|
|
@ -342,17 +368,14 @@ public final class MockMvcTester {
|
||||||
* {@code /boom} throws an {@code IllegalStateException}, the following
|
* {@code /boom} throws an {@code IllegalStateException}, the following
|
||||||
* asserts that the invocation has indeed failed with the expected error
|
* asserts that the invocation has indeed failed with the expected error
|
||||||
* message:
|
* message:
|
||||||
* <pre><code class="java">assertThat(mvc.perform(post("/boom")))
|
* <pre><code class="java">assertThat(mvc.post().uri("/boom")))
|
||||||
* .unresolvedException().isInstanceOf(IllegalStateException.class)
|
* .failure().isInstanceOf(IllegalStateException.class)
|
||||||
* .hasMessage("Expected");
|
* .hasMessage("Expected");
|
||||||
* </code></pre>
|
* </code></pre>
|
||||||
* @param requestBuilder used to prepare the request to execute;
|
* @param requestBuilder used to prepare the request to execute
|
||||||
* see static factory methods in
|
|
||||||
* {@link org.springframework.test.web.servlet.request.MockMvcRequestBuilders}
|
|
||||||
* @return an {@link MvcTestResult} to be wrapped in {@code assertThat}
|
* @return an {@link MvcTestResult} to be wrapped in {@code assertThat}
|
||||||
* @see MockMvc#perform(RequestBuilder)
|
* @see MockMvc#perform(RequestBuilder)
|
||||||
* @see #get()
|
* @see #method(HttpMethod)
|
||||||
* @see #post()
|
|
||||||
*/
|
*/
|
||||||
public MvcTestResult perform(RequestBuilder requestBuilder) {
|
public MvcTestResult perform(RequestBuilder requestBuilder) {
|
||||||
Object result = getMvcResultOrFailure(requestBuilder);
|
Object result = getMvcResultOrFailure(requestBuilder);
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,10 @@ import static org.mockito.Mockito.verifyNoInteractions;
|
||||||
@SpringJUnitWebConfig
|
@SpringJUnitWebConfig
|
||||||
public class MockMvcTesterIntegrationTests {
|
public class MockMvcTesterIntegrationTests {
|
||||||
|
|
||||||
|
private static final MockMultipartFile file = new MockMultipartFile("file", "content.txt", null,
|
||||||
|
"value".getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
|
||||||
private final MockMvcTester mvc;
|
private final MockMvcTester mvc;
|
||||||
|
|
||||||
MockMvcTesterIntegrationTests(WebApplicationContext wac) {
|
MockMvcTesterIntegrationTests(WebApplicationContext wac) {
|
||||||
|
|
@ -106,9 +110,6 @@ public class MockMvcTesterIntegrationTests {
|
||||||
@Nested
|
@Nested
|
||||||
class PerformTests {
|
class PerformTests {
|
||||||
|
|
||||||
private final MockMultipartFile file = new MockMultipartFile("file", "content.txt", null,
|
|
||||||
"value".getBytes(StandardCharsets.UTF_8));
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void syncRequestWithDefaultExchange() {
|
void syncRequestWithDefaultExchange() {
|
||||||
assertThat(mvc.get().uri("/greet")).hasStatusOk();
|
assertThat(mvc.get().uri("/greet")).hasStatusOk();
|
||||||
|
|
@ -123,7 +124,7 @@ public class MockMvcTesterIntegrationTests {
|
||||||
@Test
|
@Test
|
||||||
void asyncMultipartRequestWithDefaultExchange() {
|
void asyncMultipartRequestWithDefaultExchange() {
|
||||||
assertThat(mvc.post().uri("/multipart-streaming").multipart()
|
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");
|
.hasStatusOk().hasBodyTextEqualTo("name=Joe&file=content.txt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -141,7 +142,7 @@ public class MockMvcTesterIntegrationTests {
|
||||||
@Test
|
@Test
|
||||||
void asyncMultipartRequestWitExplicitExchange() {
|
void asyncMultipartRequestWitExplicitExchange() {
|
||||||
assertThat(mvc.post().uri("/multipart-streaming").multipart()
|
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");
|
.hasStatusOk().hasBodyTextEqualTo("name=Joe&file=content.txt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -161,7 +162,7 @@ public class MockMvcTesterIntegrationTests {
|
||||||
@Test
|
@Test
|
||||||
void asyncMultipartRequestWithExplicitExchangeAndEnoughTimeToWait() {
|
void asyncMultipartRequestWithExplicitExchangeAndEnoughTimeToWait() {
|
||||||
assertThat(mvc.post().uri("/multipart-streaming").multipart()
|
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");
|
.hasStatusOk().hasBodyTextEqualTo("name=Joe&file=content.txt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -176,7 +177,7 @@ public class MockMvcTesterIntegrationTests {
|
||||||
@Test
|
@Test
|
||||||
void asyncMultipartRequestWithExplicitExchangeAndNotEnoughTimeToWait() {
|
void asyncMultipartRequestWithExplicitExchangeAndNotEnoughTimeToWait() {
|
||||||
MockMultipartMvcRequestBuilder builder = mvc.post().uri("/multipart-streaming").multipart()
|
MockMultipartMvcRequestBuilder builder = mvc.post().uri("/multipart-streaming").multipart()
|
||||||
.file(this.file).param("timeToWait", "500");
|
.file(file).param("timeToWait", "500");
|
||||||
assertThatIllegalStateException()
|
assertThatIllegalStateException()
|
||||||
.isThrownBy(() -> builder.exchange(Duration.ofMillis(100)))
|
.isThrownBy(() -> builder.exchange(Duration.ofMillis(100)))
|
||||||
.withMessageContaining("was not set during the specified timeToWait=100");
|
.withMessageContaining("was not set during the specified timeToWait=100");
|
||||||
|
|
@ -192,11 +193,24 @@ public class MockMvcTesterIntegrationTests {
|
||||||
.request().hasAsyncStarted(true);
|
.request().hasAsyncStarted(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasAsyncStartedForMultipartTrue() {
|
||||||
|
assertThat(mvc.post().uri("/multipart-streaming").multipart()
|
||||||
|
.file(file).param("timeToWait", "100").asyncExchange())
|
||||||
|
.request().hasAsyncStarted(true);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void hasAsyncStartedFalse() {
|
void hasAsyncStartedFalse() {
|
||||||
assertThat(mvc.get().uri("/greet").asyncExchange()).request().hasAsyncStarted(false);
|
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
|
@Test
|
||||||
void attributes() {
|
void attributes() {
|
||||||
assertThat(mvc.get().uri("/greet")).request().attributes()
|
assertThat(mvc.get().uri("/greet")).request().attributes()
|
||||||
|
|
@ -220,8 +234,7 @@ public class MockMvcTesterIntegrationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void multipartWithPut() {
|
void multipartWithPut() {
|
||||||
MockMultipartFile part = new MockMultipartFile("file", "content.txt", null, "value".getBytes(StandardCharsets.UTF_8));
|
assertThat(mvc.put().uri("/multipart-put").multipart().file(file).file(JSON_PART_FILE))
|
||||||
assertThat(mvc.put().uri("/multipart-put").multipart().file(part).file(JSON_PART_FILE))
|
|
||||||
.hasStatusOk()
|
.hasStatusOk()
|
||||||
.hasViewName("index")
|
.hasViewName("index")
|
||||||
.model().contains(entry("name", "file"));
|
.model().contains(entry("name", "file"));
|
||||||
|
|
|
||||||
|
|
@ -19,19 +19,25 @@ package org.springframework.test.web.servlet.assertj;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import jakarta.servlet.ServletContext;
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.cglib.core.internal.Function;
|
||||||
import org.springframework.context.annotation.AnnotationConfigUtils;
|
import org.springframework.context.annotation.AnnotationConfigUtils;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
import org.springframework.mock.web.MockServletContext;
|
import org.springframework.mock.web.MockServletContext;
|
||||||
import org.springframework.test.json.AbstractJsonContentAssert;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
@ -57,6 +63,8 @@ class MockMvcTesterTests {
|
||||||
private static final MappingJackson2HttpMessageConverter jsonHttpMessageConverter =
|
private static final MappingJackson2HttpMessageConverter jsonHttpMessageConverter =
|
||||||
new MappingJackson2HttpMessageConverter(new ObjectMapper());
|
new MappingJackson2HttpMessageConverter(new ObjectMapper());
|
||||||
|
|
||||||
|
private final ServletContext servletContext = new MockServletContext();
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createShouldRejectNullMockMvc() {
|
void createShouldRejectNullMockMvc() {
|
||||||
|
|
@ -139,6 +147,67 @@ class MockMvcTesterTests {
|
||||||
assertThat(result).hasFieldOrPropertyWithValue("mvcResult", null);
|
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) {
|
private GenericWebApplicationContext create(Class<?>... classes) {
|
||||||
GenericWebApplicationContext applicationContext = new GenericWebApplicationContext(new MockServletContext());
|
GenericWebApplicationContext applicationContext = new GenericWebApplicationContext(new MockServletContext());
|
||||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(applicationContext);
|
AnnotationConfigUtils.registerAnnotationConfigProcessors(applicationContext);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue