Deprecate Hamcrest use in WebTestClient

Closes gh-35703
This commit is contained in:
rstoyanchev
2025-10-29 09:49:48 +00:00
parent b4c6300ac6
commit 0319fe9211
15 changed files with 95 additions and 32 deletions
@@ -16,6 +16,8 @@
package org.springframework.test.web.reactive.server;
import java.util.function.Consumer;
import org.hamcrest.Matcher;
import org.springframework.http.ResponseCookie;
@@ -53,7 +55,9 @@ public class CookieAssertions extends AbstractCookieAssertions<ExchangeResult, W
/**
* Assert the value of the response cookie with the given name with a Hamcrest
* {@link Matcher}.
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public WebTestClient.ResponseSpec value(String name, Matcher<? super String> matcher) {
String value = getCookie(name).getValue();
assertWithDiagnostics(() -> {
@@ -66,7 +70,9 @@ public class CookieAssertions extends AbstractCookieAssertions<ExchangeResult, W
/**
* Assert a cookie's "Max-Age" attribute with a Hamcrest {@link Matcher}.
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public WebTestClient.ResponseSpec maxAge(String name, Matcher<? super Long> matcher) {
long maxAge = getCookie(name).getMaxAge().getSeconds();
assertWithDiagnostics(() -> {
@@ -78,7 +84,9 @@ public class CookieAssertions extends AbstractCookieAssertions<ExchangeResult, W
/**
* Assert a cookie's "Path" attribute with a Hamcrest {@link Matcher}.
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public WebTestClient.ResponseSpec path(String name, Matcher<? super String> matcher) {
String path = getCookie(name).getPath();
assertWithDiagnostics(() -> {
@@ -90,7 +98,9 @@ public class CookieAssertions extends AbstractCookieAssertions<ExchangeResult, W
/**
* Assert a cookie's "Domain" attribute with a Hamcrest {@link Matcher}.
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public WebTestClient.ResponseSpec domain(String name, Matcher<? super String> matcher) {
String domain = getCookie(name).getDomain();
assertWithDiagnostics(() -> {
@@ -34,6 +34,7 @@ import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
@@ -584,6 +585,7 @@ class DefaultWebTestClient implements WebTestClient {
return self();
}
@SuppressWarnings("removal")
@Override
public <T extends S> T value(Matcher<? super @Nullable B> matcher) {
this.result.assertWithDiagnostics(() -> MatcherAssert.assertThat(this.result.getResponseBody(), matcher));
@@ -591,7 +593,7 @@ class DefaultWebTestClient implements WebTestClient {
}
@Override
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1129
@SuppressWarnings({"NullAway", "removal"}) // https://github.com/uber/NullAway/issues/1129
public <T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super @Nullable R> matcher) {
this.result.assertWithDiagnostics(() -> {
B body = this.result.getResponseBody();
@@ -606,6 +608,15 @@ class DefaultWebTestClient implements WebTestClient {
return self();
}
@Override
public <T extends S, R> T value(@NonNull Function<@Nullable B, @Nullable R> bodyMapper, Consumer<? super R> consumer) {
this.result.assertWithDiagnostics(() -> {
B body = this.result.getResponseBody();
consumer.accept(bodyMapper.apply(body));
});
return self();
}
@Override
public <T extends S> T consumeWith(Consumer<EntityExchangeResult<B>> consumer) {
this.result.assertWithDiagnostics(() -> consumer.accept(this.result));
@@ -17,6 +17,7 @@
package org.springframework.test.web.reactive.server;
import java.util.List;
import java.util.function.Consumer;
import org.hamcrest.Matcher;
@@ -59,7 +60,9 @@ public class HeaderAssertions extends AbstractHeaderAssertions<ExchangeResult, W
* Assert the first value of the response header with a Hamcrest {@link Matcher}.
* @param name the header name
* @param matcher the matcher to use
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public WebTestClient.ResponseSpec value(String name, Matcher<? super String> matcher) {
String value = getResponseHeaders().getFirst(name);
assertWithDiagnostics(() -> {
@@ -73,7 +76,9 @@ public class HeaderAssertions extends AbstractHeaderAssertions<ExchangeResult, W
* Assert all values of the response header with a Hamcrest {@link Matcher}.
* @param name the header name
* @param matcher the matcher to use
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public WebTestClient.ResponseSpec values(String name, Matcher<? super Iterable<String>> matcher) {
List<String> values = getResponseHeaders().get(name);
assertWithDiagnostics(() -> {
@@ -83,5 +88,4 @@ public class HeaderAssertions extends AbstractHeaderAssertions<ExchangeResult, W
return getResponseSpec();
}
}
@@ -16,6 +16,8 @@
package org.springframework.test.web.reactive.server;
import java.util.function.Consumer;
import com.jayway.jsonpath.Configuration;
import org.hamcrest.Matcher;
import org.jspecify.annotations.Nullable;
@@ -45,10 +47,11 @@ public class JsonPathAssertions extends AbstractJsonPathAssertions<WebTestClient
}
/**
* Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher)}.
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public <T> WebTestClient.BodyContentSpec value(Matcher<? super T> matcher) {
getPathHelper().assertValue(getContent(), matcher);
return getBodySpec();
@@ -56,7 +59,9 @@ public class JsonPathAssertions extends AbstractJsonPathAssertions<WebTestClient
/**
* Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher, Class)}.
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public <T> WebTestClient.BodyContentSpec value(Class<T> targetType, Matcher<? super T> matcher) {
getPathHelper().assertValue(getContent(), matcher, targetType);
return getBodySpec();
@@ -64,7 +69,9 @@ public class JsonPathAssertions extends AbstractJsonPathAssertions<WebTestClient
/**
* Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher, ParameterizedTypeReference)}.
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public <T> WebTestClient.BodyContentSpec value(ParameterizedTypeReference<T> targetType, Matcher<? super T> matcher) {
getPathHelper().assertValue(getContent(), matcher, targetType);
return getBodySpec();
@@ -16,6 +16,8 @@
package org.springframework.test.web.reactive.server;
import java.util.function.Consumer;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
@@ -51,7 +53,9 @@ public class StatusAssertions extends AbstractStatusAssertions<ExchangeResult, W
/**
* Match the response status value with a Hamcrest matcher.
* @param matcher the matcher to use
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public WebTestClient.ResponseSpec value(Matcher<? super Integer> matcher) {
int actual = getStatus().value();
assertWithDiagnostics(() -> MatcherAssert.assertThat("Response status", actual, matcher));
@@ -957,7 +957,9 @@ public interface WebTestClient {
/**
* Assert the extracted body with a {@link Matcher}.
* @since 5.1
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
@NullUnmarked // To avoid a "Cannot attach type annotations" error when org.hamcrest.Matcher is not in the classpath
<T extends S> T value(Matcher<? super B> matcher);
@@ -965,7 +967,9 @@ public interface WebTestClient {
* Transform the extracted the body with a function, for example, extracting a
* property, and assert the mapped value with a {@link Matcher}.
* @since 5.1
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
@NullUnmarked // To avoid a "Cannot attach type annotations" error when org.hamcrest.Matcher is not in the classpath
<T extends S, R> T value(@NonNull Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super R> matcher);
@@ -975,6 +979,13 @@ public interface WebTestClient {
*/
<T extends S> T value(Consumer<@Nullable B> consumer);
/**
* Transform the extracted the body with a function, for example, extracting a
* property, and assert the mapped value with a {@link Consumer}.
* @since 7.0
*/
<T extends S, R> T value(@NonNull Function<@Nullable B, @Nullable R> bodyMapper, Consumer<? super R> consumer);
/**
* Assert the exchange result with the given {@link Consumer}.
*/
@@ -18,6 +18,7 @@ package org.springframework.test.web.reactive.server;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import org.hamcrest.Matcher;
import org.jspecify.annotations.Nullable;
@@ -60,21 +61,27 @@ public class XpathAssertions extends AbstractXpathAssertions<WebTestClient.BodyC
/**
* Delegates to {@link XpathExpectationsHelper#assertString(byte[], String, Matcher)}.
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public WebTestClient.BodyContentSpec string(Matcher<? super String> matcher){
return assertWith(() -> getXpathHelper().assertString(getContent(), getCharset(), matcher));
}
/**
* Delegates to {@link XpathExpectationsHelper#assertNumber(byte[], String, Matcher)}.
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public WebTestClient.BodyContentSpec number(Matcher<? super Double> matcher){
return assertWith(() -> getXpathHelper().assertNumber(getContent(), getCharset(), matcher));
}
/**
* Delegates to {@link XpathExpectationsHelper#assertNodeCount(byte[], String, Matcher)}.
* @deprecated in favor of {@link Consumer}-based variants
*/
@Deprecated(since = "7.0", forRemoval = true)
public WebTestClient.BodyContentSpec nodeCount(Matcher<? super Integer> matcher){
return assertWith(() -> getXpathHelper().assertNodeCount(getContent(), getCharset(), matcher));
}