Skip to content

Commit b4c6300

Browse files
committed
Remove Hamcrest use from RestTestClient
Closes gh-35702
1 parent 92a186b commit b4c6300

24 files changed

+288
-201
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/CookieAssertions.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616

1717
package org.springframework.test.web.reactive.server;
1818

19+
import org.hamcrest.Matcher;
20+
1921
import org.springframework.http.ResponseCookie;
2022
import org.springframework.test.web.support.AbstractCookieAssertions;
2123
import org.springframework.util.MultiValueMap;
2224

25+
import static org.hamcrest.MatcherAssert.assertThat;
26+
2327
/**
2428
* Assertions on cookies of the response.
2529
*
@@ -45,4 +49,56 @@ protected void assertWithDiagnostics(Runnable assertion) {
4549
getExchangeResult().assertWithDiagnostics(assertion);
4650
}
4751

52+
53+
/**
54+
* Assert the value of the response cookie with the given name with a Hamcrest
55+
* {@link Matcher}.
56+
*/
57+
public WebTestClient.ResponseSpec value(String name, Matcher<? super String> matcher) {
58+
String value = getCookie(name).getValue();
59+
assertWithDiagnostics(() -> {
60+
String message = getMessage(name);
61+
assertThat(message, value, matcher);
62+
});
63+
return getResponseSpec();
64+
}
65+
66+
67+
/**
68+
* Assert a cookie's "Max-Age" attribute with a Hamcrest {@link Matcher}.
69+
*/
70+
public WebTestClient.ResponseSpec maxAge(String name, Matcher<? super Long> matcher) {
71+
long maxAge = getCookie(name).getMaxAge().getSeconds();
72+
assertWithDiagnostics(() -> {
73+
String message = getMessage(name) + " maxAge";
74+
assertThat(message, maxAge, matcher);
75+
});
76+
return getResponseSpec();
77+
}
78+
79+
/**
80+
* Assert a cookie's "Path" attribute with a Hamcrest {@link Matcher}.
81+
*/
82+
public WebTestClient.ResponseSpec path(String name, Matcher<? super String> matcher) {
83+
String path = getCookie(name).getPath();
84+
assertWithDiagnostics(() -> {
85+
String message = getMessage(name) + " path";
86+
assertThat(message, path, matcher);
87+
});
88+
return getResponseSpec();
89+
}
90+
91+
/**
92+
* Assert a cookie's "Domain" attribute with a Hamcrest {@link Matcher}.
93+
*/
94+
public WebTestClient.ResponseSpec domain(String name, Matcher<? super String> matcher) {
95+
String domain = getCookie(name).getDomain();
96+
assertWithDiagnostics(() -> {
97+
String message = getMessage(name) + " domain";
98+
assertThat(message, domain, matcher);
99+
});
100+
return getResponseSpec();
101+
}
102+
103+
48104
}

spring-test/src/main/java/org/springframework/test/web/reactive/server/HeaderAssertions.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@
1616

1717
package org.springframework.test.web.reactive.server;
1818

19+
import java.util.List;
20+
21+
import org.hamcrest.Matcher;
22+
1923
import org.springframework.http.HttpHeaders;
2024
import org.springframework.test.web.support.AbstractHeaderAssertions;
2125

26+
import static org.hamcrest.MatcherAssert.assertThat;
27+
2228
/**
2329
* Assertions on headers of the response.
2430
*
@@ -47,4 +53,35 @@ protected void assertWithDiagnostics(Runnable assertion) {
4753
getExchangeResult().assertWithDiagnostics(assertion);
4854
}
4955

56+
57+
58+
/**
59+
* Assert the first value of the response header with a Hamcrest {@link Matcher}.
60+
* @param name the header name
61+
* @param matcher the matcher to use
62+
*/
63+
public WebTestClient.ResponseSpec value(String name, Matcher<? super String> matcher) {
64+
String value = getResponseHeaders().getFirst(name);
65+
assertWithDiagnostics(() -> {
66+
String message = getMessage(name);
67+
assertThat(message, value, matcher);
68+
});
69+
return getResponseSpec();
70+
}
71+
72+
/**
73+
* Assert all values of the response header with a Hamcrest {@link Matcher}.
74+
* @param name the header name
75+
* @param matcher the matcher to use
76+
*/
77+
public WebTestClient.ResponseSpec values(String name, Matcher<? super Iterable<String>> matcher) {
78+
List<String> values = getResponseHeaders().get(name);
79+
assertWithDiagnostics(() -> {
80+
String message = getMessage(name);
81+
assertThat(message, values, matcher);
82+
});
83+
return getResponseSpec();
84+
}
85+
86+
5087
}

