diff --git a/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/reactive/HeaderMethodArgumentResolverTests.java b/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/reactive/HeaderMethodArgumentResolverTests.java index bf6c8904f6d4..43f4260e66cf 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/reactive/HeaderMethodArgumentResolverTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/reactive/HeaderMethodArgumentResolverTests.java @@ -36,12 +36,13 @@ 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.Assertions.assertThatIllegalStateException; import static org.springframework.messaging.handler.annotation.MessagingPredicates.header; import static org.springframework.messaging.handler.annotation.MessagingPredicates.headerPlain; /** * Test fixture for {@link HeaderMethodArgumentResolver} tests. + * * @author Rossen Stoyanchev */ class HeaderMethodArgumentResolverTests { @@ -110,8 +111,8 @@ void resolveArgumentDefaultValue() { @Test void resolveDefaultValueSystemProperty() { - System.setProperty("systemProperty", "sysbar"); try { + System.setProperty("systemProperty", "sysbar"); Message message = MessageBuilder.withPayload(new byte[0]).build(); MethodParameter param = this.resolvable.annot(header("name", "#{systemProperties.systemProperty}")).arg(); Object result = resolveArgument(param, message); @@ -124,8 +125,8 @@ void resolveDefaultValueSystemProperty() { @Test void resolveNameFromSystemProperty() { - System.setProperty("systemProperty", "sysbar"); try { + System.setProperty("systemProperty", "sysbar"); Message message = MessageBuilder.withPayload(new byte[0]).setHeader("sysbar", "foo").build(); MethodParameter param = this.resolvable.annot(header("#{systemProperties.systemProperty}")).arg(); Object result = resolveArgument(param, message); @@ -154,32 +155,36 @@ void resolveOptionalHeaderAsEmpty() { @Test void missingParameterFromSystemPropertyThroughPlaceholder() { - String expected = "sysbar"; - System.setProperty("systemProperty", expected); - Message message = MessageBuilder.withPayload(new byte[0]).build(); - MethodParameter param = this.resolvable.annot(header("#{systemProperties.systemProperty}")).arg(); - - assertThatThrownBy(() -> - resolveArgument(param, message)) - .isInstanceOf(MessageHandlingException.class) - .hasMessageContaining(expected); + try { + String expected = "sysbar"; + System.setProperty("systemProperty", expected); + Message message = MessageBuilder.withPayload(new byte[0]).build(); + MethodParameter param = this.resolvable.annot(header("#{systemProperties.systemProperty}")).arg(); - System.clearProperty("systemProperty"); + assertThatExceptionOfType(MessageHandlingException.class) + .isThrownBy(() -> resolveArgument(param, message)) + .withMessageContaining(expected); + } + finally { + System.clearProperty("systemProperty"); + } } @Test void notNullablePrimitiveParameterFromSystemPropertyThroughPlaceholder() { - String expected = "sysbar"; - System.setProperty("systemProperty", expected); - Message message = MessageBuilder.withPayload(new byte[0]).build(); - MethodParameter param = this.resolvable.annot(header("${systemProperty}").required(false)).arg(); - - assertThatThrownBy(() -> - resolver.resolveArgument(param, message)) - .isInstanceOf(IllegalStateException.class) - .hasMessageContaining(expected); + try { + String expected = "sysbar"; + System.setProperty("systemProperty", expected); + Message message = MessageBuilder.withPayload(new byte[0]).build(); + MethodParameter param = this.resolvable.annot(header("${systemProperty}").required(false)).arg(); - System.clearProperty("systemProperty"); + assertThatIllegalStateException() + .isThrownBy(() -> resolver.resolveArgument(param, message)) + .withMessageContaining(expected); + } + finally { + System.clearProperty("systemProperty"); + } } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java b/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java index ed4336b4c66b..7c0271f45a3a 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java @@ -35,7 +35,7 @@ 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.Assertions.assertThatIllegalStateException; import static org.springframework.messaging.handler.annotation.MessagingPredicates.header; import static org.springframework.messaging.handler.annotation.MessagingPredicates.headerPlain; @@ -112,8 +112,8 @@ void resolveArgumentDefaultValue() throws Exception { @Test void resolveDefaultValueSystemProperty() throws Exception { - System.setProperty("systemProperty", "sysbar"); try { + System.setProperty("systemProperty", "sysbar"); Message message = MessageBuilder.withPayload(new byte[0]).build(); MethodParameter param = this.resolvable.annot(header("name", "#{systemProperties.systemProperty}")).arg(); Object result = resolver.resolveArgument(param, message); @@ -126,8 +126,8 @@ void resolveDefaultValueSystemProperty() throws Exception { @Test void resolveNameFromSystemProperty() throws Exception { - System.setProperty("systemProperty", "sysbar"); try { + System.setProperty("systemProperty", "sysbar"); Message message = MessageBuilder.withPayload(new byte[0]).setHeader("sysbar", "foo").build(); MethodParameter param = this.resolvable.annot(header("#{systemProperties.systemProperty}")).arg(); Object result = resolver.resolveArgument(param, message); @@ -140,32 +140,36 @@ void resolveNameFromSystemProperty() throws Exception { @Test void missingParameterFromSystemPropertyThroughPlaceholder() { - String expected = "sysbar"; - System.setProperty("systemProperty", expected); - Message message = MessageBuilder.withPayload(new byte[0]).build(); - MethodParameter param = this.resolvable.annot(header("#{systemProperties.systemProperty}")).arg(); - - assertThatThrownBy(() -> - resolver.resolveArgument(param, message)) - .isInstanceOf(MessageHandlingException.class) - .hasMessageContaining(expected); + try { + String expected = "sysbar"; + System.setProperty("systemProperty", expected); + Message message = MessageBuilder.withPayload(new byte[0]).build(); + MethodParameter param = this.resolvable.annot(header("#{systemProperties.systemProperty}")).arg(); - System.clearProperty("systemProperty"); + assertThatExceptionOfType(MessageHandlingException.class) + .isThrownBy(() -> resolver.resolveArgument(param, message)) + .withMessageContaining(expected); + } + finally { + System.clearProperty("systemProperty"); + } } @Test void notNullablePrimitiveParameterFromSystemPropertyThroughPlaceholder() { - String expected = "sysbar"; - System.setProperty("systemProperty", expected); - Message message = MessageBuilder.withPayload(new byte[0]).build(); - MethodParameter param = this.resolvable.annot(header("${systemProperty}").required(false)).arg(); - - assertThatThrownBy(() -> - resolver.resolveArgument(param, message)) - .isInstanceOf(IllegalStateException.class) - .hasMessageContaining(expected); + try { + String expected = "sysbar"; + System.setProperty("systemProperty", expected); + Message message = MessageBuilder.withPayload(new byte[0]).build(); + MethodParameter param = this.resolvable.annot(header("${systemProperty}").required(false)).arg(); - System.clearProperty("systemProperty"); + assertThatIllegalStateException() + .isThrownBy(() -> resolver.resolveArgument(param, message)) + .withMessageContaining(expected); + } + finally { + System.clearProperty("systemProperty"); + } } @Test diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java index 41f4e9551602..659296a14780 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java @@ -45,7 +45,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Tests for {@link RequestHeaderMethodArgumentResolver}. @@ -132,7 +131,6 @@ void resolveStringArrayArgument() throws Exception { servletRequest.addHeader("name", expected); Object result = resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null); - assertThat(result).isInstanceOf(String[].class); assertThat(result).isEqualTo(expected); } @@ -145,8 +143,8 @@ void resolveDefaultValue() throws Exception { @Test void resolveDefaultValueFromSystemProperty() throws Exception { - System.setProperty("systemProperty", "bar"); try { + System.setProperty("systemProperty", "bar"); Object result = resolver.resolveArgument(paramSystemProperty, null, webRequest, null); assertThat(result).isEqualTo("bar"); @@ -161,8 +159,8 @@ void resolveNameFromSystemPropertyThroughExpression() throws Exception { String expected = "foo"; servletRequest.addHeader("bar", expected); - System.setProperty("systemProperty", "bar"); try { + System.setProperty("systemProperty", "bar"); Object result = resolver.resolveArgument(paramResolvedNameWithExpression, null, webRequest, null); assertThat(result).isEqualTo(expected); @@ -177,8 +175,8 @@ void resolveNameFromSystemPropertyThroughPlaceholder() throws Exception { String expected = "foo"; servletRequest.addHeader("bar", expected); - System.setProperty("systemProperty", "bar"); try { + System.setProperty("systemProperty", "bar"); Object result = resolver.resolveArgument(paramResolvedNameWithPlaceholder, null, webRequest, null); assertThat(result).isEqualTo(expected); @@ -190,16 +188,17 @@ void resolveNameFromSystemPropertyThroughPlaceholder() throws Exception { @Test void missingParameterFromSystemPropertyThroughPlaceholder() { - String expected = "bar"; - - System.setProperty("systemProperty", expected); - - assertThatThrownBy(() -> - resolver.resolveArgument(paramResolvedNameWithPlaceholder, null, webRequest, null)) - .isInstanceOf(MissingRequestHeaderException.class) - .extracting("headerName").isEqualTo(expected); + try { + String expected = "bar"; + System.setProperty("systemProperty", expected); - System.clearProperty("systemProperty"); + assertThatExceptionOfType(MissingRequestHeaderException.class) + .isThrownBy(() -> resolver.resolveArgument(paramResolvedNameWithPlaceholder, null, webRequest, null)) + .extracting("headerName").isEqualTo(expected); + } + finally { + System.clearProperty("systemProperty"); + } } @Test @@ -263,10 +262,10 @@ void uuidConversionWithInvalidValue() { ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer(); bindingInitializer.setConversionService(new DefaultFormattingConversionService()); + DefaultDataBinderFactory binderFactory = new DefaultDataBinderFactory(bindingInitializer); - assertThatThrownBy(() -> - resolver.resolveArgument(paramUuid, null, webRequest, new DefaultDataBinderFactory(bindingInitializer))) - .isInstanceOf(MethodArgumentTypeMismatchException.class) + assertThatExceptionOfType(MethodArgumentTypeMismatchException.class) + .isThrownBy(() -> resolver.resolveArgument(paramUuid, null, webRequest, binderFactory)) .extracting("propertyName").isEqualTo("name"); } @@ -285,10 +284,10 @@ private void uuidConversionWithEmptyOrBlankValue(String uuid) { ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer(); bindingInitializer.setConversionService(new DefaultFormattingConversionService()); + DefaultDataBinderFactory binderFactory = new DefaultDataBinderFactory(bindingInitializer); - assertThatExceptionOfType(MissingRequestHeaderException.class).isThrownBy(() -> - resolver.resolveArgument(paramUuid, null, webRequest, - new DefaultDataBinderFactory(bindingInitializer))); + assertThatExceptionOfType(MissingRequestHeaderException.class) + .isThrownBy(() -> resolver.resolveArgument(paramUuid, null, webRequest, binderFactory)); } @Test @@ -314,21 +313,23 @@ private void uuidConversionWithEmptyOrBlankValueOptional(String uuid) throws Exc @Test public void uuidPlaceholderConversionWithEmptyValue() { - String expected = "name"; - servletRequest.addHeader(expected, ""); - - System.setProperty("systemProperty", expected); + try { + String expected = "name"; + servletRequest.addHeader(expected, ""); - ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer(); - bindingInitializer.setConversionService(new DefaultFormattingConversionService()); + System.setProperty("systemProperty", expected); - assertThatThrownBy(() -> - resolver.resolveArgument(paramUuidPlaceholder, null, webRequest, - new DefaultDataBinderFactory(bindingInitializer))) - .isInstanceOf(MissingRequestHeaderException.class) - .extracting("headerName").isEqualTo(expected); + ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer(); + bindingInitializer.setConversionService(new DefaultFormattingConversionService()); + DefaultDataBinderFactory binderFactory = new DefaultDataBinderFactory(bindingInitializer); - System.clearProperty("systemProperty"); + assertThatExceptionOfType(MissingRequestHeaderException.class) + .isThrownBy(() -> resolver.resolveArgument(paramUuidPlaceholder, null, webRequest, binderFactory)) + .extracting("headerName").isEqualTo(expected); + } + finally { + System.clearProperty("systemProperty"); + } } void params( diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMethodArgumentResolverTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMethodArgumentResolverTests.java index 596dc36cd261..de8bd61b5eb6 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMethodArgumentResolverTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMethodArgumentResolverTests.java @@ -43,8 +43,9 @@ import org.springframework.web.testfixture.server.MockServerWebExchange; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.InstanceOfAssertFactories.ARRAY; /** * Tests for {@link RequestHeaderMethodArgumentResolver}. @@ -99,9 +100,9 @@ void supportsParameter() { assertThat(resolver.supportsParameter(paramNamedDefaultValueStringHeader)).as("String parameter not supported").isTrue(); assertThat(resolver.supportsParameter(paramNamedValueStringArray)).as("String array parameter not supported").isTrue(); assertThat(resolver.supportsParameter(paramNamedValueMap)).as("non-@RequestParam parameter supported").isFalse(); - assertThatIllegalStateException().isThrownBy(() -> - this.resolver.supportsParameter(this.paramMono)) - .withMessageStartingWith("RequestHeaderMethodArgumentResolver does not support reactive type wrapper"); + assertThatIllegalStateException() + .isThrownBy(() -> this.resolver.supportsParameter(this.paramMono)) + .withMessageStartingWith("RequestHeaderMethodArgumentResolver does not support reactive type wrapper"); } @Test @@ -112,10 +113,7 @@ void resolveStringArgument() { Mono mono = this.resolver.resolveArgument( this.paramNamedDefaultValueStringHeader, this.bindingContext, exchange); - Object result = mono.block(); - boolean condition = result instanceof String; - assertThat(condition).isTrue(); - assertThat(result).isEqualTo(expected); + assertThat(mono.block()).isEqualTo(expected); } @Test @@ -127,9 +125,7 @@ void resolveStringArrayArgument() { this.paramNamedValueStringArray, this.bindingContext, exchange); Object result = mono.block(); - boolean condition = result instanceof String[]; - assertThat(condition).isTrue(); - assertThat((String[]) result).isEqualTo(new String[] {"foo", "bar"}); + assertThat(result).asInstanceOf(ARRAY).containsExactly("foo", "bar"); } @Test @@ -138,24 +134,18 @@ void resolveDefaultValue() { Mono mono = this.resolver.resolveArgument( this.paramNamedDefaultValueStringHeader, this.bindingContext, exchange); - Object result = mono.block(); - boolean condition = result instanceof String; - assertThat(condition).isTrue(); - assertThat(result).isEqualTo("bar"); + assertThat(mono.block()).isEqualTo("bar"); } @Test void resolveDefaultValueFromSystemProperty() { - System.setProperty("systemProperty", "bar"); try { + System.setProperty("systemProperty", "bar"); Mono mono = this.resolver.resolveArgument( this.paramSystemProperty, this.bindingContext, MockServerWebExchange.from(MockServerHttpRequest.get("/"))); - Object result = mono.block(); - boolean condition = result instanceof String; - assertThat(condition).isTrue(); - assertThat(result).isEqualTo("bar"); + assertThat(mono.block()).isEqualTo("bar"); } finally { System.clearProperty("systemProperty"); @@ -168,15 +158,12 @@ void resolveNameFromSystemPropertyThroughExpression() { MockServerHttpRequest request = MockServerHttpRequest.get("/").header("bar", expected).build(); ServerWebExchange exchange = MockServerWebExchange.from(request); - System.setProperty("systemProperty", "bar"); try { + System.setProperty("systemProperty", "bar"); Mono mono = this.resolver.resolveArgument( this.paramResolvedNameWithExpression, this.bindingContext, exchange); - Object result = mono.block(); - boolean condition = result instanceof String; - assertThat(condition).isTrue(); - assertThat(result).isEqualTo(expected); + assertThat(mono.block()).isEqualTo(expected); } finally { System.clearProperty("systemProperty"); @@ -189,15 +176,12 @@ void resolveNameFromSystemPropertyThroughPlaceholder() { MockServerHttpRequest request = MockServerHttpRequest.get("/").header("bar", expected).build(); ServerWebExchange exchange = MockServerWebExchange.from(request); - System.setProperty("systemProperty", "bar"); try { + System.setProperty("systemProperty", "bar"); Mono mono = this.resolver.resolveArgument( this.paramResolvedNameWithPlaceholder, this.bindingContext, exchange); - Object result = mono.block(); - boolean condition = result instanceof String; - assertThat(condition).isTrue(); - assertThat(result).isEqualTo(expected); + assertThat(mono.block()).isEqualTo(expected); } finally { System.clearProperty("systemProperty"); @@ -210,13 +194,13 @@ void missingParameterFromSystemPropertyThroughPlaceholder() { MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); ServerWebExchange exchange = MockServerWebExchange.from(request); - System.setProperty("systemProperty", expected); try { + System.setProperty("systemProperty", expected); Mono mono = this.resolver.resolveArgument( this.paramResolvedNameWithExpression, this.bindingContext, exchange); - assertThatThrownBy(() -> mono.block()) - .isInstanceOf(MissingRequestValueException.class) + assertThatExceptionOfType(MissingRequestValueException.class) + .isThrownBy(() -> mono.block()) .extracting("name").isEqualTo(expected); } finally { @@ -230,14 +214,14 @@ void notNullablePrimitiveParameterFromSystemPropertyThroughPlaceholder() { MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); ServerWebExchange exchange = MockServerWebExchange.from(request); - System.setProperty("systemProperty", expected); try { + System.setProperty("systemProperty", expected); Mono mono = this.resolver.resolveArgument( this.primitivePlaceholderParam, this.bindingContext, exchange); - assertThatThrownBy(() -> mono.block()) - .isInstanceOf(IllegalStateException.class) - .hasMessageContaining(expected); + assertThatIllegalStateException() + .isThrownBy(() -> mono.block()) + .withMessageContaining(expected); } finally { System.clearProperty("systemProperty"); @@ -266,8 +250,6 @@ public void dateConversion() { Mono mono = this.resolver.resolveArgument(this.paramDate, this.bindingContext, exchange); Object result = mono.block(); - boolean condition = result instanceof Date; - assertThat(condition).isTrue(); assertThat(result).isEqualTo(new Date(rfc1123val)); } @@ -280,8 +262,6 @@ void instantConversion() { Mono mono = this.resolver.resolveArgument(this.paramInstant, this.bindingContext, exchange); Object result = mono.block(); - boolean condition = result instanceof Instant; - assertThat(condition).isTrue(); assertThat(result).isEqualTo(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val))); }