Browse Source

Polishing

pull/32782/head
Sam Brannen 2 years ago
parent
commit
3ce79c3fbf
  1. 27
      spring-test/src/main/java/org/springframework/test/json/AbstractJsonContentAssert.java
  2. 2
      spring-test/src/main/java/org/springframework/test/json/AbstractJsonValueAssert.java
  3. 2
      spring-test/src/main/java/org/springframework/test/json/JsonContentAssert.java
  4. 2
      spring-test/src/main/java/org/springframework/test/json/JsonPathValueAssert.java
  5. 2
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractHttpServletRequestAssert.java
  6. 4
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractHttpServletResponseAssert.java
  7. 4
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletRequestAssert.java
  8. 10
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletResponseAssert.java
  9. 4
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/CookieMapAssert.java
  10. 10
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/DefaultMvcTestResult.java
  11. 12
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/HandlerResultAssert.java
  12. 40
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MockMvcTester.java
  13. 6
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/ModelAssert.java
  14. 20
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResult.java
  15. 38
      spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResultAssert.java
  16. 14
      spring-test/src/test/java/org/springframework/test/web/servlet/assertj/DefaultMvcTestResultTests.java
  17. 10
      spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterIntegrationTests.java

27
spring-test/src/main/java/org/springframework/test/json/AbstractJsonContentAssert.java