spring-test/src/main/java/org/springframework/test/web/reactive/server/JsonPathAssertions.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package org.springframework.test.web.reactive.server;
1818

1919
import com.jayway.jsonpath.Configuration;
20+
import org.hamcrest.Matcher;
2021
import org.jspecify.annotations.Nullable;
2122

23+
import org.springframework.core.ParameterizedTypeReference;
2224
import org.springframework.test.util.JsonPathExpectationsHelper;
2325
import org.springframework.test.web.support.AbstractJsonPathAssertions;
2426

@@ -41,4 +43,31 @@ public class JsonPathAssertions extends AbstractJsonPathAssertions<WebTestClient
4143

4244
super(spec, content, expression, configuration);
4345
}
46+
47+
48+
49+
/**
50+
* Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher)}.
51+
*/
52+
public <T> WebTestClient.BodyContentSpec value(Matcher<? super T> matcher) {
53+
getPathHelper().assertValue(getContent(), matcher);
54+
return getBodySpec();
55+
}
56+
57+
/**
58+
* Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher, Class)}.
59+
*/
60+
public <T> WebTestClient.BodyContentSpec value(Class<T> targetType, Matcher<? super T> matcher) {
61+
getPathHelper().assertValue(getContent(), matcher, targetType);
62+
return getBodySpec();
63+
}
64+
65+
/**
66+
* Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher, ParameterizedTypeReference)}.
67+
*/
68+
public <T> WebTestClient.BodyContentSpec value(ParameterizedTypeReference<T> targetType, Matcher<? super T> matcher) {
69+
getPathHelper().assertValue(getContent(), matcher, targetType);
70+
return getBodySpec();
71+
}
72+
4473
}

spring-test/src/main/java/org/springframework/test/web/reactive/server/StatusAssertions.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.test.web.reactive.server;
1818

19+
import org.hamcrest.Matcher;
20+
import org.hamcrest.MatcherAssert;
21+
1922
import org.springframework.http.HttpStatusCode;
2023
import org.springframework.test.web.support.AbstractStatusAssertions;
2124

@@ -45,4 +48,14 @@ protected void assertWithDiagnostics(Runnable assertion) {
4548
getExchangeResult().assertWithDiagnostics(assertion);
4649
}
4750

51+
/**
52+
* Match the response status value with a Hamcrest matcher.
53+
* @param matcher the matcher to use
54+
*/
55+
public WebTestClient.ResponseSpec value(Matcher<? super Integer> matcher) {
56+
int actual = getStatus().value();
57+
assertWithDiagnostics(() -> MatcherAssert.assertThat("Response status", actual, matcher));
58+
return getResponseSpec();
59+
}
60+
4861
}

spring-test/src/main/java/org/springframework/test/web/reactive/server/XpathAssertions.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import java.util.Map;
2020
import java.util.Optional;
2121

22+
import org.hamcrest.Matcher;
2223
import org.jspecify.annotations.Nullable;
2324

2425
import org.springframework.http.HttpHeaders;
26+
import org.springframework.test.util.XpathExpectationsHelper;
2527
import org.springframework.test.web.support.AbstractXpathAssertions;
2628
import org.springframework.util.Assert;
2729

