mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-22 22:19:03 +00:00
Deprecate Hamcrest use in WebTestClient
Closes gh-35703
This commit is contained in:
+2
-1
@@ -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 {
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody()
|
||||
.jsonPath("$.firstName").value(containsString("oh"));
|
||||
.jsonPath("$.firstName").value(String.class, v -> MatcherAssert.assertThat(v, containsString("oh")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+2
-1
@@ -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 {
|
||||
.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
|
||||
|
||||
+2
-1
@@ -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 {
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody()
|
||||
.xpath("//person/name").string(startsWith("J"));
|
||||
.xpath("//person/name").string(v -> MatcherAssert.assertThat(v, startsWith("J")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+2
-1
@@ -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 {
|
||||
.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/>
|
||||
|
||||
+3
-2
@@ -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 {
|
||||
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
|
||||
|
||||
+6
-4
@@ -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 {
|
||||
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 {
|
||||
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 {
|
||||
.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()
|
||||
|
||||
+12
-11
@@ -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 {
|
||||
.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 {
|
||||
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 {
|
||||
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")))));
|
||||
}
|
||||
|
||||
|
||||
|
||||
+9
-8
@@ -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 {
|
||||
.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 {
|
||||
.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 {
|
||||
.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 {
|
||||
.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
|
||||
|
||||
Reference in New Issue
Block a user