1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .test .web .servlet .result ;
18
18
19
+ import com .jayway .jsonpath .JsonPath ;
19
20
import org .hamcrest .Matcher ;
20
21
21
22
import org .springframework .test .util .JsonPathExpectationsHelper ;
22
23
import org .springframework .test .web .servlet .MvcResult ;
23
24
import org .springframework .test .web .servlet .ResultMatcher ;
24
25
25
26
/**
26
- * Factory for assertions on the response content using <a
27
- * href="http://goessner.net/articles/JsonPath/">JSONPath</a> expressions.
28
- * An instance of this class is typically accessed via
29
- * {@link MockMvcResultMatchers#jsonPath}.
27
+ * Factory for assertions on the response content using
28
+ * <a href="https://github.com/jayway/JsonPath">JsonPath</a> expressions.
29
+ * <p>An instance of this class is typically accessed via
30
+ * {@link MockMvcResultMatchers#jsonPath(String, Matcher)} or
31
+ * {@link MockMvcResultMatchers#jsonPath(String, Object...)}.
30
32
*
31
33
* @author Rossen Stoyanchev
32
34
* @since 3.2
33
35
*/
34
36
public class JsonPathResultMatchers {
35
37
36
- private JsonPathExpectationsHelper jsonPathHelper ;
38
+ private final JsonPathExpectationsHelper jsonPathHelper ;
39
+
37
40
38
41
/**
39
- * Protected constructor. Use
40
- * {@link MockMvcResultMatchers#jsonPath(String, Object...)} or
42
+ * Protected constructor.
43
+ * <p>Use {@link MockMvcResultMatchers#jsonPath(String, Object...)} or
41
44
* {@link MockMvcResultMatchers#jsonPath(String, Matcher)}.
45
+ * @param expression the {@link JsonPath} expression; never {@code null} or empty
46
+ * @param args arguments to parameterize the {@code JsonPath} expression with,
47
+ * using formatting specifiers defined in {@link String#format(String, Object...)}
42
48
*/
43
49
protected JsonPathResultMatchers (String expression , Object ... args ) {
44
50
this .jsonPathHelper = new JsonPathExpectationsHelper (expression , args );
45
51
}
46
52
53
+
47
54
/**
48
- * Evaluate the JSONPath and assert the value of the content found with the
49
- * given Hamcrest {@code Matcher}.
55
+ * Evaluate the JSON path expression against the response content and
56
+ * assert the resulting value with the given Hamcrest {@link Matcher}.
50
57
*/
51
58
public <T > ResultMatcher value (final Matcher <T > matcher ) {
52
59
return new ResultMatcher () {
@@ -59,7 +66,8 @@ public void match(MvcResult result) throws Exception {
59
66
}
60
67
61
68
/**
62
- * Evaluate the JSONPath and assert the value of the content found.
69
+ * Evaluate the JSON path expression against the response content and
70
+ * assert that the result is equal to the supplied value.
63
71
*/
64
72
public ResultMatcher value (final Object expectedValue ) {
65
73
return new ResultMatcher () {
@@ -71,7 +79,11 @@ public void match(MvcResult result) throws Exception {
71
79
}
72
80
73
81
/**
74
- * Evaluate the JSONPath and assert that content exists.
82
+ * Evaluate the JSON path expression against the response content and
83
+ * assert that a non-null value exists at the given path.
84
+ * <p>If the JSON path expression is not {@linkplain JsonPath#isDefinite
85
+ * definite}, this method asserts that the value at the given path is not
86
+ * <em>empty</em>.
75
87
*/
76
88
public ResultMatcher exists () {
77
89
return new ResultMatcher () {
@@ -84,7 +96,11 @@ public void match(MvcResult result) throws Exception {
84
96
}
85
97
86
98
/**
87
- * Evaluate the JSON path and assert not content was found.
99
+ * Evaluate the JSON path expression against the response content and
100
+ * assert that a value does not exist at the given path.
101
+ * <p>If the JSON path expression is not {@linkplain JsonPath#isDefinite
102
+ * definite}, this method asserts that the value at the given path is
103
+ * <em>empty</em>.
88
104
*/
89
105
public ResultMatcher doesNotExist () {
90
106
return new ResultMatcher () {
@@ -97,7 +113,8 @@ public void match(MvcResult result) throws Exception {
97
113
}
98
114
99
115
/**
100
- * Evluate the JSON path and assert the content found is an array.
116
+ * Evaluate the JSON path expression against the response content and
117
+ * assert that the result is an array.
101
118
*/
102
119
public ResultMatcher isArray () {
103
120
return new ResultMatcher () {
0 commit comments