Introduce additional JsonPath RequestMatchers in Spring MVC Test
Commit fffdd1e9e9 introduced additional
JsonPath result matchers in JsonPathResultMatchers for server-side
testing of MVC controllers.
This commit introduces comparable methods in JsonPathRequestMatchers
for client-side testing with a RestTemplate.
- isString()
- isBoolean()
- isNumber()
- isMap()
Issue: SPR-13320
This commit is contained in:
@@ -115,6 +115,48 @@ public class JsonPathRequestMatchers {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the JSON path expression against the request content and
|
||||
* assert that the result is a {@link String}.
|
||||
* @since 4.2.1
|
||||
*/
|
||||
public RequestMatcher isString() {
|
||||
return new AbstractJsonPathRequestMatcher() {
|
||||
@Override
|
||||
public void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
|
||||
JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsString(request.getBodyAsString());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the JSON path expression against the request content and
|
||||
* assert that the result is a {@link Boolean}.
|
||||
* @since 4.2.1
|
||||
*/
|
||||
public RequestMatcher isBoolean() {
|
||||
return new AbstractJsonPathRequestMatcher() {
|
||||
@Override
|
||||
public void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
|
||||
JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsBoolean(request.getBodyAsString());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the JSON path expression against the request content and
|
||||
* assert that the result is a {@link Number}.
|
||||
* @since 4.2.1
|
||||
*/
|
||||
public RequestMatcher isNumber() {
|
||||
return new AbstractJsonPathRequestMatcher() {
|
||||
@Override
|
||||
public void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
|
||||
JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsNumber(request.getBodyAsString());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the JSON path expression against the request content and
|
||||
* assert that the result is an array.
|
||||
@@ -128,6 +170,20 @@ public class JsonPathRequestMatchers {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the JSON path expression against the request content and
|
||||
* assert that the result is a {@link java.util.Map}.
|
||||
* @since 4.2.1
|
||||
*/
|
||||
public RequestMatcher isMap() {
|
||||
return new AbstractJsonPathRequestMatcher() {
|
||||
@Override
|
||||
public void matchInternal(MockClientHttpRequest request) throws IOException, ParseException {
|
||||
JsonPathRequestMatchers.this.jsonPathHelper.assertValueIsMap(request.getBodyAsString());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Abstract base class for {@code JsonPath}-based {@link RequestMatcher}s.
|
||||
|
||||
Reference in New Issue
Block a user