Skip to content

Commit 0319fe9

Browse files
committed
Deprecate Hamcrest use in WebTestClient
Closes gh-35703
1 parent b4c6300 commit 0319fe9

File tree

15 files changed

+95
-32
lines changed

15 files changed

+95
-32
lines changed

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

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

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

19+
import java.util.function.Consumer;
20+
1921
import org.hamcrest.Matcher;
2022

2123
import org.springframework.http.ResponseCookie;
@@ -53,7 +55,9 @@ protected void assertWithDiagnostics(Runnable assertion) {
5355
/**
5456
* Assert the value of the response cookie with the given name with a Hamcrest
5557
* {@link Matcher}.
58+
* @deprecated in favor of {@link Consumer}-based variants
5659
*/
60+
@Deprecated(since = "7.0", forRemoval = true)
5761
public WebTestClient.ResponseSpec value(String name, Matcher<? super String> matcher) {
5862
String value = getCookie(name).getValue();
5963
assertWithDiagnostics(() -> {
@@ -66,7 +70,9 @@ public WebTestClient.ResponseSpec value(String name, Matcher<? super String> mat
6670

6771
/**
6872
* Assert a cookie's "Max-Age" attribute with a Hamcrest {@link Matcher}.
73+
* @deprecated in favor of {@link Consumer}-based variants
6974
*/
75+
@Deprecated(since = "7.0", forRemoval = true)
7076
public WebTestClient.ResponseSpec maxAge(String name, Matcher<? super Long> matcher) {
7177
long maxAge = getCookie(name).getMaxAge().getSeconds();
7278
assertWithDiagnostics(() -> {
@@ -78,7 +84,9 @@ public WebTestClient.ResponseSpec maxAge(String name, Matcher<? super Long> matc
7884

7985
/**
8086
* Assert a cookie's "Path" attribute with a Hamcrest {@link Matcher}.
87+
* @deprecated in favor of {@link Consumer}-based variants
8188
*/
89+
@Deprecated(since = "7.0", forRemoval = true)
8290
public WebTestClient.ResponseSpec path(String name, Matcher<? super String> matcher) {
8391
String path = getCookie(name).getPath();
8492
assertWithDiagnostics(() -> {
@@ -90,7 +98,9 @@ public WebTestClient.ResponseSpec path(String name, Matcher<? super String> matc
9098

9199
/**
92100
* Assert a cookie's "Domain" attribute with a Hamcrest {@link Matcher}.
101+
* @deprecated in favor of {@link Consumer}-based variants
93102
*/
103+
@Deprecated(since = "7.0", forRemoval = true)
94104
public WebTestClient.ResponseSpec domain(String name, Matcher<? super String> matcher) {
95105
String domain = getCookie(name).getDomain();
96106
assertWithDiagnostics(() -> {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.jayway.jsonpath.spi.mapper.MappingProvider;
3535
import org.hamcrest.Matcher;
3636
import org.hamcrest.MatcherAssert;
37+
import org.jspecify.annotations.NonNull;
3738
import org.jspecify.annotations.Nullable;
3839
import org.reactivestreams.Publisher;
3940
import reactor.core.publisher.Flux;
@@ -584,14 +585,15 @@ public <T extends S> T isEqualTo(@Nullable B expected) {
584585
return self();
585586
}
586587

588+
@SuppressWarnings("removal")
587589
@Override
588590
public <T extends S> T value(Matcher<? super @Nullable B> matcher) {
589591
this.result.assertWithDiagnostics(() -> MatcherAssert.assertThat(this.result.getResponseBody(), matcher));
590592
return self();
591593
}
592594

593595
@Override
594-
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1129
596+
@SuppressWarnings({"NullAway", "removal"}) // https://github.com/uber/NullAway/issues/1129
595597
public <T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super @Nullable R> matcher) {
596598
this.result.assertWithDiagnostics(() -> {
597599
B body = this.result.getResponseBody();
@@ -606,6 +608,15 @@ public <T extends S> T value(Consumer<@Nullable B> consumer) {
606608
return self();
607609
}
608610

611+
@Override
612+
public <T extends S, R> T value(@NonNull Function<@Nullable B, @Nullable R> bodyMapper, Consumer<? super R> consumer) {
613+
this.result.assertWithDiagnostics(() -> {
614+
B body = this.result.getResponseBody();
615+
consumer.accept(bodyMapper.apply(body));
616+
});
617+
return self();
618+
}
619+
609620
@Override
610621
public <T extends S> T consumeWith(Consumer<EntityExchangeResult<B>> consumer) {
611622
this.result.assertWithDiagnostics(() -> consumer.accept(this.result));

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.test.web.reactive.server;
1818

1919
import java.util.List;
20+
import java.util.function.Consumer;
2021

2122
import org.hamcrest.Matcher;
2223

@@ -59,7 +60,9 @@ protected void assertWithDiagnostics(Runnable assertion) {
5960
* Assert the first value of the response header with a Hamcrest {@link Matcher}.
6061
* @param name the header name
6162
* @param matcher the matcher to use
63+
* @deprecated in favor of {@link Consumer}-based variants
6264
*/
65+
@Deprecated(since = "7.0", forRemoval = true)
6366
public WebTestClient.ResponseSpec value(String name, Matcher<? super String> matcher) {
6467
String value = getResponseHeaders().getFirst(name);
6568
assertWithDiagnostics(() -> {
@@ -73,7 +76,9 @@ public WebTestClient.ResponseSpec value(String name, Matcher<? super String> mat
7376
* Assert all values of the response header with a Hamcrest {@link Matcher}.
7477
* @param name the header name
7578
* @param matcher the matcher to use
79+
* @deprecated in favor of {@link Consumer}-based variants
7680
*/
81+
@Deprecated(since = "7.0", forRemoval = true)
7782
public WebTestClient.ResponseSpec values(String name, Matcher<? super Iterable<String>> matcher) {
7883
List<String> values = getResponseHeaders().get(name);
7984
assertWithDiagnostics(() -> {
@@ -83,5 +88,4 @@ public WebTestClient.ResponseSpec values(String name, Matcher<? super Iterable<S
8388
return getResponseSpec();
8489
}
8590

86-
8791
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

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

19+
import java.util.function.Consumer;
20+
1921
import com.jayway.jsonpath.Configuration;
2022
import org.hamcrest.Matcher;
2123
import org.jspecify.annotations.Nullable;
@@ -45,26 +47,31 @@ public class JsonPathAssertions extends AbstractJsonPathAssertions<WebTestClient
4547
}
4648

4749

48-
4950
/**
5051
* Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher)}.
52+
* @deprecated in favor of {@link Consumer}-based variants
5153
*/
54+
@Deprecated(since = "7.0", forRemoval = true)
5255
public <T> WebTestClient.BodyContentSpec value(Matcher<? super T> matcher) {
5356
getPathHelper().assertValue(getContent(), matcher);
5457
return getBodySpec();
5558
}
5659

5760
/**
5861
* Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher, Class)}.
62+
* @deprecated in favor of {@link Consumer}-based variants
5963
*/
64+
@Deprecated(since = "7.0", forRemoval = true)
6065
public <T> WebTestClient.BodyContentSpec value(Class<T> targetType, Matcher<? super T> matcher) {
6166
getPathHelper().assertValue(getContent(), matcher, targetType);
6267
return getBodySpec();
6368
}
6469

6570
/**
6671
* Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher, ParameterizedTypeReference)}.
72+
* @deprecated in favor of {@link Consumer}-based variants
6773
*/
74+
@Deprecated(since = "7.0", forRemoval = true)
6875
public <T> WebTestClient.BodyContentSpec value(ParameterizedTypeReference<T> targetType, Matcher<? super T> matcher) {
6976
getPathHelper().assertValue(getContent(), matcher, targetType);
7077
return getBodySpec();

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

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

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

19+
import java.util.function.Consumer;
20+
1921
import org.hamcrest.Matcher;
2022
import org.hamcrest.MatcherAssert;
2123

@@ -51,7 +53,9 @@ protected void assertWithDiagnostics(Runnable assertion) {
5153
/**
5254
* Match the response status value with a Hamcrest matcher.
5355
* @param matcher the matcher to use
56+
* @deprecated in favor of {@link Consumer}-based variants
5457
*/
58+
@Deprecated(since = "7.0", forRemoval = true)
5559
public WebTestClient.ResponseSpec value(Matcher<? super Integer> matcher) {
5660
int actual = getStatus().value();
5761
assertWithDiagnostics(() -> MatcherAssert.assertThat("Response status", actual, matcher));

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,15 +957,19 @@ interface BodySpec<B, S extends BodySpec<B, S>> {
957957
/**
958958
* Assert the extracted body with a {@link Matcher}.
959959
* @since 5.1
960+
* @deprecated in favor of {@link Consumer}-based variants
960961
*/
962+
@Deprecated(since = "7.0", forRemoval = true)
961963
@NullUnmarked // To avoid a "Cannot attach type annotations" error when org.hamcrest.Matcher is not in the classpath
962964
<T extends S> T value(Matcher<? super B> matcher);
963965

964966
/**
965967
* Transform the extracted the body with a function, for example, extracting a
966968
* property, and assert the mapped value with a {@link Matcher}.
967969
* @since 5.1
970+
* @deprecated in favor of {@link Consumer}-based variants
968971
*/
972+
@Deprecated(since = "7.0", forRemoval = true)
969973
@NullUnmarked // To avoid a "Cannot attach type annotations" error when org.hamcrest.Matcher is not in the classpath
970974
<T extends S, R> T value(@NonNull Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super R> matcher);
971975

@@ -975,6 +979,13 @@ interface BodySpec<B, S extends BodySpec<B, S>> {
975979
*/
976980
<T extends S> T value(Consumer<@Nullable B> consumer);
977981

982+
/**
983+
* Transform the extracted the body with a function, for example, extracting a
984+
* property, and assert the mapped value with a {@link Consumer}.
985+
* @since 7.0
986+
*/
987+
<T extends S, R> T value(@NonNull Function<@Nullable B, @Nullable R> bodyMapper, Consumer<? super R> consumer);
988+
978989
/**
979990
* Assert the exchange result with the given {@link Consumer}.
980991
*/

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Map;
2020
import java.util.Optional;
21+
import java.util.function.Consumer;
2122

2223
import org.hamcrest.Matcher;
2324
import org.jspecify.annotations.Nullable;
@@ -60,21 +61,27 @@ protected byte[] getContent() {
6061

6162
/**
6263
* Delegates to {@link XpathExpectationsHelper#assertString(byte[], String, Matcher)}.
64+
* @deprecated in favor of {@link Consumer}-based variants
6365
*/
66+
@Deprecated(since = "7.0", forRemoval = true)
6467
public WebTestClient.BodyContentSpec string(Matcher<? super String> matcher){
6568
return assertWith(() -> getXpathHelper().assertString(getContent(), getCharset(), matcher));
6669
}
6770

6871
/**
6972
* Delegates to {@link XpathExpectationsHelper#assertNumber(byte[], String, Matcher)}.
73+
* @deprecated in favor of {@link Consumer}-based variants
7074
*/
75+
@Deprecated(since = "7.0", forRemoval = true)
7176
public WebTestClient.BodyContentSpec number(Matcher<? super Double> matcher){
7277
return assertWith(() -> getXpathHelper().assertNumber(getContent(), getCharset(), matcher));
7378
}
7479

7580
/**
7681
* Delegates to {@link XpathExpectationsHelper#assertNodeCount(byte[], String, Matcher)}.
82+
* @deprecated in favor of {@link Consumer}-based variants
7783
*/
84+
@Deprecated(since = "7.0", forRemoval = true)
7885
public WebTestClient.BodyContentSpec nodeCount(Matcher<? super Integer> matcher){
7986
return assertWith(() -> getXpathHelper().assertNodeCount(getContent(), getCharset(), matcher));
8087
}

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.net.URI;
2020

21+
import org.hamcrest.MatcherAssert;
2122
import org.junit.jupiter.api.Test;
2223
import reactor.core.publisher.Flux;
2324

@@ -113,7 +114,7 @@ void jsonPathMatches() {
113114
.exchange()
114115
.expectStatus().isOk()
115116
.expectBody()
116-
.jsonPath("$.firstName").value(containsString("oh"));
117+
.jsonPath("$.firstName").value(String.class, v -> MatcherAssert.assertThat(v, containsString("oh")));
117118
}
118119

119120
@Test

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ResponseEntityTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424

25+
import org.hamcrest.MatcherAssert;
2526
import org.junit.jupiter.api.Test;
2627
import reactor.core.publisher.Flux;
2728
import reactor.test.StepVerifier;
@@ -72,7 +73,7 @@ void entityMatcher() {
7273
.exchange()
7374
.expectStatus().isOk()
7475
.expectHeader().contentType(MediaType.APPLICATION_JSON)
75-
.expectBody(Person.class).value(Person::getName, startsWith("Joh"));
76+
.expectBody(Person.class).value(Person::getName, v -> MatcherAssert.assertThat(v, startsWith("Joh")));
7677
}
7778

7879
@Test

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/XmlContentTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import jakarta.xml.bind.annotation.XmlAccessorType;
2626
import jakarta.xml.bind.annotation.XmlElement;
2727
import jakarta.xml.bind.annotation.XmlRootElement;
28+
import org.hamcrest.MatcherAssert;
2829
import org.junit.jupiter.api.Test;
2930

3031
import org.springframework.http.HttpHeaders;
@@ -93,7 +94,7 @@ void xpathMatches() {
9394
.exchange()
9495
.expectStatus().isOk()
9596
.expectBody()
96-
.xpath("//person/name").string(startsWith("J"));
97+
.xpath("//person/name").string(v -> MatcherAssert.assertThat(v, startsWith("J")));
9798
}
9899

99100
@Test

0 commit comments

Comments
 (0)