From bed36890019a1764f1ed846ec1c94325a2ac5c72 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Wed, 10 Apr 2024 10:56:41 +0200 Subject: [PATCH] Polishing --- ...questParamMethodArgumentResolverTests.java | 76 +++++-------------- ...questParamMethodArgumentResolverTests.java | 19 ++--- 2 files changed, 27 insertions(+), 68 deletions(-) diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java index 629b723c39e0..d871942f24d7 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java @@ -22,7 +22,6 @@ import java.util.Optional; import jakarta.servlet.http.Part; -import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -54,6 +53,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.InstanceOfAssertFactories.LIST; +import static org.assertj.core.api.InstanceOfAssertFactories.array; +import static org.assertj.core.api.InstanceOfAssertFactories.optional; +import static org.assertj.core.api.InstanceOfAssertFactories.type; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.springframework.web.testfixture.method.MvcAnnotationPredicates.requestParam; @@ -84,6 +87,7 @@ void setup() { // Expose request to the current thread (for SpEL expressions) RequestContextHolder.setRequestAttributes(webRequest); + context.close(); } @Test @@ -169,9 +173,7 @@ void resolveString() throws Exception { MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof String; - assertThat(condition).isTrue(); - assertThat(result).as("Invalid result").isEqualTo(expected); + assertThat(result).isEqualTo(expected); } @Test @@ -181,9 +183,7 @@ void resolveStringArray() throws Exception { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof String[]; - assertThat(condition).isTrue(); - assertThat((String[]) result).as("Invalid result").isEqualTo(expected); + assertThat(result).asInstanceOf(array(String[].class)).containsExactly(expected); } @Test // gh-32577 @@ -194,9 +194,7 @@ void resolveStringArrayWithEmptyArraySuffix() throws Exception { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof String[]; - assertThat(condition).isTrue(); - assertThat((String[]) result).isEqualTo(expected); + assertThat(result).asInstanceOf(array(String[].class)).containsExactly(expected); } @Test @@ -208,9 +206,7 @@ void resolveMultipartFile() throws Exception { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof MultipartFile; - assertThat(condition).isTrue(); - assertThat(result).as("Invalid result").isEqualTo(expected); + assertThat(result).asInstanceOf(type(MultipartFile.class)).isEqualTo(expected); } @Test @@ -226,9 +222,7 @@ void resolveMultipartFileList() throws Exception { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof List; - assertThat(condition).isTrue(); - assertThat(result).isEqualTo(Arrays.asList(expected1, expected2)); + assertThat(result).asInstanceOf(LIST).containsExactly(expected1, expected2); } @Test @@ -255,12 +249,7 @@ void resolveMultipartFileArray() throws Exception { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof MultipartFile[]; - assertThat(condition).isTrue(); - MultipartFile[] parts = (MultipartFile[]) result; - assertThat(parts).hasSize(2); - assertThat(expected1).isEqualTo(parts[0]); - assertThat(expected2).isEqualTo(parts[1]); + assertThat(result).asInstanceOf(array(MultipartFile[].class)).containsExactly(expected1, expected2); } @Test @@ -286,9 +275,7 @@ void resolvePart() throws Exception { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof Part; - assertThat(condition).isTrue(); - assertThat(result).as("Invalid result").isEqualTo(expected); + assertThat(result).asInstanceOf(type(Part.class)).isEqualTo(expected); } @Test @@ -306,9 +293,7 @@ void resolvePartList() throws Exception { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof List; - assertThat(condition).isTrue(); - assertThat(result).isEqualTo(Arrays.asList(expected1, expected2)); + assertThat(result).asInstanceOf(LIST).containsExactly(expected1, expected2); } @Test @@ -339,12 +324,7 @@ void resolvePartArray() throws Exception { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof Part[]; - assertThat(condition).isTrue(); - Part[] parts = (Part[]) result; - assertThat(parts).hasSize(2); - assertThat(expected1).isEqualTo(parts[0]); - assertThat(expected2).isEqualTo(parts[1]); + assertThat(result).asInstanceOf(array(Part[].class)).containsExactly(expected1, expected2); } @Test @@ -369,9 +349,7 @@ void resolveMultipartFileNotAnnot() throws Exception { MethodParameter param = this.testMethod.annotNotPresent().arg(MultipartFile.class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof MultipartFile; - assertThat(condition).isTrue(); - assertThat(result).as("Invalid result").isEqualTo(expected); + assertThat(result).asInstanceOf(type(MultipartFile.class)).isEqualTo(expected); } @Test @@ -387,9 +365,7 @@ void resolveMultipartFileListNotannot() throws Exception { .annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof List; - assertThat(condition).isTrue(); - assertThat(result).isEqualTo(Arrays.asList(expected1, expected2)); + assertThat(result).asInstanceOf(LIST).containsExactly(expected1, expected2); } @Test @@ -411,8 +387,7 @@ public void isMultipartRequestHttpPut() throws Exception { .annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class); Object actual = resolver.resolveArgument(param, null, webRequest, null); - assertThat(actual).isInstanceOf(List.class).asInstanceOf(InstanceOfAssertFactories.LIST) - .containsExactly(expected); + assertThat(actual).asInstanceOf(LIST).containsExactly(expected); } @Test @@ -443,18 +418,14 @@ void resolvePartNotAnnot() throws Exception { MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof Part; - assertThat(condition).isTrue(); - assertThat(result).as("Invalid result").isEqualTo(expected); + assertThat(result).asInstanceOf(type(Part.class)).isEqualTo(expected); } @Test void resolveDefaultValue() throws Exception { MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof String; - assertThat(condition).isTrue(); - assertThat(result).as("Invalid result").isEqualTo("bar"); + assertThat(result).isEqualTo("bar"); } @Test @@ -529,8 +500,6 @@ void resolveSimpleTypeParam() throws Exception { MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); - boolean condition = result instanceof String; - assertThat(condition).isTrue(); assertThat(result).isEqualTo("plainValue"); } @@ -678,9 +647,7 @@ void resolveOptionalMultipartFile() throws Exception { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class); Object result = resolver.resolveArgument(param, null, webRequest, binderFactory); - boolean condition = result instanceof Optional; - assertThat(condition).isTrue(); - assertThat(((Optional) result).get()).as("Invalid result").isEqualTo(expected); + assertThat(result).asInstanceOf(optional(MultipartFile.class)).contains(expected); } @Test @@ -724,8 +691,7 @@ void resolveNameFromSystemPropertyThroughPlaceholder() throws Exception { try { MethodParameter param = this.testMethod.annot(requestParam().name("${systemProperty}")).arg(Integer.class); Object result = resolver.resolveArgument(param, null, webRequest, binderFactory); - boolean condition = result instanceof Integer; - assertThat(condition).isTrue(); + assertThat(result).isInstanceOf(Integer.class); } finally { System.clearProperty("systemProperty"); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverTests.java index c42bb55f1516..d9049540d2af 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverTests.java @@ -16,6 +16,7 @@ package org.springframework.web.reactive.result.method.annotation; + import java.time.Duration; import java.util.Map; import java.util.Optional; @@ -39,6 +40,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.assertj.core.api.InstanceOfAssertFactories.array; +import static org.assertj.core.api.InstanceOfAssertFactories.optional; import static org.springframework.core.ResolvableType.forClassWithGenerics; import static org.springframework.web.testfixture.method.MvcAnnotationPredicates.requestParam; @@ -58,7 +61,6 @@ class RequestParamMethodArgumentResolverTests { @BeforeEach void setup() throws Exception { - ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); this.resolver = new RequestParamMethodArgumentResolver(null, adapterRegistry, true); @@ -70,7 +72,6 @@ void setup() throws Exception { @Test void supportsParameter() { - MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class); assertThat(this.resolver.supportsParameter(param)).isTrue(); @@ -91,7 +92,6 @@ void supportsParameter() { param = this.testMethod.annot(requestParam().notRequired()).arg(String.class); assertThat(this.resolver.supportsParameter(param)).isTrue(); - } @Test @@ -125,9 +125,7 @@ void resolveStringArray() { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class); MockServerHttpRequest request = MockServerHttpRequest.get("/path?name=foo&name=bar").build(); Object result = resolve(param, MockServerWebExchange.from(request)); - boolean condition = result instanceof String[]; - assertThat(condition).isTrue(); - assertThat((String[]) result).isEqualTo(new String[] {"foo", "bar"}); + assertThat(result).asInstanceOf(array(String[].class)).containsExactly("foo", "bar"); } @Test // gh-32577 @@ -135,9 +133,7 @@ void resolveStringArrayWithEmptyArraySuffix() { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class); MockServerHttpRequest request = MockServerHttpRequest.get("/path?name[]=foo&name[]=bar").build(); Object result = resolve(param, MockServerWebExchange.from(request)); - boolean condition = result instanceof String[]; - assertThat(condition).isTrue(); - assertThat((String[]) result).isEqualTo(new String[] {"foo", "bar"}); + assertThat(result).asInstanceOf(array(String[].class)).containsExactly("foo", "bar"); } @Test @@ -214,10 +210,7 @@ void resolveOptionalParamValue() { exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path?name=123")); result = resolve(param, exchange); - assertThat(result.getClass()).isEqualTo(Optional.class); - Optional value = (Optional) result; - assertThat(value).isPresent(); - assertThat(value.get()).isEqualTo(123); + assertThat(result).asInstanceOf(optional(Integer.class)).contains(123); }