Support multiple matchers in MockMvc Kotlin DSL

Previous incarnation of MockMvc Kotlin DSL tried to reuse directly
Java APIs like ModelResultMatchers or StatusResultMatchers, but
when using multiple matchers in DSL blocks like model { } or
status { }, only the last statement was taken in account which
was very confusing.

This refactoring provides dedicated Kotlin DSLs for matchers.

The main API breaking changes is that functions like isOk() need to be
invoked with the parenthesis, isOk is not supported anymore (on purpose).

Closes gh-24103
This commit is contained in:
Sébastien Deleuze
2020-10-26 18:13:39 +01:00
parent 41247d49ba
commit d04c5f8b2c
16 changed files with 1483 additions and 35 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.test.web.servlet.result;
import org.hamcrest.Matcher;
import org.springframework.lang.Nullable;
import org.springframework.test.web.servlet.ResultMatcher;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -54,7 +55,7 @@ public class FlashAttributeResultMatchers {
/**
* Assert a flash attribute's value.
*/
public ResultMatcher attribute(String name, Object value) {
public ResultMatcher attribute(String name, @Nullable Object value) {
return result -> assertEquals("Flash attribute '" + name + "'", value, result.getFlashMap().get(name));
}

View File

@@ -107,7 +107,7 @@ public class JsonPathResultMatchers {
* @see #value(Matcher)
* @see #value(Matcher, Class)
*/
public ResultMatcher value(Object expectedValue) {
public ResultMatcher value(@Nullable Object expectedValue) {
return result -> this.jsonPathHelper.assertValue(getContent(result), expectedValue);
}

View File

@@ -18,6 +18,7 @@ package org.springframework.test.web.servlet.result;
import org.hamcrest.Matcher;
import org.springframework.lang.Nullable;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.ui.ModelMap;
@@ -67,7 +68,7 @@ public class ModelResultMatchers {
/**
* Assert a model attribute value.
*/
public ResultMatcher attribute(String name, Object value) {
public ResultMatcher attribute(String name, @Nullable Object value) {
return result -> {
ModelAndView mav = getModelAndView(result);
assertEquals("Model attribute '" + name + "'", value, mav.getModel().get(name));

View File

@@ -23,6 +23,7 @@ import javax.servlet.http.HttpSession;
import org.hamcrest.Matcher;
import org.springframework.lang.Nullable;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.util.Assert;
@@ -98,7 +99,7 @@ public class RequestResultMatchers {
* or {@link WebAsyncTask}. The value matched is the value returned from the
* {@code Callable} or the exception raised.
*/
public ResultMatcher asyncResult(Object expectedResult) {
public ResultMatcher asyncResult(@Nullable Object expectedResult) {
return result -> {
HttpServletRequest request = result.getRequest();
assertAsyncStarted(request);
@@ -120,7 +121,7 @@ public class RequestResultMatchers {
/**
* Assert a request attribute value.
*/
public ResultMatcher attribute(String name, Object expectedValue) {
public ResultMatcher attribute(String name, @Nullable Object expectedValue) {
return result ->
assertEquals("Request attribute '" + name + "'", expectedValue, result.getRequest().getAttribute(name));
}
@@ -141,7 +142,7 @@ public class RequestResultMatchers {
/**
* Assert a session attribute value.
*/
public ResultMatcher sessionAttribute(String name, Object value) {
public ResultMatcher sessionAttribute(String name, @Nullable Object value) {
return result -> {
HttpSession session = result.getRequest().getSession();
Assert.state(session != null, "No HttpSession");