Remove Hamcrest use from RestTestClient

Closes gh-35702
This commit is contained in:
rstoyanchev
2025-10-29 09:50:36 +00:00
parent 92a186b44b
commit b4c6300ac6
24 changed files with 295 additions and 208 deletions
@@ -20,6 +20,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
@@ -101,8 +102,8 @@ 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
@@ -110,10 +111,10 @@ 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(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms")))));
}
@Test
@@ -121,11 +122,11 @@ class JsonPathAssertionTests {
client.get().uri("/music/people")
.exchange()
.expectBody()
.jsonPath("$.composers[0].name").value(String.class, startsWith("Johann"))
.jsonPath("$.composers[0].name").value(String.class, v -> MatcherAssert.assertThat(v, startsWith("Johann")))
.jsonPath("$.composers[0].name").value(String.class, s -> assertThat(s).startsWith("Johann"))
.jsonPath("$.composers[0].name").value(o -> assertThat((String) o).startsWith("Johann"))
.jsonPath("$.performers[1].name").value(containsString("di Me"))
.jsonPath("$.composers[1].name").value(is(in(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms"))));
.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(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms")))));
}
@Test
@@ -25,7 +25,7 @@ import java.util.Map;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.hamcrest.Matchers;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@@ -44,6 +44,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.DefaultUriBuilderFactory;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.equalTo;
/**
* Tests using the {@link RestTestClient} API.
@@ -285,7 +286,7 @@ class RestTestClientTests {
void testExpectCookie() {
RestTestClientTests.this.client.get().uri("/test")
.exchange()
.expectCookie().value("session", Matchers.equalTo("abc"));
.expectCookie().value("session", v -> MatcherAssert.assertThat(v, equalTo("abc")));
}
}
@@ -19,6 +19,7 @@ package org.springframework.test.web.servlet.client.samples;
import java.net.URI;
import java.util.List;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
@@ -111,7 +112,7 @@ class JsonContentTests {
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.firstName").value(containsString("oh"));
.jsonPath("$.firstName").value(String.class, v -> MatcherAssert.assertThat(v, containsString("oh")));
}
@Test
@@ -21,6 +21,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.springframework.core.ParameterizedTypeReference;
@@ -64,7 +65,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, name -> MatcherAssert.assertThat(name, startsWith("Joh")));
}
@Test
@@ -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;
@@ -104,7 +105,7 @@ class XmlContentTests {
.expectStatus().isOk()
.expectBody()
.xpath("/persons/person").nodeCount(3)
.xpath("/persons/person").nodeCount(equalTo(3));
.xpath("/persons/person").nodeCount(count -> MatcherAssert.assertThat(count, equalTo(3)));
}
@Test
@@ -114,10 +115,10 @@ class XmlContentTests {
.exchange()
.expectStatus().isOk()
.expectBody()
.xpath("//person/name").string(startsWith("J"))
.xpath("//person/name").string(s -> {
if (!s.startsWith("J")) {
throw new AssertionError("Name does not start with J: " + s);
.xpath("//person/name").string(name -> MatcherAssert.assertThat(name, startsWith("J")))
.xpath("//person/name").string(name -> {
if (!name.startsWith("J")) {
throw new AssertionError("Name does not start with J: " + name);
}
});
}
@@ -20,6 +20,7 @@ import java.util.List;
import java.util.function.Consumer;
import jakarta.validation.constraints.NotNull;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.springframework.core.ParameterizedTypeReference;
@@ -46,8 +47,8 @@ class ResponseBodyTests {
void json() {
execute("/persons/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
@@ -18,6 +18,7 @@ package org.springframework.test.web.servlet.samples.client.standalone.resultmat
import java.time.Duration;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -75,12 +76,14 @@ public class CookieAssertionTests {
@Test
public void testEqualTo() {
client.get().uri("/").exchange().expectCookie().valueEquals(COOKIE_NAME, "en-US");
client.get().uri("/").exchange().expectCookie().value(COOKIE_NAME, equalTo("en-US"));
client.get().uri("/").exchange().expectCookie()
.value(COOKIE_NAME, v -> MatcherAssert.assertThat(v, equalTo("en-US")));
}
@Test
public void testMatcher() {
client.get().uri("/").exchange().expectCookie().value(COOKIE_NAME, startsWith("en-US"));
client.get().uri("/").exchange().expectCookie()
.value(COOKIE_NAME, v -> MatcherAssert.assertThat(v, startsWith("en-US")));
}
@Test
@@ -22,6 +22,7 @@ import java.util.Locale;
import java.util.TimeZone;
import java.util.function.Consumer;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -90,7 +91,7 @@ class HeaderAssertionTests {
testClient.get().uri("/persons/1").header(IF_MODIFIED_SINCE, minuteAgo)
.exchange()
.expectStatus().isOk()
.expectHeader().value(LAST_MODIFIED, equalTo(now));
.expectHeader().value(LAST_MODIFIED, v -> MatcherAssert.assertThat(v, equalTo(now)));
}
@Test
@@ -106,7 +107,8 @@ class HeaderAssertionTests {
testClient.get().uri("/persons/1")
.exchange()
.expectStatus().isOk()
.expectHeader().values(VARY, hasItems(containsString("foo"), startsWith("bar")));
.expectHeader().values(VARY, v ->
MatcherAssert.assertThat(v, hasItems(containsString("foo"), startsWith("bar"))));
}
@Test
@@ -140,7 +142,7 @@ class HeaderAssertionTests {
testClient.get().uri("/persons/1").header(IF_MODIFIED_SINCE, now)
.exchange()
.expectStatus().isNotModified()
.expectHeader().value("X-Custom-Header", nullValue());
.expectHeader().value("X-Custom-Header", v -> MatcherAssert.assertThat(v, nullValue()));
}
@Test
@@ -202,8 +204,11 @@ class HeaderAssertionTests {
long secondLater = this.currentTime + 1000;
String expected = this.dateFormat.format(new Date(secondLater));
assertIncorrectResponseHeader(spec -> spec.expectHeader().valueEquals(LAST_MODIFIED, expected), expected);
assertIncorrectResponseHeader(spec -> spec.expectHeader().value(LAST_MODIFIED, equalTo(expected)), expected);
// Comparison by date uses HttpHeaders to format the date in the error message.
assertIncorrectResponseHeader(spec -> spec.expectHeader().value(LAST_MODIFIED, value -> {
// Comparison by date uses HttpHeaders to format the date in the error message.
String reason = "Response header '" + LAST_MODIFIED + "'";
MatcherAssert.assertThat(reason, value, equalTo(expected));
}), expected);
HttpHeaders headers = new HttpHeaders();
headers.setDate("expected", secondLater);
assertIncorrectResponseHeader(spec -> spec.expectHeader().valueEqualsDate(LAST_MODIFIED, secondLater), expected);
@@ -19,6 +19,7 @@ package org.springframework.test.web.servlet.samples.client.standalone.resultmat
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
@@ -71,7 +72,8 @@ class StatusAssertionTests {
@Test
void matcher() {
testClient.get().uri("/badRequest").exchange().expectStatus().value(equalTo(BAD_REQUEST.value()));
testClient.get().uri("/badRequest").exchange().expectStatus()
.value(status -> MatcherAssert.assertThat(status, equalTo(BAD_REQUEST.value())));
}
@@ -20,6 +20,7 @@ import java.io.IOException;
import java.time.Duration;
import java.util.Map;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -67,8 +68,9 @@ public class CookieAssertionsTests {
@Test
void value() {
assertions.value("foo", equalTo("bar"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.value("foo", equalTo("what?!")));
assertions.value("foo", v -> MatcherAssert.assertThat(v, equalTo("bar")));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.value("foo", v -> MatcherAssert.assertThat(v, equalTo("what?!"))));
}
@Test
@@ -96,9 +98,9 @@ public class CookieAssertionsTests {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.maxAge("foo", Duration.ofMinutes(29)));
assertions.maxAge("foo", equalTo(Duration.ofMinutes(30).getSeconds()));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.maxAge("foo", equalTo(Duration.ofMinutes(29).getSeconds())));
assertions.maxAge("foo", v -> MatcherAssert.assertThat(v, equalTo(Duration.ofMinutes(30).getSeconds())));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertions.maxAge("foo", v -> MatcherAssert.assertThat(v, equalTo(Duration.ofMinutes(29).getSeconds()))));
}
@Test
@@ -106,8 +108,9 @@ public class CookieAssertionsTests {
assertions.domain("foo", "foo.com");
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.domain("foo", "what.com"));
assertions.domain("foo", equalTo("foo.com"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.domain("foo", equalTo("what.com")));
assertions.domain("foo", v -> MatcherAssert.assertThat(v, equalTo("foo.com")));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertions.domain("foo", v -> MatcherAssert.assertThat(v, equalTo("what.com"))));
}
@Test
@@ -115,8 +118,9 @@ public class CookieAssertionsTests {
assertions.path("foo", "/foo");
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.path("foo", "/what"));
assertions.path("foo", equalTo("/foo"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.path("foo", equalTo("/what")));
assertions.path("foo", v -> MatcherAssert.assertThat(v, equalTo("/foo")));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertions.path("foo", v -> MatcherAssert.assertThat(v, equalTo("/what"))));
}
@Test
@@ -21,6 +21,7 @@ import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.concurrent.TimeUnit;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.springframework.http.CacheControl;
@@ -142,7 +143,7 @@ class HeaderAssertionTests {
headers.add("foo", "bar");
TestHeaderAssertions assertions = new TestHeaderAssertions(headers);
assertions.value("foo", containsString("a"));
assertions.value("foo", v -> MatcherAssert.assertThat(v, containsString("a")));
}
@Test
@@ -152,7 +153,7 @@ class HeaderAssertionTests {
headers.add("foo", "baz");
TestHeaderAssertions assertions = new TestHeaderAssertions(headers);
assertions.values("foo", hasItems("bar", "baz"));
assertions.values("foo", v -> MatcherAssert.assertThat(v, hasItems("bar", "baz")));
}
@Test
@@ -16,6 +16,7 @@
package org.springframework.test.web.support;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
@@ -141,16 +142,17 @@ class StatusAssertionTests {
TestStatusAssertions assertions = new TestStatusAssertions(HttpStatus.CONFLICT);
// Success
assertions.value(equalTo(409));
assertions.value(greaterThan(400));
assertions.value(v -> MatcherAssert.assertThat(v, equalTo(409)));
assertions.value(v -> MatcherAssert.assertThat(v, greaterThan(400)));
// Wrong status
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.value(equalTo(200)));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.value(v -> MatcherAssert.assertThat(v, equalTo(200))));
}
@Test
void matchesCustomStatusValue() {
new TestStatusAssertions(600).value(equalTo(600));
new TestStatusAssertions(600).value(v -> MatcherAssert.assertThat(v, equalTo(600)));
}
@Test