Polishing and consistent use of exception assertions

This commit is contained in:
Sam Brannen
2024-03-21 16:36:14 +01:00
parent 458c30cb63
commit a30c06b883
4 changed files with 109 additions and 119 deletions

View File

@@ -43,8 +43,9 @@ import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRe
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 @@ class RequestHeaderMethodArgumentResolverTests {
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 @@ class RequestHeaderMethodArgumentResolverTests {
Mono<Object> 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 @@ class RequestHeaderMethodArgumentResolverTests {
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 @@ class RequestHeaderMethodArgumentResolverTests {
Mono<Object> 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<Object> 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 @@ class RequestHeaderMethodArgumentResolverTests {
MockServerHttpRequest request = MockServerHttpRequest.get("/").header("bar", expected).build();
ServerWebExchange exchange = MockServerWebExchange.from(request);
System.setProperty("systemProperty", "bar");
try {
System.setProperty("systemProperty", "bar");
Mono<Object> 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 @@ class RequestHeaderMethodArgumentResolverTests {
MockServerHttpRequest request = MockServerHttpRequest.get("/").header("bar", expected).build();
ServerWebExchange exchange = MockServerWebExchange.from(request);
System.setProperty("systemProperty", "bar");
try {
System.setProperty("systemProperty", "bar");
Mono<Object> 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 @@ class RequestHeaderMethodArgumentResolverTests {
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
ServerWebExchange exchange = MockServerWebExchange.from(request);
System.setProperty("systemProperty", expected);
try {
System.setProperty("systemProperty", expected);
Mono<Object> 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 @@ class RequestHeaderMethodArgumentResolverTests {
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
ServerWebExchange exchange = MockServerWebExchange.from(request);
System.setProperty("systemProperty", expected);
try {
System.setProperty("systemProperty", expected);
Mono<Object> 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 @@ class RequestHeaderMethodArgumentResolverTests {
Mono<Object> 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 @@ class RequestHeaderMethodArgumentResolverTests {
Mono<Object> 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)));
}