Browse Source

Deprecate Hamcrest use in WebTestClient

Closes gh-35703
pull/35473/head
rstoyanchev 2 months ago
parent
commit
0319fe9211
  1. 10
      spring-test/src/main/java/org/springframework/test/web/reactive/server/CookieAssertions.java
  2. 13
      spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java
  3. 6
      spring-test/src/main/java/org/springframework/test/web/reactive/server/HeaderAssertions.java
  4. 9
      spring-test/src/main/java/org/springframework/test/web/reactive/server/JsonPathAssertions.java
  5. 4
      spring-test/src/main/java/org/springframework/test/web/reactive/server/StatusAssertions.java
  6. 11
      spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java
  7. 7
      spring-test/src/main/java/org/springframework/test/web/reactive/server/XpathAssertions.java
  8. 3
      spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/JsonContentTests.java
  9. 3
      spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ResponseEntityTests.java
  10. 3
      spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/XmlContentTests.java
  11. 3
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/context/WebAppResourceTests.java
  12. 5
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/RouterFunctionTests.java
  13. 10
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/resultmatches/ContentAssertionTests.java
  14. 23
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/resultmatches/JsonPathAssertionTests.java
  15. 17
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/resultmatches/XpathAssertionTests.java

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

@ -16,6 +16,8 @@ @@ -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 @@ -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 @@ -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 @@ -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 @@ -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(() -> {

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

@ -34,6 +34,7 @@ import com.jayway.jsonpath.Configuration; @@ -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 { @@ -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 { @@ -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 { @@ -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));

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

@ -17,6 +17,7 @@ @@ -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 @@ -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 @@ -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 @@ -83,5 +88,4 @@ public class HeaderAssertions extends AbstractHeaderAssertions<ExchangeResult, W
return getResponseSpec();
}
}

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

@ -16,6 +16,8 @@ @@ -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 @@ -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 @@ -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 @@ -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();

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

@ -16,6 +16,8 @@ @@ -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 @@ -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));

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

@ -957,7 +957,9 @@ public interface WebTestClient { @@ -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 { @@ -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 { @@ -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}.
*/

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

@ -18,6 +18,7 @@ package org.springframework.test.web.reactive.server; @@ -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 @@ -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));
}

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

