diff --git a/spring-test/src/main/java/org/springframework/test/json/JsonLoader.java b/spring-test/src/main/java/org/springframework/test/json/JsonLoader.java index 9a9e2c694d..fe905c000a 100644 --- a/spring-test/src/main/java/org/springframework/test/json/JsonLoader.java +++ b/spring-test/src/main/java/org/springframework/test/json/JsonLoader.java @@ -54,11 +54,11 @@ class JsonLoader { if (source == null) { return null; } - String string = source.toString(); - if (string.endsWith(".json")) { - return getJson(new ClassPathResource(string, this.resourceLoadClass)); + String jsonSource = source.toString(); + if (jsonSource.endsWith(".json")) { + return getJson(new ClassPathResource(jsonSource, this.resourceLoadClass)); } - return string; + return jsonSource; } String getJson(Resource source) { diff --git a/spring-test/src/main/java/org/springframework/test/web/UriAssert.java b/spring-test/src/main/java/org/springframework/test/web/UriAssert.java index 72fe4539fc..1a278591ec 100644 --- a/spring-test/src/main/java/org/springframework/test/web/UriAssert.java +++ b/spring-test/src/main/java/org/springframework/test/web/UriAssert.java @@ -69,8 +69,9 @@ public class UriAssert extends AbstractStringAssert { * assertThat(uri).matchPattern("/orders/*")); * * @param uriPattern the pattern that is expected to match + * @see AntPathMatcher */ - public UriAssert matchesPattern(String uriPattern) { + public UriAssert matchesAntPattern(String uriPattern) { Assertions.assertThat(pathMatcher.isPattern(uriPattern)) .withFailMessage("'%s' is not an Ant-style path pattern", uriPattern).isTrue(); Assertions.assertThat(pathMatcher.match(uriPattern, this.actual)) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractHttpServletRequestAssert.java b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractHttpServletRequestAssert.java index a93fb9e37b..8f74ca0d60 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractHttpServletRequestAssert.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractHttpServletRequestAssert.java @@ -104,7 +104,7 @@ public abstract class AbstractHttpServletRequestAssert assertThat("/orders/1").matchesPattern("/orders/")) + .isThrownBy(() -> assertThat("/orders/1").matchesAntPattern("/orders/")) .withMessage("'/orders/' is not an Ant-style path pattern"); } @Test - void matchesPatternWithWrongValue() { + void matchesAntPatternWithWrongValue() { assertThatExceptionOfType(AssertionError.class) - .isThrownBy(() -> assertThat("/orders/1").matchesPattern("/resources/*")) + .isThrownBy(() -> assertThat("/orders/1").matchesAntPattern("/resources/*")) .withMessageContainingAll("Test URI", "/resources/*", "/orders/1"); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletResponseAssertTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletResponseAssertTests.java index ec12ff879d..6c0de703ac 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletResponseAssertTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletResponseAssertTests.java @@ -18,13 +18,13 @@ package org.springframework.test.web.servlet.assertj; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import org.junit.jupiter.api.Test; import org.springframework.http.HttpHeaders; import org.springframework.mock.web.MockHttpServletResponse; -import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** @@ -34,12 +34,10 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; */ public class AbstractMockHttpServletResponseAssertTests { - private MockHttpServletResponse response = new MockHttpServletResponse(); - - @Test void hasForwardedUrl() { String forwardedUrl = "https://example.com/42"; + MockHttpServletResponse response = new MockHttpServletResponse(); response.setForwardedUrl(forwardedUrl); assertThat(response).hasForwardedUrl(forwardedUrl); } @@ -47,6 +45,7 @@ public class AbstractMockHttpServletResponseAssertTests { @Test void hasForwardedUrlWithWrongValue() { String forwardedUrl = "https://example.com/42"; + MockHttpServletResponse response = new MockHttpServletResponse(); response.setForwardedUrl(forwardedUrl); assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> assertThat(response).hasForwardedUrl("another")) @@ -56,6 +55,7 @@ public class AbstractMockHttpServletResponseAssertTests { @Test void hasRedirectedUrl() { String redirectedUrl = "https://example.com/42"; + MockHttpServletResponse response = new MockHttpServletResponse(); response.addHeader(HttpHeaders.LOCATION, redirectedUrl); assertThat(response).hasRedirectedUrl(redirectedUrl); } @@ -63,6 +63,7 @@ public class AbstractMockHttpServletResponseAssertTests { @Test void hasRedirectedUrlWithWrongValue() { String redirectedUrl = "https://example.com/42"; + MockHttpServletResponse response = new MockHttpServletResponse(); response.addHeader(HttpHeaders.LOCATION, redirectedUrl); assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> assertThat(response).hasRedirectedUrl("another")) @@ -71,15 +72,17 @@ public class AbstractMockHttpServletResponseAssertTests { @Test void bodyHasContent() throws UnsupportedEncodingException { + MockHttpServletResponse response = new MockHttpServletResponse(); response.getWriter().write("OK"); assertThat(response).body().asString().isEqualTo("OK"); } @Test void bodyHasContentWithResponseCharacterEncoding() throws UnsupportedEncodingException { - byte[] bytes = "OK".getBytes(UTF_8); + byte[] bytes = "OK".getBytes(StandardCharsets.UTF_8); + MockHttpServletResponse response = new MockHttpServletResponse(); response.getWriter().write("OK"); - response.setContentType(UTF_8.name()); + response.setContentType(StandardCharsets.UTF_8.name()); assertThat(response).body().isEqualTo(bytes); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/CookieMapAssertTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/CookieMapAssertTests.java index 300446cb35..4a00c438ac 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/CookieMapAssertTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/CookieMapAssertTests.java @@ -52,130 +52,130 @@ class CookieMapAssertTests { @Test void containsCookieWhenCookieExistsShouldPass() { - cookies().containsCookie("framework"); + assertThat(cookies()).containsCookie("framework"); } @Test void containsCookieWhenCookieMissingShouldFail() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - cookies().containsCookie("missing")); + assertThat(cookies()).containsCookie("missing")); } @Test void containsCookiesWhenCookiesExistShouldPass() { - cookies().containsCookies("framework", "age"); + assertThat(cookies()).containsCookies("framework", "age"); } @Test void containsCookiesWhenCookieMissingShouldFail() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - cookies().containsCookies("framework", "missing")); + assertThat(cookies()).containsCookies("framework", "missing")); } @Test void doesNotContainCookieWhenCookieMissingShouldPass() { - cookies().doesNotContainCookie("missing"); + assertThat(cookies()).doesNotContainCookie("missing"); } @Test void doesNotContainCookieWhenCookieExistsShouldFail() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - cookies().doesNotContainCookie("framework")); + assertThat(cookies()).doesNotContainCookie("framework")); } @Test void doesNotContainCookiesWhenCookiesMissingShouldPass() { - cookies().doesNotContainCookies("missing", "missing2"); + assertThat(cookies()).doesNotContainCookies("missing", "missing2"); } @Test void doesNotContainCookiesWhenAtLeastOneCookieExistShouldFail() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - cookies().doesNotContainCookies("missing", "framework")); + assertThat(cookies()).doesNotContainCookies("missing", "framework")); } @Test void hasValueEqualsWhenCookieValueMatchesShouldPass() { - cookies().hasValue("framework", "spring"); + assertThat(cookies()).hasValue("framework", "spring"); } @Test void hasValueEqualsWhenCookieValueDiffersShouldFail() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - cookies().hasValue("framework", "other")); + assertThat(cookies()).hasValue("framework", "other")); } @Test void hasCookieSatisfyingWhenCookieValueMatchesShouldPass() { - cookies().hasCookieSatisfying("framework", cookie -> + assertThat(cookies()).hasCookieSatisfying("framework", cookie -> assertThat(cookie.getValue()).startsWith("spr")); } @Test void hasCookieSatisfyingWhenCookieValueDiffersShouldFail() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - cookies().hasCookieSatisfying("framework", cookie -> + assertThat(cookies()).hasCookieSatisfying("framework", cookie -> assertThat(cookie.getValue()).startsWith("not"))); } @Test void hasMaxAgeWhenCookieAgeMatchesShouldPass() { - cookies().hasMaxAge("age", Duration.ofMinutes(20)); + assertThat(cookies()).hasMaxAge("age", Duration.ofMinutes(20)); } @Test void hasMaxAgeWhenCookieAgeDiffersShouldFail() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - cookies().hasMaxAge("age", Duration.ofMinutes(30))); + assertThat(cookies()).hasMaxAge("age", Duration.ofMinutes(30))); } @Test void pathWhenCookiePathMatchesShouldPass() { - cookies().hasPath("path", "/spring"); + assertThat(cookies()).hasPath("path", "/spring"); } @Test void pathWhenCookiePathDiffersShouldFail() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - cookies().hasPath("path", "/other")); + assertThat(cookies()).hasPath("path", "/other")); } @Test void hasDomainWhenCookieDomainMatchesShouldPass() { - cookies().hasDomain("domain", "spring.io"); + assertThat(cookies()).hasDomain("domain", "spring.io"); } @Test void hasDomainWhenCookieDomainDiffersShouldFail() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - cookies().hasDomain("domain", "example.org")); + assertThat(cookies()).hasDomain("domain", "example.org")); } @Test void isSecureWhenCookieSecureMatchesShouldPass() { - cookies().isSecure("framework", true); + assertThat(cookies()).isSecure("framework", true); } @Test void isSecureWhenCookieSecureDiffersShouldFail() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - cookies().isSecure("domain", true)); + assertThat(cookies()).isSecure("domain", true)); } @Test void isHttpOnlyWhenCookieHttpOnlyMatchesShouldPass() { - cookies().isHttpOnly("framework", true); + assertThat(cookies()).isHttpOnly("framework", true); } @Test void isHttpOnlyWhenCookieHttpOnlyDiffersShouldFail() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - cookies().isHttpOnly("domain", true)); + assertThat(cookies()).isHttpOnly("domain", true)); } - private static CookieMapAssert cookies() { - return assertThat((AssertProvider) () -> new CookieMapAssert(cookies)); + private static AssertProvider cookies() { + return () -> new CookieMapAssert(cookies); } } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/HandlerResultAssertTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/HandlerResultAssertTests.java index d9e36bdc4f..94532bbfba 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/HandlerResultAssertTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/HandlerResultAssertTests.java @@ -128,12 +128,12 @@ class HandlerResultAssertTests { static class TestController { @GetMapping("/greet") - public ResponseEntity greet() { + ResponseEntity greet() { return ResponseEntity.ok().body("Hello"); } @PostMapping("/update") - public void update() { + void update() { } } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/ResponseBodyAssertTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/ResponseBodyAssertTests.java index 02d65f6dad..0284636c3d 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/ResponseBodyAssertTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/assertj/ResponseBodyAssertTests.java @@ -19,6 +19,7 @@ package org.springframework.test.web.servlet.assertj; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.assertj.core.api.AssertProvider; import org.junit.jupiter.api.Test; @@ -26,7 +27,6 @@ import org.junit.jupiter.api.Test; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.json.JsonContent; -import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; /** @@ -40,8 +40,8 @@ class ResponseBodyAssertTests { @Test void isEqualToWithByteArray() { MockHttpServletResponse response = createResponse("hello"); - response.setCharacterEncoding(UTF_8.name()); - assertThat(fromResponse(response)).isEqualTo("hello".getBytes(UTF_8)); + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + assertThat(fromResponse(response)).isEqualTo("hello".getBytes(StandardCharsets.UTF_8)); } @Test