From c8967de065603e6e515a08406fc9f46a82f7468f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Mon, 6 May 2024 17:21:30 +0200 Subject: [PATCH] Provide better name for entry points This commit renames AssertableMockMvc to MockMvcTester as the former was too mouthful, and we are looking for a naming pattern that can be applied consistently to test utilities that rely on AssertJ. Rather than AssertableMvcResult, we now use MvcTestResult and it no longer extends from MvcResult itself. That avoids having existing code that is migrated to the new API whilst assigning the result to MvcResult and get a bad DevXP as that doesn't bring any of the AssertJ support. See gh-32712 --- ...cResult.java => DefaultMvcTestResult.java} | 79 ++++------------ ...ertableMockMvc.java => MockMvcTester.java} | 89 +++++++++++++------ ...tableMvcResult.java => MvcTestResult.java} | 25 ++++-- ...ltAssert.java => MvcTestResultAssert.java} | 54 +++++------ ...ts.java => DefaultMvcTestResultTests.java} | 47 ++-------- ...ava => MockMvcTesterIntegrationTests.java} | 27 +++--- ...kMvcTests.java => MockMvcTesterTests.java} | 26 +++--- 7 files changed, 155 insertions(+), 192 deletions(-) rename spring-test/src/main/java/org/springframework/test/web/servlet/assertj/{DefaultAssertableMvcResult.java => DefaultMvcTestResult.java} (53%) rename spring-test/src/main/java/org/springframework/test/web/servlet/assertj/{AssertableMockMvc.java => MockMvcTester.java} (70%) rename spring-test/src/main/java/org/springframework/test/web/servlet/assertj/{AssertableMvcResult.java => MvcTestResult.java} (61%) rename spring-test/src/main/java/org/springframework/test/web/servlet/assertj/{MvcResultAssert.java => MvcTestResultAssert.java} (82%) rename spring-test/src/test/java/org/springframework/test/web/servlet/assertj/{DefaultAssertableMvcResultTests.java => DefaultMvcTestResultTests.java} (57%) rename spring-test/src/test/java/org/springframework/test/web/servlet/assertj/{AssertableMockMvcIntegrationTests.java => MockMvcTesterIntegrationTests.java} (94%) rename spring-test/src/test/java/org/springframework/test/web/servlet/assertj/{AssertableMockMvcTests.java => MockMvcTesterTests.java} (88%) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/DefaultAssertableMvcResult.java b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/DefaultMvcTestResult.java similarity index 53% rename from spring-test/src/main/java/org/springframework/test/web/servlet/assertj/DefaultAssertableMvcResult.java rename to spring-test/src/main/java/org/springframework/test/web/servlet/assertj/DefaultMvcTestResult.java index 83f4037c3a..8c71808069 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/DefaultAssertableMvcResult.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/DefaultMvcTestResult.java @@ -21,20 +21,17 @@ import org.springframework.lang.Nullable; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.MvcResult; -import org.springframework.web.servlet.FlashMap; -import org.springframework.web.servlet.HandlerInterceptor; -import org.springframework.web.servlet.ModelAndView; /** - * The default {@link AssertableMvcResult} implementation. + * The default {@link MvcTestResult} implementation. * * @author Stephane Nicoll * @since 6.2 */ -final class DefaultAssertableMvcResult implements AssertableMvcResult { +final class DefaultMvcTestResult implements MvcTestResult { @Nullable - private final MvcResult target; + private final MvcResult mvcResult; @Nullable private final Exception unresolvedException; @@ -42,86 +39,46 @@ final class DefaultAssertableMvcResult implements AssertableMvcResult { @Nullable private final GenericHttpMessageConverter jsonMessageConverter; - DefaultAssertableMvcResult(@Nullable MvcResult target, @Nullable Exception unresolvedException, @Nullable GenericHttpMessageConverter jsonMessageConverter) { - this.target = target; + DefaultMvcTestResult(@Nullable MvcResult mvcResult, @Nullable Exception unresolvedException, @Nullable GenericHttpMessageConverter jsonMessageConverter) { + this.mvcResult = mvcResult; this.unresolvedException = unresolvedException; this.jsonMessageConverter = jsonMessageConverter; } - /** - * Return the exception that was thrown unexpectedly while processing the - * request, if any. - */ + public MvcResult getMvcResult() { + if (this.mvcResult == null) { + throw new IllegalStateException( + "Request has failed with unresolved exception " + this.unresolvedException); + } + return this.mvcResult; + } + @Nullable public Exception getUnresolvedException() { return this.unresolvedException; } - @Override public MockHttpServletRequest getRequest() { - return getTarget().getRequest(); + return getMvcResult().getRequest(); } - @Override public MockHttpServletResponse getResponse() { - return getTarget().getResponse(); + return getMvcResult().getResponse(); } - @Override - @Nullable - public Object getHandler() { - return getTarget().getHandler(); - } - - @Override - @Nullable - public HandlerInterceptor[] getInterceptors() { - return getTarget().getInterceptors(); - } - - @Override - @Nullable - public ModelAndView getModelAndView() { - return getTarget().getModelAndView(); - } - - @Override @Nullable public Exception getResolvedException() { - return getTarget().getResolvedException(); + return getMvcResult().getResolvedException(); } - @Override - public FlashMap getFlashMap() { - return getTarget().getFlashMap(); - } - - @Override - public Object getAsyncResult() { - return getTarget().getAsyncResult(); - } - - @Override - public Object getAsyncResult(long timeToWait) { - return getTarget().getAsyncResult(timeToWait); - } - - - private MvcResult getTarget() { - if (this.target == null) { - throw new IllegalStateException( - "Request has failed with unresolved exception " + this.unresolvedException); - } - return this.target; - } /** * Use AssertJ's {@link org.assertj.core.api.Assertions#assertThat assertThat} * instead. */ @Override - public MvcResultAssert assertThat() { - return new MvcResultAssert(this, this.jsonMessageConverter); + public MvcTestResultAssert assertThat() { + return new MvcTestResultAssert(this, this.jsonMessageConverter); } } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AssertableMockMvc.java b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MockMvcTester.java similarity index 70% rename from spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AssertableMockMvc.java rename to spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MockMvcTester.java index 6e99119b9a..cca3087f60 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AssertableMockMvc.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MockMvcTester.java @@ -38,15 +38,46 @@ import org.springframework.util.Assert; import org.springframework.web.context.WebApplicationContext; /** - * {@link MockMvc} variant that tests Spring MVC exchanges and provides fluent - * assertions using {@link org.assertj.core.api.Assertions AssertJ}. + * Test Spring MVC applications with {@link MockMvc} for server request handling + * using {@link org.assertj.core.api.Assertions AssertJ}. + * + *

A tester instance can be created from a {@link WebApplicationContext}: + *


+ * // Create an instance with default settings
+ * MockMvcTester mvc = MockMvcTester.from(applicationContext);
+ *
+ * // Create an instance with a custom Filter
+ * MockMvcTester mvc = MockMvcTester.from(applicationContext,
+ *         builder -> builder.addFilters(filter).build());
+ * 
+ * + *

A tester can be created standalone by providing the controller(s) to + * include in a standalone setup:


+ * // Create an instance for PersonController
+ * MockMvcTester mvc = MockMvcTester.of(new PersonController());
+ * 
+ * + *

Once a test instance is available, you can perform requests in + * a similar fashion as with {@link MockMvc}, and wrapping the result in + * {@code assertThat} provides access to assertions. For instance: + *


+ * // perform a GET on /hi and assert the response body is equal to Hello
+ * assertThat(mvc.perform(get("/hi")))
+ *         .hasStatusOk().hasBodyTextEqualTo("Hello");
+ * 
* *

A main difference with {@link MockMvc} is that an unresolved exception - * is not thrown directly. Rather an {@link AssertableMvcResult} is available - * with an {@link AssertableMvcResult#getUnresolvedException() unresolved - * exception}. + * is not thrown directly. Rather an {@link MvcTestResult} is available + * with an {@link MvcTestResult#getUnresolvedException() unresolved + * exception}. You can assert that a request has failed unexpectedly: + *


+ * // perform a GET on /hi and assert the response body is equal to Hello
+ * assertThat(mvc.perform(get("/boom")))
+ *         .hasUnresolvedException())
+ * 		   .withMessage("Test exception");
+ * 
* - *

{@link AssertableMockMvc} can be configured with a list of + *

{@link MockMvcTester} can be configured with a list of * {@linkplain HttpMessageConverter message converters} to allow the response * body to be deserialized, rather than asserting on the raw values. * @@ -54,7 +85,7 @@ import org.springframework.web.context.WebApplicationContext; * @author Brian Clozel * @since 6.2 */ -public final class AssertableMockMvc { +public final class MockMvcTester { private static final MediaType JSON = MediaType.APPLICATION_JSON; @@ -64,23 +95,23 @@ public final class AssertableMockMvc { private final GenericHttpMessageConverter jsonMessageConverter; - private AssertableMockMvc(MockMvc mockMvc, @Nullable GenericHttpMessageConverter jsonMessageConverter) { + private MockMvcTester(MockMvc mockMvc, @Nullable GenericHttpMessageConverter jsonMessageConverter) { Assert.notNull(mockMvc, "mockMVC should not be null"); this.mockMvc = mockMvc; this.jsonMessageConverter = jsonMessageConverter; } /** - * Create a {@link AssertableMockMvc} instance that delegates to the given + * Create a {@link MockMvcTester} instance that delegates to the given * {@link MockMvc} instance. * @param mockMvc the MockMvc instance to delegate calls to */ - public static AssertableMockMvc create(MockMvc mockMvc) { - return new AssertableMockMvc(mockMvc, null); + public static MockMvcTester create(MockMvc mockMvc) { + return new MockMvcTester(mockMvc, null); } /** - * Create an {@link AssertableMockMvc} instance using the given, fully + * Create an {@link MockMvcTester} instance using the given, fully * initialized (i.e., refreshed) {@link WebApplicationContext}. The * given {@code customizations} are applied to the {@link DefaultMockMvcBuilder} * that ultimately creates the underlying {@link MockMvc} instance. @@ -92,7 +123,7 @@ public final class AssertableMockMvc { * instance based on a {@link DefaultMockMvcBuilder}. * @see MockMvcBuilders#webAppContextSetup(WebApplicationContext) */ - public static AssertableMockMvc from(WebApplicationContext applicationContext, + public static MockMvcTester from(WebApplicationContext applicationContext, Function customizations) { DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(applicationContext); @@ -101,7 +132,7 @@ public final class AssertableMockMvc { } /** - * Shortcut to create an {@link AssertableMockMvc} instance using the given, + * Shortcut to create an {@link MockMvcTester} instance using the given, * fully initialized (i.e., refreshed) {@link WebApplicationContext}. *

Consider using {@link #from(WebApplicationContext, Function)} if * further customization of the underlying {@link MockMvc} instance is @@ -110,12 +141,12 @@ public final class AssertableMockMvc { * MVC infrastructure and application controllers from * @see MockMvcBuilders#webAppContextSetup(WebApplicationContext) */ - public static AssertableMockMvc from(WebApplicationContext applicationContext) { + public static MockMvcTester from(WebApplicationContext applicationContext) { return from(applicationContext, DefaultMockMvcBuilder::build); } /** - * Create an {@link AssertableMockMvc} instance by registering one or more + * Create an {@link MockMvcTester} instance by registering one or more * {@code @Controller} instances and configuring Spring MVC infrastructure * programmatically. *

This allows full control over the instantiation and initialization of @@ -129,7 +160,7 @@ public final class AssertableMockMvc { * Spring MVC infrastructure * @see MockMvcBuilders#standaloneSetup(Object...) */ - public static AssertableMockMvc of(Collection controllers, + public static MockMvcTester of(Collection controllers, Function customizations) { StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(controllers.toArray()); @@ -137,8 +168,8 @@ public final class AssertableMockMvc { } /** - * Shortcut to create an {@link AssertableMockMvc} instance by registering - * one or more {@code @Controller} instances. + * Shortcut to create an {@link MockMvcTester} instance by registering one + * or more {@code @Controller} instances. *

The minimum infrastructure required by the * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet} * to serve requests with annotated controllers is created. Consider using @@ -149,12 +180,12 @@ public final class AssertableMockMvc { * into an instance * @see MockMvcBuilders#standaloneSetup(Object...) */ - public static AssertableMockMvc of(Object... controllers) { + public static MockMvcTester of(Object... controllers) { return of(Arrays.asList(controllers), StandaloneMockMvcBuilder::build); } /** - * Return a new {@link AssertableMockMvc} instance using the specified + * Return a new {@link MockMvcTester} instance using the specified * {@linkplain HttpMessageConverter message converters}. *

If none are specified, only basic assertions on the response body can * be performed. Consider registering a suitable JSON converter for asserting @@ -162,13 +193,13 @@ public final class AssertableMockMvc { * @param httpMessageConverters the message converters to use * @return a new instance using the specified converters */ - public AssertableMockMvc withHttpMessageConverters(Iterable> httpMessageConverters) { - return new AssertableMockMvc(this.mockMvc, findJsonMessageConverter(httpMessageConverters)); + public MockMvcTester withHttpMessageConverters(Iterable> httpMessageConverters) { + return new MockMvcTester(this.mockMvc, findJsonMessageConverter(httpMessageConverters)); } /** - * Perform a request and return a type that can be used with standard - * {@link org.assertj.core.api.Assertions AssertJ} assertions. + * Perform a request and return a {@link MvcTestResult result} that can be + * used with standard {@link org.assertj.core.api.Assertions AssertJ} assertions. *

Use static methods of {@link MockMvcRequestBuilders} to prepare the * request, wrapping the invocation in {@code assertThat}. The following * asserts that a {@linkplain MockMvcRequestBuilders#get(URI) GET} request @@ -191,16 +222,16 @@ public final class AssertableMockMvc { * @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 AssertableMvcResult} to be wrapped in {@code assertThat} + * @return an {@link MvcTestResult} to be wrapped in {@code assertThat} * @see MockMvc#perform(RequestBuilder) */ - public AssertableMvcResult perform(RequestBuilder requestBuilder) { + public MvcTestResult perform(RequestBuilder requestBuilder) { Object result = getMvcResultOrFailure(requestBuilder); if (result instanceof MvcResult mvcResult) { - return new DefaultAssertableMvcResult(mvcResult, null, this.jsonMessageConverter); + return new DefaultMvcTestResult(mvcResult, null, this.jsonMessageConverter); } else { - return new DefaultAssertableMvcResult(null, (Exception) result, this.jsonMessageConverter); + return new DefaultMvcTestResult(null, (Exception) result, this.jsonMessageConverter); } } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AssertableMvcResult.java b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResult.java similarity index 61% rename from spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AssertableMvcResult.java rename to spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResult.java index 8f0674db61..db8745bc17 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AssertableMvcResult.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResult.java @@ -22,23 +22,34 @@ import org.springframework.lang.Nullable; import org.springframework.test.web.servlet.MvcResult; /** - * A {@link MvcResult} that additionally supports AssertJ style assertions. + * Provide to the result of an executed request using {@link MockMvcTester} that + * is meant to be used with {@link org.assertj.core.api.Assertions#assertThat(AssertProvider) + * assertThat}. * *

Can be in one of two distinct states: *

    - *
  1. The request processed successfully, and {@link #getUnresolvedException()} - * is therefore {@code null}.
  2. + *
  3. The request processed successfully, even if it fails with an exception + * that has been resolved. {@link #getMvcResult()} is available and + * {@link #getUnresolvedException()} is {@code null}.
  4. *
  5. The request failed unexpectedly with {@link #getUnresolvedException()} - * providing more information about the error. Any attempt to access a member of - * the result fails with an exception.
  6. + * providing more information about the error. Any attempt to access + * {@link #getMvcResult() the result } fails with an exception. *
* * @author Stephane Nicoll * @author Brian Clozel * @since 6.2 - * @see AssertableMockMvc + * @see MockMvcTester */ -public interface AssertableMvcResult extends MvcResult, AssertProvider { +public interface MvcTestResult extends AssertProvider { + + /** + * Return the {@link MvcResult result} of the processing. + *

If the request has failed unexpectedly, this throws an + * {@link IllegalStateException}. + * @return the {@link MvcResult} + */ + MvcResult getMvcResult(); /** * Return the exception that was thrown unexpectedly while processing the diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcResultAssert.java b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResultAssert.java similarity index 82% rename from spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcResultAssert.java rename to spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResultAssert.java index e4e666546f..ca1dd65c4a 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcResultAssert.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResultAssert.java @@ -43,22 +43,22 @@ import org.springframework.web.servlet.ModelAndView; /** * AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied - * to {@link MvcResult}. + * to {@link MvcTestResult}. * * @author Stephane Nicoll * @author Brian Clozel * @since 6.2 */ -public class MvcResultAssert extends AbstractMockHttpServletResponseAssert { +public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert { - MvcResultAssert(AssertableMvcResult mvcResult, @Nullable GenericHttpMessageConverter jsonMessageConverter) { - super(jsonMessageConverter, mvcResult, MvcResultAssert.class); + MvcTestResultAssert(MvcTestResult actual, @Nullable GenericHttpMessageConverter jsonMessageConverter) { + super(jsonMessageConverter, actual, MvcTestResultAssert.class); } @Override protected MockHttpServletResponse getResponse() { - checkHasNotFailedUnexpectedly(); - return this.actual.getResponse(); + getMvcResult(); + return this.actual.getMvcResult().getResponse(); } /** @@ -76,8 +76,7 @@ public class MvcResultAssert extends AbstractMockHttpServletResponseAssert request() { - checkHasNotFailedUnexpectedly(); - return new MockHttpRequestAssert(this.actual.getRequest()); + return new MockHttpRequestAssert(getMvcResult().getRequest()); } /** @@ -85,8 +84,7 @@ public class MvcResultAssert extends AbstractMockHttpServletResponseAssert */ public HandlerResultAssert handler() { - checkHasNotFailedUnexpectedly(); - return new HandlerResultAssert(this.actual.getHandler()); + return new HandlerResultAssert(getMvcResult().getHandler()); } /** @@ -118,7 +114,6 @@ public class MvcResultAssert extends AbstractMockHttpServletResponseAssert viewName() { - checkHasNotFailedUnexpectedly(); return Assertions.assertThat(getModelAndView().getViewName()).as("View name"); } @@ -139,8 +133,7 @@ public class MvcResultAssert extends AbstractMockHttpServletResponseAssert flash() { - checkHasNotFailedUnexpectedly(); - return new MapAssert<>(this.actual.getFlashMap()); + return new MapAssert<>(getMvcResult().getFlashMap()); } /** @@ -151,14 +144,14 @@ public class MvcResultAssert extends AbstractMockHttpServletResponseAssert asyncResult() { request().hasAsyncStarted(true); - return Assertions.assertThat(this.actual.getAsyncResult()).as("Async result"); + return Assertions.assertThat(getMvcResult().getAsyncResult()).as("Async result"); } /** * Verify that the request has failed with an unresolved exception. * @see #unresolvedException() */ - public MvcResultAssert hasUnresolvedException() { + public MvcTestResultAssert hasUnresolvedException() { Assertions.assertThat(this.actual.getUnresolvedException()) .withFailMessage("Expecting request to have failed but it has succeeded").isNotNull(); return this; @@ -167,7 +160,7 @@ public class MvcResultAssert extends AbstractMockHttpServletResponseAssert resultMatcher.match(mvcResult)); } /** * Apply the given {@link ResultHandler} to the actual mvc result. * @param resultHandler the result matcher to invoke */ - public MvcResultAssert apply(ResultHandler resultHandler) { - checkHasNotFailedUnexpectedly(); - return satisfies(resultHandler::handle); + public MvcTestResultAssert apply(ResultHandler resultHandler) { + MvcResult mvcResult = getMvcResult(); + return satisfies(tmc -> resultHandler.handle(mvcResult)); } /** @@ -197,7 +190,7 @@ public class MvcResultAssert extends AbstractMockHttpServletResponseAssert { diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/DefaultAssertableMvcResultTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/DefaultMvcTestResultTests.java similarity index 57% rename from spring-test/src/test/java/org/springframework/test/web/servlet/assertj/DefaultAssertableMvcResultTests.java rename to spring-test/src/test/java/org/springframework/test/web/servlet/assertj/DefaultMvcTestResultTests.java index f59a53521d..e1e1867c89 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/DefaultAssertableMvcResultTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/DefaultMvcTestResultTests.java @@ -30,76 +30,47 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; /** - * Tests for {@link DefaultAssertableMvcResult}. + * Tests for {@link DefaultMvcTestResult}. * * @author Stephane Nicoll */ -class DefaultAssertableMvcResultTests { +class DefaultMvcTestResultTests { @Test void createWithMvcResultDelegatesToIt() { MockHttpServletRequest request = new MockHttpServletRequest(); MvcResult mvcResult = mock(MvcResult.class); given(mvcResult.getRequest()).willReturn(request); - DefaultAssertableMvcResult result = new DefaultAssertableMvcResult(mvcResult, null, null); + DefaultMvcTestResult result = new DefaultMvcTestResult(mvcResult, null, null); assertThat(result.getRequest()).isSameAs(request); verify(mvcResult).getRequest(); } @Test void createWithExceptionDoesNotAllowAccessToRequest() { - assertRequestHasFailed(DefaultAssertableMvcResult::getRequest); + assertRequestHasFailed(DefaultMvcTestResult::getRequest); } @Test void createWithExceptionDoesNotAllowAccessToResponse() { - assertRequestHasFailed(DefaultAssertableMvcResult::getResponse); + assertRequestHasFailed(DefaultMvcTestResult::getResponse); } - @Test - void createWithExceptionDoesNotAllowAccessToHandler() { - assertRequestHasFailed(DefaultAssertableMvcResult::getHandler); - } - - @Test - void createWithExceptionDoesNotAllowAccessToInterceptors() { - assertRequestHasFailed(DefaultAssertableMvcResult::getInterceptors); - } - - @Test - void createWithExceptionDoesNotAllowAccessToModelAndView() { - assertRequestHasFailed(DefaultAssertableMvcResult::getModelAndView); - } @Test void createWithExceptionDoesNotAllowAccessToResolvedException() { - assertRequestHasFailed(DefaultAssertableMvcResult::getResolvedException); - } - - @Test - void createWithExceptionDoesNotAllowAccessToFlashMap() { - assertRequestHasFailed(DefaultAssertableMvcResult::getFlashMap); - } - - @Test - void createWithExceptionDoesNotAllowAccessToAsyncResult() { - assertRequestHasFailed(DefaultAssertableMvcResult::getAsyncResult); - } - - @Test - void createWithExceptionDoesNotAllowAccessToAsyncResultWithTimeToWait() { - assertRequestHasFailed(result -> result.getAsyncResult(1000)); + assertRequestHasFailed(DefaultMvcTestResult::getResolvedException); } @Test void createWithExceptionReturnsException() { IllegalStateException exception = new IllegalStateException("Expected"); - DefaultAssertableMvcResult result = new DefaultAssertableMvcResult(null, exception, null); + DefaultMvcTestResult result = new DefaultMvcTestResult(null, exception, null); assertThat(result.getUnresolvedException()).isSameAs(exception); } - private void assertRequestHasFailed(Consumer action) { - DefaultAssertableMvcResult result = new DefaultAssertableMvcResult(null, new IllegalStateException("Expected"), null); + private void assertRequestHasFailed(Consumer action) { + DefaultMvcTestResult result = new DefaultMvcTestResult(null, new IllegalStateException("Expected"), null); assertThatIllegalStateException().isThrownBy(() -> action.accept(result)) .withMessageContaining("Request has failed with unresolved exception"); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/AssertableMockMvcIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterIntegrationTests.java similarity index 94% rename from spring-test/src/test/java/org/springframework/test/web/servlet/assertj/AssertableMockMvcIntegrationTests.java rename to spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterIntegrationTests.java index 34a164f887..4d58ad5c37 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/AssertableMockMvcIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterIntegrationTests.java @@ -43,7 +43,6 @@ import org.springframework.stereotype.Controller; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.Person; -import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.ui.Model; import org.springframework.validation.Errors; @@ -69,19 +68,19 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** - * Integration tests for {@link AssertableMockMvc}. + * Integration tests for {@link MockMvcTester}. * * @author Brian Clozel * @author Stephane Nicoll */ @SpringJUnitConfig @WebAppConfiguration -public class AssertableMockMvcIntegrationTests { +public class MockMvcTesterIntegrationTests { - private final AssertableMockMvc mockMvc; + private final MockMvcTester mockMvc; - AssertableMockMvcIntegrationTests(WebApplicationContext wac) { - this.mockMvc = AssertableMockMvc.from(wac); + MockMvcTesterIntegrationTests(WebApplicationContext wac) { + this.mockMvc = MockMvcTester.from(wac); } @Nested @@ -126,8 +125,8 @@ public class AssertableMockMvcIntegrationTests { assertThat(performWithCookie(cookie, get("/greet"))).cookies().hasValue("test", "value"); } - private AssertableMvcResult performWithCookie(Cookie cookie, MockHttpServletRequestBuilder request) { - AssertableMockMvc mockMvc = AssertableMockMvc.of(List.of(new TestController()), builder -> builder.addInterceptors( + private MvcTestResult performWithCookie(Cookie cookie, MockHttpServletRequestBuilder request) { + MockMvcTester mockMvc = MockMvcTester.of(List.of(new TestController()), builder -> builder.addInterceptors( new HandlerInterceptor() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { @@ -257,12 +256,12 @@ public class AssertableMockMvcIntegrationTests { @Test void jsonContentCanLoadResourceFromClasspath() { assertThat(perform(get("/message"))).bodyJson().isLenientlyEqualTo( - new ClassPathResource("message.json", AssertableMockMvcIntegrationTests.class)); + new ClassPathResource("message.json", MockMvcTesterIntegrationTests.class)); } @Test void jsonContentUsingResourceLoaderClass() { - assertThat(perform(get("/message"))).bodyJson().withResourceLoadClass(AssertableMockMvcIntegrationTests.class) + assertThat(perform(get("/message"))).bodyJson().withResourceLoadClass(MockMvcTesterIntegrationTests.class) .isLenientlyEqualTo("message.json"); } @@ -416,8 +415,8 @@ public class AssertableMockMvcIntegrationTests { } - private void testAssertionFailureWithUnresolvableException(Consumer assertions) { - AssertableMvcResult result = perform(get("/error/1")); + private void testAssertionFailureWithUnresolvableException(Consumer assertions) { + MvcTestResult result = perform(get("/error/1")); assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> assertions.accept(result)) .withMessageContainingAll("Request has failed unexpectedly:", @@ -441,7 +440,7 @@ public class AssertableMockMvcIntegrationTests { @Test void satisfiesAllowsAdditionalAssertions() { assertThat(this.mockMvc.perform(get("/greet"))).satisfies(result -> { - assertThat(result).isInstanceOf(MvcResult.class); + assertThat(result).isInstanceOf(MvcTestResult.class); assertThat(result).hasStatusOk(); }); } @@ -467,7 +466,7 @@ public class AssertableMockMvcIntegrationTests { } - private AssertableMvcResult perform(MockHttpServletRequestBuilder builder) { + private MvcTestResult perform(MockHttpServletRequestBuilder builder) { return this.mockMvc.perform(builder); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/AssertableMockMvcTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterTests.java similarity index 88% rename from spring-test/src/test/java/org/springframework/test/web/servlet/assertj/AssertableMockMvcTests.java rename to spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterTests.java index 587f0f521c..a8f17e6cf6 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/AssertableMockMvcTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterTests.java @@ -48,11 +48,11 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; /** - * Tests for {@link AssertableMockMvc}. + * Tests for {@link MockMvcTester}. * * @author Stephane Nicoll */ -class AssertableMockMvcTests { +class MockMvcTesterTests { private static final MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter(new ObjectMapper()); @@ -60,13 +60,13 @@ class AssertableMockMvcTests { @Test void createShouldRejectNullMockMvc() { - assertThatIllegalArgumentException().isThrownBy(() -> AssertableMockMvc.create(null)); + assertThatIllegalArgumentException().isThrownBy(() -> MockMvcTester.create(null)); } @Test void createWithExistingWebApplicationContext() { try (GenericWebApplicationContext wac = create(WebConfiguration.class)) { - AssertableMockMvc mockMvc = AssertableMockMvc.from(wac); + MockMvcTester mockMvc = MockMvcTester.from(wac); assertThat(mockMvc.perform(post("/increase"))).hasBodyTextEqualTo("counter 41"); assertThat(mockMvc.perform(post("/increase"))).hasBodyTextEqualTo("counter 42"); } @@ -74,7 +74,7 @@ class AssertableMockMvcTests { @Test void createWithControllerClassShouldInstantiateControllers() { - AssertableMockMvc mockMvc = AssertableMockMvc.of(HelloController.class, CounterController.class); + MockMvcTester mockMvc = MockMvcTester.of(HelloController.class, CounterController.class); assertThat(mockMvc.perform(get("/hello"))).hasBodyTextEqualTo("Hello World"); assertThat(mockMvc.perform(post("/increase"))).hasBodyTextEqualTo("counter 1"); assertThat(mockMvc.perform(post("/increase"))).hasBodyTextEqualTo("counter 2"); @@ -82,7 +82,7 @@ class AssertableMockMvcTests { @Test void createWithControllersShouldUseThemAsIs() { - AssertableMockMvc mockMvc = AssertableMockMvc.of(new HelloController(), + MockMvcTester mockMvc = MockMvcTester.of(new HelloController(), new CounterController(new AtomicInteger(41))); assertThat(mockMvc.perform(get("/hello"))).hasBodyTextEqualTo("Hello World"); assertThat(mockMvc.perform(post("/increase"))).hasBodyTextEqualTo("counter 42"); @@ -91,14 +91,14 @@ class AssertableMockMvcTests { @Test void createWithControllerAndCustomizations() { - AssertableMockMvc mockMvc = AssertableMockMvc.of(List.of(new HelloController()), builder -> + MockMvcTester mockMvc = MockMvcTester.of(List.of(new HelloController()), builder -> builder.defaultRequest(get("/hello").accept(MediaType.APPLICATION_JSON)).build()); assertThat(mockMvc.perform(get("/hello"))).hasStatus(HttpStatus.NOT_ACCEPTABLE); } @Test void createWithControllersHasNoHttpMessageConverter() { - AssertableMockMvc mockMvc = AssertableMockMvc.of(new HelloController()); + MockMvcTester mockMvc = MockMvcTester.of(new HelloController()); AbstractJsonContentAssert jsonContentAssert = assertThat(mockMvc.perform(get("/json"))).hasStatusOk().bodyJson(); assertThatIllegalStateException() .isThrownBy(() -> jsonContentAssert.extractingPath("$").convertTo(Message.class)) @@ -107,7 +107,7 @@ class AssertableMockMvcTests { @Test void createWithControllerCanConfigureHttpMessageConverters() { - AssertableMockMvc mockMvc = AssertableMockMvc.of(HelloController.class) + MockMvcTester mockMvc = MockMvcTester.of(HelloController.class) .withHttpMessageConverters(List.of(jsonHttpMessageConverter)); assertThat(mockMvc.perform(get("/json"))).hasStatusOk().bodyJson() .extractingPath("$").convertTo(Message.class).satisfies(message -> { @@ -120,7 +120,7 @@ class AssertableMockMvcTests { @SuppressWarnings("unchecked") void withHttpMessageConverterDetectsJsonConverter() { MappingJackson2HttpMessageConverter converter = spy(jsonHttpMessageConverter); - AssertableMockMvc mockMvc = AssertableMockMvc.of(HelloController.class) + MockMvcTester mockMvc = MockMvcTester.of(HelloController.class) .withHttpMessageConverters(List.of(mock(), mock(), converter)); assertThat(mockMvc.perform(get("/json"))).hasStatusOk().bodyJson() .extractingPath("$").convertTo(Message.class).satisfies(message -> { @@ -132,11 +132,11 @@ class AssertableMockMvcTests { @Test void performWithUnresolvedExceptionSetsException() { - AssertableMockMvc mockMvc = AssertableMockMvc.of(HelloController.class); - AssertableMvcResult result = mockMvc.perform(get("/error")); + MockMvcTester mockMvc = MockMvcTester.of(HelloController.class); + MvcTestResult result = mockMvc.perform(get("/error")); assertThat(result.getUnresolvedException()).isInstanceOf(ServletException.class) .cause().isInstanceOf(IllegalStateException.class).hasMessage("Expected"); - assertThat(result).hasFieldOrPropertyWithValue("target", null); + assertThat(result).hasFieldOrPropertyWithValue("mvcResult", null); } private GenericWebApplicationContext create(Class... classes) {