Support target type in JsonPath assertions for MockMvc results
This commit picks up where SPR-14498 left off by adding support for an explicit target type when using JsonPath to perform an assertion against the response content using a Hamcrest Matcher. Specifically, there is a new overloaded value(Matcher<T>, Class<T>) method in JsonPathResultMatchers for use with Hamcrest matchers where the target type (i.e., Class<T>) can be specified. Issue: SPR-16587
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -282,21 +282,21 @@ public class JsonPathExpectationsHelper {
|
||||
|
||||
@Nullable
|
||||
private Object evaluateJsonPath(String content) {
|
||||
String message = "No value at JSON path \"" + this.expression + "\"";
|
||||
try {
|
||||
return this.jsonPath.read(content);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
String message = "No value at JSON path \"" + this.expression + "\"";
|
||||
throw new AssertionError(message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Object evaluateJsonPath(String content, Class<?> targetType) {
|
||||
String message = "No value at JSON path \"" + this.expression + "\"";
|
||||
try {
|
||||
return JsonPath.parse(content).read(this.expression, targetType);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
String message = "No value at JSON path \"" + this.expression + "\"";
|
||||
throw new AssertionError(message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,6 +30,7 @@ import org.springframework.test.web.client.RequestMatcher;
|
||||
/**
|
||||
* Factory for assertions on the request content using
|
||||
* <a href="https://github.com/jayway/JsonPath">JsonPath</a> expressions.
|
||||
*
|
||||
* <p>An instance of this class is typically accessed via
|
||||
* {@link MockRestRequestMatchers#jsonPath(String, Matcher)} or
|
||||
* {@link MockRestRequestMatchers#jsonPath(String, Object...)}.
|
||||
@@ -70,10 +71,11 @@ public class JsonPathRequestMatchers {
|
||||
}
|
||||
|
||||
/**
|
||||
* An overloaded variant of (@link {@link #value(Matcher)} that also
|
||||
* accepts a target type for the resulting value that the matcher can work
|
||||
* reliably against. This can be useful for matching numbers reliably for
|
||||
* example coercing an integer into a double.
|
||||
* An overloaded variant of {@link #value(Matcher)} that also accepts a
|
||||
* target type for the resulting value that the matcher can work reliably
|
||||
* against.
|
||||
* <p>This can be useful for matching numbers reliably — for example,
|
||||
* to coerce an integer into a double.
|
||||
* @since 4.3.3
|
||||
*/
|
||||
public <T> RequestMatcher value(final Matcher<T> matcher, final Class<T> targetType) {
|
||||
|
||||
@@ -199,7 +199,7 @@ public abstract class MockRestRequestMatchers {
|
||||
* @param expression the JSON path optionally parameterized with arguments
|
||||
* @param args arguments to parameterize the JSON path expression with
|
||||
*/
|
||||
public static JsonPathRequestMatchers jsonPath(String expression, Object ... args) {
|
||||
public static JsonPathRequestMatchers jsonPath(String expression, Object... args) {
|
||||
return new JsonPathRequestMatchers(expression, args);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -80,19 +80,40 @@ public class JsonPathResultMatchers {
|
||||
/**
|
||||
* Evaluate the JSON path expression against the response content and
|
||||
* assert the resulting value with the given Hamcrest {@link Matcher}.
|
||||
* @see #value(Matcher, Class)
|
||||
* @see #value(Object)
|
||||
*/
|
||||
public <T> ResultMatcher value(final Matcher<T> matcher) {
|
||||
public <T> ResultMatcher value(Matcher<T> matcher) {
|
||||
return result -> {
|
||||
String content = getContent(result);
|
||||
jsonPathHelper.assertValue(content, matcher);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* An overloaded variant of {@link #value(Matcher)} that also accepts a
|
||||
* target type for the resulting value that the matcher can work reliably
|
||||
* against.
|
||||
* <p>This can be useful for matching numbers reliably — for example,
|
||||
* to coerce an integer into a double.
|
||||
* @since 4.3.15
|
||||
* @see #value(Matcher)
|
||||
* @see #value(Object)
|
||||
*/
|
||||
public <T> ResultMatcher value(Matcher<T> matcher, Class<T> targetType) {
|
||||
return result -> {
|
||||
String content = getContent(result);
|
||||
jsonPathHelper.assertValue(content, matcher, targetType);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the JSON path expression against the response content and
|
||||
* assert that the result is equal to the supplied value.
|
||||
* @see #value(Matcher)
|
||||
* @see #value(Matcher, Class)
|
||||
*/
|
||||
public ResultMatcher value(final Object expectedValue) {
|
||||
public ResultMatcher value(Object expectedValue) {
|
||||
return result -> jsonPathHelper.assertValue(getContent(result), expectedValue);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -186,7 +186,7 @@ public abstract class MockMvcResultMatchers {
|
||||
* @param expression the JSON path expression, optionally parameterized with arguments
|
||||
* @param args arguments to parameterize the JSON path expression with
|
||||
*/
|
||||
public static JsonPathResultMatchers jsonPath(String expression, Object ... args) {
|
||||
public static JsonPathResultMatchers jsonPath(String expression, Object... args) {
|
||||
return new JsonPathResultMatchers(expression, args);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user