@ -18,6 +18,7 @@ package org.springframework.test.web.reactive.server.samples; @@ -18,6 +18,7 @@ package org.springframework.test.web.reactive.server.samples;
import java.net.URI;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
@ -113,7 +114,7 @@ class JsonContentTests { @@ -113,7 +114,7 @@ class JsonContentTests {
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.firstName").value(containsString("oh"));
.jsonPath("$.firstName").value(String.class, v -> MatcherAssert.assertThat(v, containsString("oh")));
}
@Test

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

@ -22,6 +22,7 @@ import java.util.LinkedHashMap; @@ -22,6 +22,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
@ -72,7 +73,7 @@ class ResponseEntityTests { @@ -72,7 +73,7 @@ class ResponseEntityTests {
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody(Person.class).value(Person::getName, startsWith("Joh"));
.expectBody(Person.class).value(Person::getName, v -> MatcherAssert.assertThat(v, startsWith("Joh")));
}
@Test

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

@ -25,6 +25,7 @@ import jakarta.xml.bind.annotation.XmlAccessType; @@ -25,6 +25,7 @@ import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
@ -93,7 +94,7 @@ class XmlContentTests { @@ -93,7 +94,7 @@ class XmlContentTests {
.exchange()
.expectStatus().isOk()
.expectBody()
.xpath("//person/name").string(startsWith("J"));
.xpath("//person/name").string(v -> MatcherAssert.assertThat(v, startsWith("J")));
}
@Test

3
spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/context/WebAppResourceTests.java

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.test.web.servlet.samples.client.context;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -70,7 +71,7 @@ public class WebAppResourceTests { @@ -70,7 +71,7 @@ public class WebAppResourceTests {
.exchange()
.expectStatus().isOk()
.expectHeader().contentType("text/javascript")
.expectBody(String.class).value(containsString("Spring={};"));
.expectBody(String.class).value(v -> MatcherAssert.assertThat(v, containsString("Spring={};")));
}
// Forwarded to the "default" servlet via <mvc:default-servlet-handler/>

5
spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/RouterFunctionTests.java

@ -19,6 +19,7 @@ package org.springframework.test.web.servlet.samples.client.standalone; @@ -19,6 +19,7 @@ package org.springframework.test.web.servlet.samples.client.standalone;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
@ -46,8 +47,8 @@ public class RouterFunctionTests { @@ -46,8 +47,8 @@ public class RouterFunctionTests {
void json() {
execute("/person/Lee", body -> body.jsonPath("$.name").isEqualTo("Lee")
.jsonPath("$.age").isEqualTo(42)
.jsonPath("$.age").value(equalTo(42))
.jsonPath("$.age").value(Float.class, equalTo(42.0f)));
.jsonPath("$.age").value(v -> MatcherAssert.assertThat(v, equalTo(42)))
.jsonPath("$.age").value(Float.class, v -> MatcherAssert.assertThat(v, equalTo(42.0f))));
}
@Test

10
spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/resultmatches/ContentAssertionTests.java

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.test.web.servlet.samples.client.standalone.resultmatches;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
@ -77,11 +78,12 @@ class ContentAssertionTests { @@ -77,11 +78,12 @@ class ContentAssertionTests {
testClient.get().uri("/handle").accept(TEXT_PLAIN)
.exchange()
.expectStatus().isOk()
.expectBody(String.class).value(equalTo("Hello world!"));
.expectBody(String.class).value(v -> MatcherAssert.assertThat(v, equalTo("Hello world!")));
testClient.get().uri("/handleUtf8")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).value(equalTo("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"));
.expectBody(String.class).value(v ->
MatcherAssert.assertThat(v, equalTo("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01")));
}
@Test
@ -104,7 +106,7 @@ class ContentAssertionTests { @@ -104,7 +106,7 @@ class ContentAssertionTests {
testClient.get().uri("/handle").accept(TEXT_PLAIN)
.exchange()
.expectStatus().isOk()
.expectBody(String.class).value(containsString("world"));
.expectBody(String.class).value(v -> MatcherAssert.assertThat(v, containsString("world")));
}
@Test
@ -113,7 +115,7 @@ class ContentAssertionTests { @@ -113,7 +115,7 @@ class ContentAssertionTests {
.exchange()
.expectStatus().isOk()
.expectHeader().contentType("text/plain;charset=ISO-8859-1")
.expectBody(String.class).value(containsString("world"));
.expectBody(String.class).value(v -> MatcherAssert.assertThat(v, containsString("world")));
testClient.get().uri("/handleUtf8")
.exchange()

23
spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/resultmatches/JsonPathAssertionTests.java

@ -16,8 +16,9 @@ @@ -16,8 +16,9 @@
package org.springframework.test.web.servlet.samples.client.standalone.resultmatches;
import java.util.Arrays;
import java.util.List;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
@ -100,8 +101,8 @@ public class JsonPathAssertionTests { @@ -100,8 +101,8 @@ public class JsonPathAssertionTests {
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody()
.jsonPath("$.composers[0].name").value(equalTo("Johann Sebastian Bach"))
.jsonPath("$.performers[1].name").value(equalTo("Yehudi Menuhin"));
.jsonPath("$.composers[0].name").value(v -> MatcherAssert.assertThat(v, equalTo("Johann Sebastian Bach")))
.jsonPath("$.performers[1].name").value(v -> MatcherAssert.assertThat(v, equalTo("Yehudi Menuhin")));
}
@Test
@ -109,10 +110,10 @@ public class JsonPathAssertionTests { @@ -109,10 +110,10 @@ public class JsonPathAssertionTests {
client.get().uri("/music/people")
.exchange()
.expectBody()
.jsonPath("$.composers[0].name").value(startsWith("Johann"))
.jsonPath("$.performers[0].name").value(endsWith("Ashkenazy"))
.jsonPath("$.performers[1].name").value(containsString("di Me"))
.jsonPath("$.composers[1].name").value(is(in(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms"))));
.jsonPath("$.composers[0].name").value(String.class, v -> MatcherAssert.assertThat(v, startsWith("Johann")))
.jsonPath("$.performers[0].name").value(String.class, v -> MatcherAssert.assertThat(v, endsWith("Ashkenazy")))
.jsonPath("$.performers[1].name").value(String.class, v -> MatcherAssert.assertThat(v, containsString("di Me")))
.jsonPath("$.composers[1].name").value(v -> MatcherAssert.assertThat(v, is(in(List.of("Johann Sebastian Bach", "Johannes Brahms")))));
}
@Test
@ -120,10 +121,10 @@ public class JsonPathAssertionTests { @@ -120,10 +121,10 @@ public class JsonPathAssertionTests {
client.get().uri("/music/people")
.exchange()
.expectBody()
.jsonPath("$.composers[0].name").value(startsWith("Johann"))
.jsonPath("$.performers[0].name").value(endsWith("Ashkenazy"))
.jsonPath("$.performers[1].name").value(containsString("di Me"))
.jsonPath("$.composers[1].name").value(is(in(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms"))));
.jsonPath("$.composers[0].name").value(String.class, v -> MatcherAssert.assertThat(v, startsWith("Johann")))
.jsonPath("$.performers[0].name").value(String.class, v -> MatcherAssert.assertThat(v, endsWith("Ashkenazy")))
.jsonPath("$.performers[1].name").value(String.class, v -> MatcherAssert.assertThat(v, containsString("di Me")))
.jsonPath("$.composers[1].name").value(v -> MatcherAssert.assertThat(v, is(in(List.of("Johann Sebastian Bach", "Johannes Brahms")))));
}

17
spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/resultmatches/XpathAssertionTests.java

@ -26,6 +26,7 @@ import jakarta.xml.bind.annotation.XmlAccessorType; @@ -26,6 +26,7 @@ import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlElementWrapper;
import jakarta.xml.bind.annotation.XmlRootElement;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
@ -80,7 +81,7 @@ public class XpathAssertionTests { @@ -80,7 +81,7 @@ public class XpathAssertionTests {
.xpath(composer, musicNamespace, 4).exists()
.xpath(performer, musicNamespace, 1).exists()
.xpath(performer, musicNamespace, 2).exists()
.xpath(composer, musicNamespace, 1).string(notNullValue());
.xpath(composer, musicNamespace, 1).string(v -> MatcherAssert.assertThat(v, notNullValue()));
}
@Test
@ -112,9 +113,9 @@ public class XpathAssertionTests { @@ -112,9 +113,9 @@ public class XpathAssertionTests {
.xpath(composerName, musicNamespace, 4).isEqualTo("Robert Schumann")
.xpath(performerName, musicNamespace, 1).isEqualTo("Vladimir Ashkenazy")
.xpath(performerName, musicNamespace, 2).isEqualTo("Yehudi Menuhin")
.xpath(composerName, musicNamespace, 1).string(equalTo("Johann Sebastian Bach")) // Hamcrest..
.xpath(composerName, musicNamespace, 1).string(startsWith("Johann"))
.xpath(composerName, musicNamespace, 1).string(notNullValue());
.xpath(composerName, musicNamespace, 1).string(v -> MatcherAssert.assertThat(v, equalTo("Johann Sebastian Bach"))) // Hamcrest..
.xpath(composerName, musicNamespace, 1).string(v -> MatcherAssert.assertThat(v, startsWith("Johann")))
.xpath(composerName, musicNamespace, 1).string(v -> MatcherAssert.assertThat(v, notNullValue()));
}
@Test
@ -128,8 +129,8 @@ public class XpathAssertionTests { @@ -128,8 +129,8 @@ public class XpathAssertionTests {
.xpath(expression, musicNamespace, 2).isEqualTo(.0025)
.xpath(expression, musicNamespace, 3).isEqualTo(1.6035)
.xpath(expression, musicNamespace, 4).isEqualTo(Double.NaN)
.xpath(expression, musicNamespace, 1).number(equalTo(21d)) // Hamcrest..
.xpath(expression, musicNamespace, 3).number(closeTo(1.6, .01));
.xpath(expression, musicNamespace, 1).number(v -> MatcherAssert.assertThat(v, equalTo(21d))) // Hamcrest..
.xpath(expression, musicNamespace, 3).number(v -> MatcherAssert.assertThat(v, closeTo(1.6, .01)));
}
@Test
@ -150,8 +151,8 @@ public class XpathAssertionTests { @@ -150,8 +151,8 @@ public class XpathAssertionTests {
.expectBody()
.xpath("/ns:people/composers/composer", musicNamespace).nodeCount(4)
.xpath("/ns:people/performers/performer", musicNamespace).nodeCount(2)
.xpath("/ns:people/composers/composer", musicNamespace).nodeCount(equalTo(4)) // Hamcrest..
.xpath("/ns:people/performers/performer", musicNamespace).nodeCount(equalTo(2));
.xpath("/ns:people/composers/composer", musicNamespace).nodeCount(v -> MatcherAssert.assertThat(v, equalTo(4))) // Hamcrest..
.xpath("/ns:people/performers/performer", musicNamespace).nodeCount(v -> MatcherAssert.assertThat(v, equalTo(2)));
}
@Test

Loading…
Cancel
Save