@ -44,17 +44,17 @@ import org.springframework.util.Assert; @@ -44,17 +44,17 @@ import org.springframework.util.Assert;
import org.springframework.util.function.ThrowingBiFunction;
/**
* Base AssertJ {@link org.assertj.core.api.Assert assertions} that can be
* Base AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be
* applied to a JSON document.
*
* <p>Support evaluating {@linkplain JsonPath JSON path} expressions and
* <p>Supports evaluating {@linkplain JsonPath JSON path} expressions and
* extracting a part of the document for further {@linkplain JsonPathValueAssert
* assertions} on the value.
*
* <p>Also support comparing the JSON document against a target, using
* <p>Also supports comparing the JSON document against a target, using
* {@linkplain JSONCompare JSON Assert}. Resources that are loaded from
* the classpath can be relative if a {@linkplain #withResourceLoadClass(Class)
* class} is provided. By default, {@code UTF-8} is used to load resources
* class} is provided. By default, {@code UTF-8} is used to load resources,
* but this can be overridden using {@link #withCharset(Charset)}.
*
* @author Stephane Nicoll
@ -85,7 +85,7 @@ public abstract class AbstractJsonContentAssert<SELF extends AbstractJsonContent @@ -85,7 +85,7 @@ public abstract class AbstractJsonContentAssert<SELF extends AbstractJsonContent
/**
* Create an assert for the given JSON document.
* <p>Path can be converted to a value object using the given
* {@linkplain GenericHttpMessageConverter json message converter}.
* {@linkplain GenericHttpMessageConverter JSON message converter}.
* @param json the JSON document to assert
* @param jsonMessageConverter the converter to use
* @param selfType the implementation type of this assert
@ -379,12 +379,13 @@ public abstract class AbstractJsonContentAssert<SELF extends AbstractJsonContent @@ -379,12 +379,13 @@ public abstract class AbstractJsonContentAssert<SELF extends AbstractJsonContent
}
/**
* Override the class used to load resources. Resources can be loaded from
* an absolute location or relative to tpe specified class. For instance,
* specifying {@code com.example.MyClass} as the resource class allows you
* to use "my-file.json" to load {@code /com/example/my-file.json}.
* @param resourceLoadClass the class used to load resources or {@code null}
* to only use absolute paths.
* Override the class used to load resources.
* <p>Resources can be loaded from an absolute location or relative to the
* specified class. For instance, specifying {@code com.example.MyClass} as
* the resource class allows you to use "my-file.json" to load
* {@code /com/example/my-file.json}.
* @param resourceLoadClass the class used to load resources, or {@code null}
* to only use absolute paths
*/
public SELF withResourceLoadClass(@Nullable Class<?> resourceLoadClass) {
this.resourceLoadClass = resourceLoadClass;
@ -393,8 +394,8 @@ public abstract class AbstractJsonContentAssert<SELF extends AbstractJsonContent @@ -393,8 +394,8 @@ public abstract class AbstractJsonContentAssert<SELF extends AbstractJsonContent
}
/**
* Override the {@link Charset} to use to load resources. By default,
* resources are loaded using {@code UTF-8}.
* Override the {@link Charset} to use to load resources.
* <p>By default, resources are loaded using {@code UTF-8}.
* @param charset the charset to use, or {@code null} to use the default
*/
public SELF withCharset(@Nullable Charset charset) {

2
spring-test/src/main/java/org/springframework/test/json/AbstractJsonValueAssert.java

@ -41,7 +41,7 @@ import org.springframework.util.ObjectUtils; @@ -41,7 +41,7 @@ import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Base AssertJ {@link org.assertj.core.api.Assert assertions} that can be
* Base AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be
* applied to a JSON value.
*
* <p>In JSON, values must be one of the following data types:

2
spring-test/src/main/java/org/springframework/test/json/JsonContentAssert.java

@ -30,7 +30,7 @@ public class JsonContentAssert extends AbstractJsonContentAssert<JsonContentAsse @@ -30,7 +30,7 @@ public class JsonContentAssert extends AbstractJsonContentAssert<JsonContentAsse
/**
* Create an assert for the given JSON document.
* <p>Path can be converted to a value object using the given
* {@linkplain GenericHttpMessageConverter json message converter}.
* {@linkplain GenericHttpMessageConverter JSON message converter}.
* @param json the JSON document to assert
* @param jsonMessageConverter the converter to use
*/

2
spring-test/src/main/java/org/springframework/test/json/JsonPathValueAssert.java

@ -22,7 +22,7 @@ import org.springframework.http.converter.GenericHttpMessageConverter; @@ -22,7 +22,7 @@ import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.lang.Nullable;
/**
* AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied
* AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be applied
* to a JSON value produced by evaluating a {@linkplain JsonPath JSON path}
* expression.
*

2
spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractHttpServletRequestAssert.java

@ -34,7 +34,7 @@ import org.springframework.util.function.SingletonSupplier; @@ -34,7 +34,7 @@ import org.springframework.util.function.SingletonSupplier;
import org.springframework.web.context.request.async.DeferredResult;
/**
* Base AssertJ {@link org.assertj.core.api.Assert assertions} that can be
* Base AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be
* applied to an {@link HttpServletRequest}.
*
* @author Stephane Nicoll

4
spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractHttpServletResponseAssert.java

@ -36,7 +36,7 @@ import org.springframework.util.MultiValueMap; @@ -36,7 +36,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.util.function.SingletonSupplier;
/**
* Base AssertJ {@link org.assertj.core.api.Assert assertions} that can be
* Base AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be
* applied to any object that provides an {@link HttpServletResponse}. This
* provides direct access to response assertions while also providing access to
* a different top-level object.
@ -91,7 +91,7 @@ public abstract class AbstractHttpServletResponseAssert<R extends HttpServletRes @@ -91,7 +91,7 @@ public abstract class AbstractHttpServletResponseAssert<R extends HttpServletRes
* {@link HttpHeaders} as the object to test. The returned assertion
* object provides all the regular {@linkplain AbstractMapAssert map
* assertions}, with headers mapped by header name.
* Examples: <pre><code class='java'>
* Examples: <pre><code class="java">
* // Check for the presence of the Accept header:
* assertThat(response).headers().containsHeader(HttpHeaders.ACCEPT);
*

4
spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletRequestAssert.java

@ -19,8 +19,8 @@ package org.springframework.test.web.servlet.assertj; @@ -19,8 +19,8 @@ package org.springframework.test.web.servlet.assertj;
import org.springframework.mock.web.MockHttpServletRequest;
/**
* AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied to
* {@link MockHttpServletRequest}.
* AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be applied
* to {@link MockHttpServletRequest}.
*
* @author Stephane Nicoll
* @since 6.2

10
spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletResponseAssert.java

@ -56,7 +56,7 @@ public abstract class AbstractMockHttpServletResponseAssert<SELF extends Abstrac @@ -56,7 +56,7 @@ public abstract class AbstractMockHttpServletResponseAssert<SELF extends Abstrac
/**
* Return a new {@linkplain AbstractStringAssert assertion} object that uses
* the response body converted to text as the object to test.
* <p>Examples: <pre><code class='java'>
* <p>Examples: <pre><code class="java">
* // Check that the response body is equal to "Hello World":
* assertThat(response).bodyText().isEqualTo("Hello World");
* </code></pre>
@ -70,7 +70,7 @@ public abstract class AbstractMockHttpServletResponseAssert<SELF extends Abstrac @@ -70,7 +70,7 @@ public abstract class AbstractMockHttpServletResponseAssert<SELF extends Abstrac
* uses the response body converted to text as the object to test. Compared
* to {@link #bodyText()}, the assertion object provides dedicated JSON
* support.
* <p>Examples: <pre><code class='java'>
* <p>Examples: <pre><code class="java">
* // Check that the response body is strictly equal to the content of
* // "/com/acme/sample/person-created.json":
* assertThat(response).bodyJson()
@ -82,7 +82,7 @@ public abstract class AbstractMockHttpServletResponseAssert<SELF extends Abstrac @@ -82,7 +82,7 @@ public abstract class AbstractMockHttpServletResponseAssert<SELF extends Abstrac
* .isStrictlyEqualToJson("person-created.json");
* </code></pre>
* The returned assert object also supports JSON path expressions.
* <p>Examples: <pre><code class='java'>
* <p>Examples: <pre><code class="java">
* // Check that the JSON document does not have an "error" element
* assertThat(response).bodyJson().doesNotHavePath("$.error");
*
@ -114,7 +114,7 @@ public abstract class AbstractMockHttpServletResponseAssert<SELF extends Abstrac @@ -114,7 +114,7 @@ public abstract class AbstractMockHttpServletResponseAssert<SELF extends Abstrac
* Return a new {@linkplain UriAssert assertion} object that uses the
* forwarded URL as the object to test. If a simple equality check is
* required, consider using {@link #hasForwardedUrl(String)} instead.
* <p>Example: <pre><code class='java'>
* <p>Example: <pre><code class="java">
* // Check that the forwarded URL starts with "/orders/":
* assertThat(response).forwardedUrl().matchPattern("/orders/*);
* </code></pre>
@ -127,7 +127,7 @@ public abstract class AbstractMockHttpServletResponseAssert<SELF extends Abstrac @@ -127,7 +127,7 @@ public abstract class AbstractMockHttpServletResponseAssert<SELF extends Abstrac
* Return a new {@linkplain UriAssert assertion} object that uses the
* redirected URL as the object to test. If a simple equality check is
* required, consider using {@link #hasRedirectedUrl(String)} instead.
* <p>Example: <pre><code class='java'>
* <p>Example: <pre><code class="java">
* // Check that the redirected URL starts with "/orders/":
* assertThat(response).redirectedUrl().matchPattern("/orders/*);
* </code></pre>

4
spring-test/src/main/java/org/springframework/test/web/servlet/assertj/CookieMapAssert.java

@ -26,8 +26,8 @@ import org.assertj.core.api.AbstractMapAssert; @@ -26,8 +26,8 @@ import org.assertj.core.api.AbstractMapAssert;
import org.assertj.core.api.Assertions;
/**
* AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied to
* {@link Cookie cookies}.
* AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be applied
* to {@link Cookie cookies}.
*
* @author Brian Clozel
* @author Stephane Nicoll

10
spring-test/src/main/java/org/springframework/test/web/servlet/assertj/DefaultMvcTestResult.java

@ -39,20 +39,26 @@ final class DefaultMvcTestResult implements MvcTestResult { @@ -39,20 +39,26 @@ final class DefaultMvcTestResult implements MvcTestResult {
@Nullable
private final GenericHttpMessageConverter<Object> jsonMessageConverter;
DefaultMvcTestResult(@Nullable MvcResult mvcResult, @Nullable Exception unresolvedException, @Nullable GenericHttpMessageConverter<Object> jsonMessageConverter) {
DefaultMvcTestResult(@Nullable MvcResult mvcResult, @Nullable Exception unresolvedException,
@Nullable GenericHttpMessageConverter<Object> jsonMessageConverter) {
this.mvcResult = mvcResult;
this.unresolvedException = unresolvedException;
this.jsonMessageConverter = jsonMessageConverter;
}
@Override
public MvcResult getMvcResult() {
if (this.mvcResult == null) {
throw new IllegalStateException(
"Request has failed with unresolved exception " + this.unresolvedException);
"Request failed with unresolved exception " + this.unresolvedException);
}
return this.mvcResult;
}
@Override
@Nullable
public Exception getUnresolvedException() {
return this.unresolvedException;

12
spring-test/src/main/java/org/springframework/test/web/servlet/assertj/HandlerResultAssert.java

@ -30,8 +30,8 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui @@ -30,8 +30,8 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.MethodInvocationInfo;
/**
* AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied to
* a handler or handler method.
* AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be applied
* to a handler or handler method.
* @author Stephane Nicoll
* @since 6.2
@ -48,9 +48,9 @@ public class HandlerResultAssert extends AbstractObjectAssert<HandlerResultAsser @@ -48,9 +48,9 @@ public class HandlerResultAssert extends AbstractObjectAssert<HandlerResultAsser
* the {@link Method} that handles the request as the object to test.
* <p>Verifies first that the handler is a {@linkplain #isMethodHandler()
* method handler}.
* <p>Example: <pre><code class='java'>
* <p>Example: <pre><code class="java">
* // Check that a GET to "/greet" is invoked on a "handleGreet" method name
* assertThat(mvc.perform(get("/greet")).handler().method().hasName("sayGreet");
* assertThat(mvc.perform(get("/greet")).handler().method().hasName("handleGreet");
* </code></pre>
*/
public MethodAssert method() {
@ -71,7 +71,7 @@ public class HandlerResultAssert extends AbstractObjectAssert<HandlerResultAsser @@ -71,7 +71,7 @@ public class HandlerResultAssert extends AbstractObjectAssert<HandlerResultAsser
* the method invocation in the {@code handlerMethod}. The arguments used by
* the target method invocation can be {@code null} as the purpose of the mock
* is to identify the method that was invoked.
* <p>Example: <pre><code class='java'>
* <p>Example: <pre><code class="java">
* // If the method has a return type, you can return the result of the invocation
* assertThat(mvc.perform(get("/greet")).handler().isInvokedOn(
* GreetController.class, controller -> controller.sayGreet());
@ -96,7 +96,7 @@ public class HandlerResultAssert extends AbstractObjectAssert<HandlerResultAsser @@ -96,7 +96,7 @@ public class HandlerResultAssert extends AbstractObjectAssert<HandlerResultAsser
/**
* Verify that the handler is of the given {@code type}. For a controller
* method, this is the type of the controller.
* <p>Example: <pre><code class='java'>
* <p>Example: <pre><code class="java">
* // Check that a GET to "/greet" is managed by GreetController
* assertThat(mvc.perform(get("/greet")).handler().hasType(GreetController.class);
* </code></pre>

40
spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MockMvcTester.java

@ -38,11 +38,12 @@ import org.springframework.util.Assert; @@ -38,11 +38,12 @@ import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
/**
* Test Spring MVC applications with {@link MockMvc} for server request handling
* using {@link org.assertj.core.api.Assertions AssertJ}.
* {@code MockMvcTester} provides support for testing Spring MVC applications
* with {@link MockMvc} for server request handling using
* {@linkplain org.assertj.core.api.Assertions AssertJ}.
*
* <p>A tester instance can be created from a {@link WebApplicationContext}:
* <pre><code class='java'>
* <pre><code class="java">
* // Create an instance with default settings
* MockMvcTester mvc = MockMvcTester.from(applicationContext);
*
@ -51,30 +52,31 @@ import org.springframework.web.context.WebApplicationContext; @@ -51,30 +52,31 @@ import org.springframework.web.context.WebApplicationContext;
* builder -> builder.addFilters(filter).build());
* </code></pre>
*
* <p>A tester can be created standalone by providing the controller(s) to
* include in a standalone setup:<pre><code class='java'>
* <p>A tester can be created in standalone mode by providing the controller(s)
* to include:<pre><code class="java">
* // Create an instance for PersonController
* MockMvcTester mvc = MockMvcTester.of(new PersonController());
* </code></pre>
*
* <p>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:
* <pre><code class='java'>
* <p>Once a tester 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:
* <pre><code class="java">
* // perform a GET on /hi and assert the response body is equal to Hello
* assertThat(mvc.perform(get("/hi")))
* .hasStatusOk().hasBodyTextEqualTo("Hello");
* </code></pre>
*
* <p>A main difference with {@link MockMvc} is that an 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:
* <pre><code class='java'>
* // perform a GET on /hi and assert the response body is equal to Hello
* <p>One main difference between {@link MockMvc} and {@code MockMvcTester} is
* that an unresolved exception is not thrown directly when using
* {@code MockMvcTester}. Rather an {@link MvcTestResult} is available with an
* {@linkplain MvcTestResult#getUnresolvedException() unresolved exception}
* which allows you to assert that a request failed unexpectedly:
* <pre><code class="java">
* // perform a GET on /boom and assert the message for the the unresolved exception
* assertThat(mvc.perform(get("/boom")))
* .hasUnresolvedException())
* .withMessage("Test exception");
* .withMessage("Test exception");
* </code></pre>
*
* <p>{@link MockMvcTester} can be configured with a list of
@ -120,7 +122,7 @@ public final class MockMvcTester { @@ -120,7 +122,7 @@ public final class MockMvcTester {
* @param applicationContext the application context to detect the Spring
* MVC infrastructure and application controllers from
* @param customizations a function that creates a {@link MockMvc}
* instance based on a {@link DefaultMockMvcBuilder}.
* instance based on a {@link DefaultMockMvcBuilder}
* @see MockMvcBuilders#webAppContextSetup(WebApplicationContext)
*/
public static MockMvcTester from(WebApplicationContext applicationContext,
@ -204,7 +206,7 @@ public final class MockMvcTester { @@ -204,7 +206,7 @@ public final class MockMvcTester {
* request, wrapping the invocation in {@code assertThat}. The following
* asserts that a {@linkplain MockMvcRequestBuilders#get(URI) GET} request
* against "/greet" has an HTTP status code 200 (OK) and a simple body:
* <pre><code class='java'>assertThat(mvc.perform(get("/greet")))
* <pre><code class="java">assertThat(mvc.perform(get("/greet")))
* .hasStatusOk()
* .body().asString().isEqualTo("Hello");
* </code></pre>
@ -215,7 +217,7 @@ public final class MockMvcTester { @@ -215,7 +217,7 @@ public final class MockMvcTester {
* {@code /boom} throws an {@code IllegalStateException}, the following
* asserts that the invocation has indeed failed with the expected error
* message:
* <pre><code class='java'>assertThat(mvc.perform(post("/boom")))
* <pre><code class="java">assertThat(mvc.perform(post("/boom")))
* .unresolvedException().isInstanceOf(IllegalStateException.class)
* .hasMessage("Expected");
* </code></pre>

6
spring-test/src/main/java/org/springframework/test/web/servlet/assertj/ModelAssert.java

@ -33,8 +33,8 @@ import org.springframework.validation.Errors; @@ -33,8 +33,8 @@ import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
/**
* AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied to
* a {@linkplain ModelAndView#getModel() model}.
* AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be applied
* to a {@linkplain ModelAndView#getModel() model}.
*
* @author Stephane Nicoll
* @since 6.2
@ -51,7 +51,7 @@ public class ModelAssert extends AbstractMapAssert<ModelAssert, Map<String, Obje @@ -51,7 +51,7 @@ public class ModelAssert extends AbstractMapAssert<ModelAssert, Map<String, Obje
* Return a new {@linkplain AbstractBindingResultAssert assertion} object
* that uses the {@link BindingResult} with the given {@code name} as the
* object to test.
* <p>Example: <pre><code class='java'>
* <p>Example: <pre><code class="java">
* // Check that the "person" attribute in the model has 2 errors:
* assertThat(...).model().extractingBindingResult("person").hasErrorsCount(2);
* </code></pre>

20
spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResult.java

@ -22,18 +22,18 @@ import org.springframework.lang.Nullable; @@ -22,18 +22,18 @@ import org.springframework.lang.Nullable;
import org.springframework.test.web.servlet.MvcResult;
/**
* Provide to the result of an executed request using {@link MockMvcTester} that
* Provides 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}.
*
* <p>Can be in one of two distinct states:
* <ol>
* <li>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}.</li>
* <li>The request failed unexpectedly with {@link #getUnresolvedException()}
* providing more information about the error. Any attempt to access
* {@link #getMvcResult() the result } fails with an exception.</li>
* <li>The request processed successfully, even if it failed with an exception
* that has been resolved. The {@linkplain #getMvcResult() result} is available,
* and {@link #getUnresolvedException()} will return {@code null}.</li>
* <li>The request failed unexpectedly. {@link #getUnresolvedException()}
* provides more information about the error, and any attempt to access the
* {@linkplain #getMvcResult() result} will fail with an exception.</li>
* </ol>
*
* @author Stephane Nicoll
@ -44,10 +44,10 @@ import org.springframework.test.web.servlet.MvcResult; @@ -44,10 +44,10 @@ import org.springframework.test.web.servlet.MvcResult;
public interface MvcTestResult extends AssertProvider<MvcTestResultAssert> {
/**
* Return the {@link MvcResult result} of the processing.
* <p>If the request has failed unexpectedly, this throws an
* {@link IllegalStateException}.
* Return the {@linkplain MvcResult result} of the processing.
* @return the {@link MvcResult}
* @throw {@link IllegalStateException} if the request failed unexpectedly
* @see #getUnresolvedException()
*/
MvcResult getMvcResult();

38
spring-test/src/main/java/org/springframework/test/web/servlet/assertj/MvcTestResultAssert.java

@ -40,7 +40,7 @@ import org.springframework.test.web.servlet.ResultMatcher; @@ -40,7 +40,7 @@ import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.web.servlet.ModelAndView;
/**
* AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied
* AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be applied
* to {@link MvcTestResult}.
*
* @author Stephane Nicoll
@ -60,7 +60,7 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M @@ -60,7 +60,7 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M
}
/**
* Verify that the request has failed with an unresolved exception, and
* Verify that the request failed with an unresolved exception, and
* return a new {@linkplain AbstractThrowableAssert assertion} object
* that uses the unresolved {@link Exception} as the object to test.
*/
@ -87,11 +87,11 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M @@ -87,11 +87,11 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M
/**
* Return a new {@linkplain HandlerResultAssert assertion} object that uses
* the handler as the object to test. For a method invocation on a
* controller, this is relative method handler
* <p>Example: <pre><code class='java'>
* the handler as the object to test.
* <p>For a method invocation on a controller, this is a relative method handler.
* <p>Example: <pre><code class="java">
* // Check that a GET to "/greet" is invoked on a "handleGreet" method name
* assertThat(mvc.perform(get("/greet")).handler().method().hasName("sayGreet");
* assertThat(mvc.perform(get("/greet")).handler().method().hasName("handleGreet");
* </code></pre>
*/
public HandlerResultAssert handler() {
@ -127,7 +127,7 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M @@ -127,7 +127,7 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M
}
/**
* Verify that an {@linkplain AbstractHttpServletRequestAssert#hasAsyncStarted(boolean)
* Verify that {@linkplain AbstractHttpServletRequestAssert#hasAsyncStarted(boolean)
* asynchronous processing has started} and return a new
* {@linkplain ObjectAssert assertion} object that uses the asynchronous
* result as the object to test.
@ -138,46 +138,46 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M @@ -138,46 +138,46 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M
}
/**
* Verify that the request has failed with an unresolved exception.
* Verify that the request failed with an unresolved exception.
* @see #unresolvedException()
*/
public MvcTestResultAssert hasUnresolvedException() {
Assertions.assertThat(this.actual.getUnresolvedException())
.withFailMessage("Expecting request to have failed but it has succeeded").isNotNull();
.withFailMessage("Expected request to fail, but it succeeded").isNotNull();
return this;
}
/**
* Verify that the request has not failed with an unresolved exception.
* Verify that the request did not fail with an unresolved exception.
*/
public MvcTestResultAssert doesNotHaveUnresolvedException() {
Assertions.assertThat(this.actual.getUnresolvedException())
.withFailMessage("Expecting request to have succeeded but it has failed").isNull();
.withFailMessage("Expected request to succeed, but it failed").isNull();
return this;
}
/**
* Verify that the actual mvc result matches the given {@link ResultMatcher}.
* Verify that the actual MVC result matches the given {@link ResultMatcher}.
* @param resultMatcher the result matcher to invoke
*/
public MvcTestResultAssert matches(ResultMatcher resultMatcher) {
MvcResult mvcResult = getMvcResult();
return super.satisfies(tmc -> resultMatcher.match(mvcResult));
return super.satisfies(mvcTestResult -> resultMatcher.match(mvcResult));
}
/**
* Apply the given {@link ResultHandler} to the actual mvc result.
* Apply the given {@link ResultHandler} to the actual MVC result.
* @param resultHandler the result matcher to invoke
*/
public MvcTestResultAssert apply(ResultHandler resultHandler) {
MvcResult mvcResult = getMvcResult();
return satisfies(tmc -> resultHandler.handle(mvcResult));
return satisfies(mvcTestResult -> resultHandler.handle(mvcResult));
}
/**
* Verify that a {@link ModelAndView} is available with a view equals to
* the given one. For more advanced assertions, consider using
* {@link #viewName()}
* Verify that a {@link ModelAndView} is available with a view name equal to
* the given one.
* <p>For more advanced assertions, consider using {@link #viewName()}.
* @param viewName the expected view name
*/
public MvcTestResultAssert hasViewName(String viewName) {
@ -212,7 +212,7 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M @@ -212,7 +212,7 @@ public class MvcTestResultAssert extends AbstractMockHttpServletResponseAssert<M
private static final class RequestFailedUnexpectedly extends BasicErrorMessageFactory {
private RequestFailedUnexpectedly(Exception ex) {
super("%nRequest has failed unexpectedly:%n%s", unquotedString(getIndentedStackTraceAsString(ex)));
super("%nRequest failed unexpectedly:%n%s", unquotedString(getIndentedStackTraceAsString(ex)));
}
private static String getIndentedStackTraceAsString(Throwable ex) {

14
spring-test/src/test/java/org/springframework/test/web/servlet/assertj/DefaultMvcTestResultTests.java

@ -48,18 +48,17 @@ class DefaultMvcTestResultTests { @@ -48,18 +48,17 @@ class DefaultMvcTestResultTests {
@Test
void createWithExceptionDoesNotAllowAccessToRequest() {
assertRequestHasFailed(DefaultMvcTestResult::getRequest);
assertRequestFailed(DefaultMvcTestResult::getRequest);
}
@Test
void createWithExceptionDoesNotAllowAccessToResponse() {
assertRequestHasFailed(DefaultMvcTestResult::getResponse);
assertRequestFailed(DefaultMvcTestResult::getResponse);
}
@Test
void createWithExceptionDoesNotAllowAccessToResolvedException() {
assertRequestHasFailed(DefaultMvcTestResult::getResolvedException);
assertRequestFailed(DefaultMvcTestResult::getResolvedException);
}
@Test
@ -69,10 +68,11 @@ class DefaultMvcTestResultTests { @@ -69,10 +68,11 @@ class DefaultMvcTestResultTests {
assertThat(result.getUnresolvedException()).isSameAs(exception);
}
private void assertRequestHasFailed(Consumer<DefaultMvcTestResult> action) {
private void assertRequestFailed(Consumer<DefaultMvcTestResult> action) {
DefaultMvcTestResult result = new DefaultMvcTestResult(null, new IllegalStateException("Expected"), null);
assertThatIllegalStateException().isThrownBy(() -> action.accept(result))
.withMessageContaining("Request has failed with unresolved exception");
assertThatIllegalStateException()
.isThrownBy(() -> action.accept(result))
.withMessageContaining("Request failed with unresolved exception");
}
}

10
spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterIntegrationTests.java

@ -300,14 +300,14 @@ public class MockMvcTesterIntegrationTests { @@ -300,14 +300,14 @@ public class MockMvcTesterIntegrationTests {
void doesNotHaveUnresolvedExceptionWithUnresolvedException() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(perform(get("/error/1"))).doesNotHaveUnresolvedException())
.withMessage("Expecting request to have succeeded but it has failed");
.withMessage("Expected request to succeed, but it failed");
}
@Test
void hasUnresolvedExceptionWithoutUnresolvedException() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(perform(get("/greet"))).hasUnresolvedException())
.withMessage("Expecting request to have failed but it has succeeded");
.withMessage("Expected request to fail, but it succeeded");
}
@Test
@ -321,10 +321,10 @@ public class MockMvcTesterIntegrationTests { @@ -321,10 +321,10 @@ public class MockMvcTesterIntegrationTests {
void unresolvedExceptionWithSuccessfulRequest() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(perform(get("/greet"))).unresolvedException())
.withMessage("Expecting request to have failed but it has succeeded");
.withMessage("Expected request to fail, but it succeeded");
}
// Check that assertions fail immediately if request has failed with unresolved exception
// Check that assertions fail immediately if request failed with unresolved exception
@Test
void assertAndApplyWithUnresolvedException() {
@ -409,7 +409,7 @@ public class MockMvcTesterIntegrationTests { @@ -409,7 +409,7 @@ public class MockMvcTesterIntegrationTests {
MvcTestResult result = perform(get("/error/1"));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.accept(result))
.withMessageContainingAll("Request has failed unexpectedly:",
.withMessageContainingAll("Request failed unexpectedly:",
ServletException.class.getName(), IllegalStateException.class.getName(),
"Expected");
}

Loading…
Cancel
Save