Skip to content

Commit 5f4d5f1

Browse files
committed
Polishing
1 parent 2c2ce55 commit 5f4d5f1

File tree

2 files changed

+14
-55
lines changed

2 files changed

+14
-55
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/result/JsonPathResultMatchers.java

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ public JsonPathResultMatchers prefix(String prefix) {
8484
* @see #value(Object)
8585
*/
8686
public <T> ResultMatcher value(Matcher<T> matcher) {
87-
return result -> {
88-
String content = getContent(result);
89-
jsonPathHelper.assertValue(content, matcher);
90-
};
87+
return result -> jsonPathHelper.assertValue(getContent(result), matcher);
9188
}
9289

9390
/**
@@ -101,10 +98,7 @@ public <T> ResultMatcher value(Matcher<T> matcher) {
10198
* @see #value(Object)
10299
*/
103100
public <T> ResultMatcher value(Matcher<T> matcher, Class<T> targetType) {
104-
return result -> {
105-
String content = getContent(result);
106-
jsonPathHelper.assertValue(content, matcher, targetType);
107-
};
101+
return result -> jsonPathHelper.assertValue(getContent(result), matcher, targetType);
108102
}
109103

110104
/**
@@ -126,10 +120,7 @@ public ResultMatcher value(Object expectedValue) {
126120
* <em>empty</em>.
127121
*/
128122
public ResultMatcher exists() {
129-
return result -> {
130-
String content = getContent(result);
131-
jsonPathHelper.exists(content);
132-
};
123+
return result -> jsonPathHelper.exists(getContent(result));
133124
}
134125

135126
/**
@@ -140,10 +131,7 @@ public ResultMatcher exists() {
140131
* <em>empty</em>.
141132
*/
142133
public ResultMatcher doesNotExist() {
143-
return result -> {
144-
String content = getContent(result);
145-
jsonPathHelper.doesNotExist(content);
146-
};
134+
return result -> jsonPathHelper.doesNotExist(getContent(result));
147135
}
148136

149137
/**
@@ -157,10 +145,7 @@ public ResultMatcher doesNotExist() {
157145
* @see #doesNotExist()
158146
*/
159147
public ResultMatcher isEmpty() {
160-
return result -> {
161-
String content = getContent(result);
162-
jsonPathHelper.assertValueIsEmpty(content);
163-
};
148+
return result -> jsonPathHelper.assertValueIsEmpty(getContent(result));
164149
}
165150

166151
/**
@@ -174,10 +159,7 @@ public ResultMatcher isEmpty() {
174159
* @see #doesNotExist()
175160
*/
176161
public ResultMatcher isNotEmpty() {
177-
return result -> {
178-
String content = getContent(result);
179-
jsonPathHelper.assertValueIsNotEmpty(content);
180-
};
162+
return result -> jsonPathHelper.assertValueIsNotEmpty(getContent(result));
181163
}
182164

183165
/**
@@ -191,10 +173,7 @@ public ResultMatcher isNotEmpty() {
191173
* @see #isNotEmpty()
192174
*/
193175
public ResultMatcher hasJsonPath() {
194-
return result -> {
195-
String content = getContent(result);
196-
jsonPathHelper.hasJsonPath(content);
197-
};
176+
return result -> jsonPathHelper.hasJsonPath(getContent(result));
198177
}
199178

200179
/**
@@ -209,10 +188,7 @@ public ResultMatcher hasJsonPath() {
209188
* @see #isEmpty()
210189
*/
211190
public ResultMatcher doesNotHaveJsonPath() {
212-
return result -> {
213-
String content = getContent(result);
214-
jsonPathHelper.doesNotHaveJsonPath(content);
215-
};
191+
return result -> jsonPathHelper.doesNotHaveJsonPath(getContent(result));
216192
}
217193

218194
/**
@@ -221,10 +197,7 @@ public ResultMatcher doesNotHaveJsonPath() {
221197
* @since 4.2.1
222198
*/
223199
public ResultMatcher isString() {
224-
return result -> {
225-
String content = getContent(result);
226-
jsonPathHelper.assertValueIsString(content);
227-
};
200+
return result -> jsonPathHelper.assertValueIsString(getContent(result));
228201
}
229202

230203
/**
@@ -233,10 +206,7 @@ public ResultMatcher isString() {
233206
* @since 4.2.1
234207
*/
235208
public ResultMatcher isBoolean() {
236-
return result -> {
237-
String content = getContent(result);
238-
jsonPathHelper.assertValueIsBoolean(content);
239-
};
209+
return result -> jsonPathHelper.assertValueIsBoolean(getContent(result));
240210
}
241211

242212
/**
@@ -245,21 +215,15 @@ public ResultMatcher isBoolean() {
245215
* @since 4.2.1
246216
*/
247217
public ResultMatcher isNumber() {
248-
return result -> {
249-
String content = getContent(result);
250-
jsonPathHelper.assertValueIsNumber(content);
251-
};
218+
return result -> jsonPathHelper.assertValueIsNumber(getContent(result));
252219
}
253220

254221
/**
255222
* Evaluate the JSON path expression against the response content and
256223
* assert that the result is an array.
257224
*/
258225
public ResultMatcher isArray() {
259-
return result -> {
260-
String content = getContent(result);
261-
jsonPathHelper.assertValueIsArray(content);
262-
};
226+
return result -> jsonPathHelper.assertValueIsArray(getContent(result));
263227
}
264228

265229
/**
@@ -268,10 +232,7 @@ public ResultMatcher isArray() {
268232
* @since 4.2.1
269233
*/
270234
public ResultMatcher isMap() {
271-
return result -> {
272-
String content = getContent(result);
273-
jsonPathHelper.assertValueIsMap(content);
274-
};
235+
return result -> jsonPathHelper.assertValueIsMap(getContent(result));
275236
}
276237

277238
private String getContent(MvcResult result) throws UnsupportedEncodingException {

spring-test/src/test/java/org/springframework/test/web/client/match/JsonPathRequestMatchersTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import java.io.IOException;
2020

21-
import org.hamcrest.Matchers;
22-
2321
import org.junit.Test;
2422

2523
import org.springframework.mock.http.client.MockClientHttpRequest;
@@ -79,7 +77,7 @@ public void valueWithMatcher() throws Exception {
7977

8078
@Test // SPR-14498
8179
public void valueWithMatcherAndNumberConversion() throws Exception {
82-
new JsonPathRequestMatchers("$.num").value(Matchers.equalTo(5.0f), Float.class).match(request);
80+
new JsonPathRequestMatchers("$.num").value(equalTo(5.0f), Float.class).match(request);
8381
}
8482

8583
@Test(expected = AssertionError.class)

0 commit comments

Comments
 (0)