@@ -55,4 +57,26 @@ protected byte[] getContent() {
5557
Assert.notNull(body, "Expected body content");
5658
return body;
5759
}
60+
61+
/**
62+
* Delegates to {@link XpathExpectationsHelper#assertString(byte[], String, Matcher)}.
63+
*/
64+
public WebTestClient.BodyContentSpec string(Matcher<? super String> matcher){
65+
return assertWith(() -> getXpathHelper().assertString(getContent(), getCharset(), matcher));
66+
}
67+
68+
/**
69+
* Delegates to {@link XpathExpectationsHelper#assertNumber(byte[], String, Matcher)}.
70+
*/
71+
public WebTestClient.BodyContentSpec number(Matcher<? super Double> matcher){
72+
return assertWith(() -> getXpathHelper().assertNumber(getContent(), getCharset(), matcher));
73+
}
74+
75+
/**
76+
* Delegates to {@link XpathExpectationsHelper#assertNodeCount(byte[], String, Matcher)}.
77+
*/
78+
public WebTestClient.BodyContentSpec nodeCount(Matcher<? super Integer> matcher){
79+
return assertWith(() -> getXpathHelper().assertNodeCount(getContent(), getCharset(), matcher));
80+
}
81+
5882
}

spring-test/src/main/java/org/springframework/test/web/servlet/client/DefaultRestTestClient.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import java.util.function.Consumer;
2929
import java.util.function.Function;
3030

31-
import org.hamcrest.Matcher;
32-
import org.hamcrest.MatcherAssert;
3331
import org.jspecify.annotations.Nullable;
3432

3533
import org.springframework.core.ParameterizedTypeReference;
@@ -380,26 +378,20 @@ public <T extends S> T isEqualTo(@Nullable B expected) {
380378
}
381379

382380
@Override
383-
public <T extends S> T value(Matcher<? super @Nullable B> matcher) {
384-
this.result.assertWithDiagnostics(() -> MatcherAssert.assertThat(this.result.getResponseBody(), matcher));
381+
public <T extends S> T value(Consumer<@Nullable B> consumer) {
382+
this.result.assertWithDiagnostics(() -> consumer.accept(this.result.getResponseBody()));
385383
return self();
386384
}
387385

388386
@Override
389-
public <T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super @Nullable R> matcher) {
387+
public <T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Consumer<? super @Nullable R> consumer) {
390388
this.result.assertWithDiagnostics(() -> {
391389
B body = this.result.getResponseBody();
392-
MatcherAssert.assertThat(bodyMapper.apply(body), matcher);
390+
consumer.accept(bodyMapper.apply(body));
393391
});
394392
return self();
395393
}
396394

397-
@Override
398-
public <T extends S> T value(Consumer<@Nullable B> consumer) {
399-
this.result.assertWithDiagnostics(() -> consumer.accept(this.result.getResponseBody()));
400-
return self();
401-
}
402-
403395
@Override
404396
public <T extends S> T consumeWith(Consumer<EntityExchangeResult<B>> consumer) {
405397
this.result.assertWithDiagnostics(() -> consumer.accept(this.result));

spring-test/src/main/java/org/springframework/test/web/servlet/client/RestTestClient.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.function.Consumer;
2525
import java.util.function.Function;
2626

27-
import org.hamcrest.Matcher;
2827
import org.jspecify.annotations.Nullable;
2928

3029
import org.springframework.core.ParameterizedTypeReference;
@@ -682,20 +681,15 @@ interface BodySpec<B, S extends BodySpec<B, S>> {
682681
<T extends S> T isEqualTo(@Nullable B expected);
683682

684683
/**
685-
* Assert the extracted body with a {@link Matcher}.
684+
* Assert the extracted body with a {@link Consumer}.
686685
*/
687-
<T extends S> T value(Matcher<? super @Nullable B> matcher);
686+
<T extends S> T value(Consumer<@Nullable B> consumer);
688687

689688
/**
690689
* Transform the extracted the body with a function, for example, extracting a
691-
* property, and assert the mapped value with a {@link Matcher}.
690+
* property, and assert the mapped value with a {@link Consumer}.
692691
*/
693-
<T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super @Nullable R> matcher);
694-
695-
/**
696-
* Assert the extracted body with a {@link Consumer}.
697-
*/
698-
<T extends S> T value(Consumer<@Nullable B> consumer);
692+
<T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Consumer<? super @Nullable R> consumer);
699693

700694
/**
701695
* Assert the exchange result with the given {@link Consumer}.

0 commit comments

Comments
 (0)