Migrate exception checking tests to use AssertJ

Migrate tests that use `@Test(expectedException=...)` or
`try...fail...catch` to use AssertJ's `assertThatException`
instead.
This commit is contained in:
Phillip Webb
2019-05-20 10:34:51 -07:00
parent fb26fc3f94
commit 02850f357f
561 changed files with 6592 additions and 10389 deletions

View File

@@ -25,6 +25,7 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.web.server.NotAcceptableStatusException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
/**
@@ -50,11 +51,12 @@ public class HeaderContentTypeResolverTests {
assertEquals("text/plain;q=0.5", mediaTypes.get(3).toString());
}
@Test(expected = NotAcceptableStatusException.class)
@Test
public void resolveMediaTypesParseError() throws Exception {
String header = "textplain; q=0.5";
this.resolver.resolveMediaTypes(
MockServerWebExchange.from(MockServerHttpRequest.get("/").header("accept", header)));
assertThatExceptionOfType(NotAcceptableStatusException.class).isThrownBy(() ->
this.resolver.resolveMediaTypes(
MockServerWebExchange.from(MockServerHttpRequest.get("/").header("accept", header))));
}
}

View File

@@ -27,6 +27,7 @@ import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.web.server.NotAcceptableStatusException;
import org.springframework.web.server.ServerWebExchange;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
/**
@@ -44,12 +45,11 @@ public class ParameterContentTypeResolverTests {
assertEquals(RequestedContentTypeResolver.MEDIA_TYPE_ALL_LIST, mediaTypes);
}
@Test(expected = NotAcceptableStatusException.class)
@Test
public void noMatchForKey() {
ParameterContentTypeResolver resolver = new ParameterContentTypeResolver(Collections.emptyMap());
List<MediaType> mediaTypes = resolver.resolveMediaTypes(createExchange("blah"));
assertEquals(0, mediaTypes.size());
assertThatExceptionOfType(NotAcceptableStatusException.class).isThrownBy(() ->
resolver.resolveMediaTypes(createExchange("blah")));
}
@Test

View File

@@ -63,10 +63,10 @@ import org.springframework.mock.http.client.reactive.test.MockClientHttpResponse
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.util.MultiValueMap;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT;
/**
@@ -434,12 +434,8 @@ public class BodyExtractorsTests {
})
.expectErrorSatisfies(throwable -> {
assertTrue(throwable instanceof UnsupportedMediaTypeException);
try {
buffer.release();
fail("releasing the buffer should have failed");
}
catch (IllegalReferenceCountException exc) {
}
assertThatExceptionOfType(IllegalReferenceCountException.class).isThrownBy(
buffer::release);
body.assertCancelled();
}).verify();
}

View File

@@ -46,9 +46,9 @@ import org.springframework.http.codec.HttpMessageReader;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
@@ -247,13 +247,8 @@ public class DefaultClientResponseTests {
ResponseEntity<String> result = defaultClientResponse.toEntity(String.class).block();
assertEquals("foo", result.getBody());
try {
result.getStatusCode();
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// do nothing
}
assertThatIllegalArgumentException().isThrownBy(
result::getStatusCode);
assertEquals(999, result.getStatusCodeValue());
assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
}
@@ -318,13 +313,8 @@ public class DefaultClientResponseTests {
ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block();
assertEquals(Collections.singletonList("foo"), result.getBody());
try {
result.getStatusCode();
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// do nothing
}
assertThatIllegalArgumentException().isThrownBy(
result::getStatusCode);
assertEquals(999, result.getStatusCodeValue());
assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
}

View File

@@ -34,6 +34,7 @@ import org.springframework.core.NamedThreadLocal;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@@ -179,12 +180,13 @@ public class DefaultWebClientTests {
assertEquals("bar", actual.get("foo"));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void bodyObjectPublisher() {
Mono<Void> mono = Mono.empty();
WebClient client = this.builder.build();
client.post().uri("https://example.com").syncBody(mono);
assertThatIllegalArgumentException().isThrownBy(() ->
client.post().uri("https://example.com").syncBody(mono));
}
@Test

View File

@@ -33,6 +33,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.BodyExtractors;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -115,12 +116,13 @@ public class ExchangeFilterFunctionsTests {
assertEquals(response, result);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void basicAuthenticationInvalidCharacters() {
ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
ExchangeFunction exchange = r -> Mono.just(mock(ClientResponse.class));
ExchangeFilterFunctions.basicAuthentication("foo", "\ud83d\udca9").filter(request, exchange);
assertThatIllegalArgumentException().isThrownBy(() ->
ExchangeFilterFunctions.basicAuthentication("foo", "\ud83d\udca9").filter(request, exchange));
}
@Test

View File

@@ -56,6 +56,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebInputException;
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
@@ -157,7 +158,7 @@ public class DefaultServerRequestTests {
}
@Test(expected = IllegalArgumentException.class)
@Test
public void pathVariableNotFound() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("https://example.com"));
Map<String, String> pathVariables = Collections.singletonMap("foo", "bar");
@@ -165,7 +166,8 @@ public class DefaultServerRequestTests {
DefaultServerRequest request = new DefaultServerRequest(exchange, messageReaders);
request.pathVariable("baz");
assertThatIllegalArgumentException().isThrownBy(() ->
request.pathVariable("baz"));
}
@Test

View File

@@ -44,6 +44,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.result.view.ViewResolver;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -355,11 +356,12 @@ public class DefaultServerResponseBuilderTests {
StepVerifier.create(response.getBody()).expectComplete().verify();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void bodyObjectPublisher() {
Mono<Void> mono = Mono.empty();
ServerResponse.ok().syncBody(mono);
assertThatIllegalArgumentException().isThrownBy(() ->
ServerResponse.ok().syncBody(mono));
}
@Test

View File

@@ -19,6 +19,7 @@ package org.springframework.web.reactive.resource;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@@ -43,9 +44,10 @@ public class FixedVersionStrategyTests {
}
@Test(expected = IllegalArgumentException.class)
@Test
public void emptyPrefixVersion() {
new FixedVersionStrategy(" ");
assertThatIllegalArgumentException().isThrownBy(() ->
new FixedVersionStrategy(" "));
}
@Test

View File

@@ -55,6 +55,8 @@ import org.springframework.web.server.MethodNotAllowedException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
@@ -453,17 +455,19 @@ public class ResourceWebHandlerTests {
}).verify(TIMEOUT);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void noPathWithinHandlerMappingAttribute() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
this.handler.handle(exchange).block(TIMEOUT);
assertThatIllegalArgumentException().isThrownBy(() ->
this.handler.handle(exchange).block(TIMEOUT));
}
@Test(expected = MethodNotAllowedException.class)
@Test
public void unsupportedHttpMethod() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post(""));
setPathWithinHandlerMapping(exchange, "foo.css");
this.handler.handle(exchange).block(TIMEOUT);
assertThatExceptionOfType(MethodNotAllowedException.class).isThrownBy(() ->
this.handler.handle(exchange).block(TIMEOUT));
}
@Test

View File

@@ -23,6 +23,7 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.web.bind.annotation.RequestMethod;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
@@ -74,11 +75,12 @@ public class CompositeRequestConditionTests {
assertSame(notEmpty, empty.combine(notEmpty));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void combineDifferentLength() {
CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1);
CompositeRequestCondition cond2 = new CompositeRequestCondition(this.param1, this.header1);
cond1.combine(cond2);
assertThatIllegalArgumentException().isThrownBy(() ->
cond1.combine(cond2));
}
@Test
@@ -128,11 +130,12 @@ public class CompositeRequestConditionTests {
assertEquals(1, empty.compareTo(notEmpty, exchange));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void compareDifferentLength() {
CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1);
CompositeRequestCondition cond2 = new CompositeRequestCondition(this.param1, this.header1);
cond1.compareTo(cond2, MockServerWebExchange.from(MockServerHttpRequest.get("/")));
assertThatIllegalArgumentException().isThrownBy(() ->
cond1.compareTo(cond2, MockServerWebExchange.from(MockServerHttpRequest.get("/"))));
}

View File

@@ -22,6 +22,7 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.web.bind.annotation.RequestMethod;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@@ -56,11 +57,12 @@ public class RequestConditionHolderTests {
assertSame(notEmpty, empty.combine(notEmpty));
}
@Test(expected = ClassCastException.class)
@Test
public void combineIncompatible() {
RequestConditionHolder params = new RequestConditionHolder(new ParamsRequestCondition("name"));
RequestConditionHolder headers = new RequestConditionHolder(new HeadersRequestCondition("name"));
params.combine(headers);
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() ->
params.combine(headers));
}
@Test
@@ -108,11 +110,12 @@ public class RequestConditionHolderTests {
assertEquals(1, empty.compareTo(notEmpty, this.exchange));
}
@Test(expected = ClassCastException.class)
@Test
public void compareIncompatible() {
RequestConditionHolder params = new RequestConditionHolder(new ParamsRequestCondition("name"));
RequestConditionHolder headers = new RequestConditionHolder(new HeadersRequestCondition("name"));
params.compareTo(headers, this.exchange);
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() ->
params.compareTo(headers, this.exchange));
}

View File

@@ -35,6 +35,7 @@ import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -66,10 +67,11 @@ public class HandlerMethodMappingTests {
}
@Test(expected = IllegalStateException.class)
@Test
public void registerDuplicates() {
this.mapping.registerMapping("foo", this.handler, this.method1);
this.mapping.registerMapping("foo", this.handler, this.method2);
assertThatIllegalStateException().isThrownBy(() ->
this.mapping.registerMapping("foo", this.handler, this.method2));
}
@Test

View File

@@ -42,13 +42,12 @@ import org.springframework.web.reactive.HandlerResult;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -96,15 +95,9 @@ public class InvocableHandlerMethodTests {
public void cannotResolveArg() {
Method method = ResolvableMethod.on(TestController.class).mockCall(o -> o.singleArg(null)).method();
Mono<HandlerResult> mono = invoke(new TestController(), method);
try {
mono.block();
fail("Expected IllegalStateException");
}
catch (IllegalStateException ex) {
assertThat(ex.getMessage(), is("Could not resolve parameter [0] in " +
method.toGenericString() + ": No suitable resolver"));
}
assertThatIllegalStateException().isThrownBy(
mono::block)
.withMessage("Could not resolve parameter [0] in " + method.toGenericString() + ": No suitable resolver");
}
@Test
@@ -130,13 +123,9 @@ public class InvocableHandlerMethodTests {
Method method = ResolvableMethod.on(TestController.class).mockCall(o -> o.singleArg(null)).method();
Mono<HandlerResult> mono = invoke(new TestController(), method);
try {
mono.block();
fail("Expected UnsupportedMediaTypeStatusException");
}
catch (UnsupportedMediaTypeStatusException ex) {
assertThat(ex.getMessage(), is("415 UNSUPPORTED_MEDIA_TYPE \"boo\""));
}
assertThatExceptionOfType(UnsupportedMediaTypeStatusException.class).isThrownBy(
mono::block)
.withMessage("415 UNSUPPORTED_MEDIA_TYPE \"boo\"");
}
@Test
@@ -144,19 +133,13 @@ public class InvocableHandlerMethodTests {
this.resolvers.add(stubResolver(1));
Method method = ResolvableMethod.on(TestController.class).mockCall(o -> o.singleArg(null)).method();
Mono<HandlerResult> mono = invoke(new TestController(), method);
try {
mono.block();
fail("Expected IllegalStateException");
}
catch (IllegalStateException ex) {
assertNotNull("Exception not wrapped", ex.getCause());
assertTrue(ex.getCause() instanceof IllegalArgumentException);
assertTrue(ex.getMessage().contains("Controller ["));
assertTrue(ex.getMessage().contains("Method ["));
assertTrue(ex.getMessage().contains("with argument values:"));
assertTrue(ex.getMessage().contains("[0] [type=java.lang.Integer] [value=1]"));
}
assertThatIllegalStateException().isThrownBy(
mono::block)
.withCauseInstanceOf(IllegalArgumentException.class)
.withMessageContaining("Controller [")
.withMessageContaining("Method [")
.withMessageContaining("with argument values:")
.withMessageContaining("[0] [type=java.lang.Integer] [value=1]");
}
@Test
@@ -164,13 +147,9 @@ public class InvocableHandlerMethodTests {
Method method = ResolvableMethod.on(TestController.class).mockCall(TestController::exceptionMethod).method();
Mono<HandlerResult> mono = invoke(new TestController(), method);
try {
mono.block();
fail("Expected IllegalStateException");
}
catch (IllegalStateException ex) {
assertThat(ex.getMessage(), is("boo"));
}
assertThatIllegalStateException().isThrownBy(
mono::block)
.withMessage("boo");
}
@Test

View File

@@ -35,10 +35,10 @@ import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.reactive.BindingContext;
import org.springframework.web.server.ServerWebInputException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Test fixture with {@link CookieValueMethodArgumentResolver}.
@@ -84,15 +84,9 @@ public class CookieValueMethodArgumentResolverTests {
@Test
public void doesNotSupportParameter() {
assertFalse(this.resolver.supportsParameter(this.stringParameter));
try {
this.resolver.supportsParameter(this.cookieMonoParameter);
fail();
}
catch (IllegalStateException ex) {
assertTrue("Unexpected error message:\n" + ex.getMessage(),
ex.getMessage().startsWith(
"CookieValueMethodArgumentResolver does not support reactive type wrapper"));
}
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.supportsParameter(this.cookieMonoParameter))
.withMessageStartingWith("CookieValueMethodArgumentResolver does not support reactive type wrapper");
}
@Test

View File

@@ -31,10 +31,10 @@ import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.reactive.BindingContext;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link ExpressionValueMethodArgumentResolver}.
@@ -75,15 +75,9 @@ public class ExpressionValueMethodArgumentResolverTests {
@Test
public void doesNotSupport() {
assertFalse(this.resolver.supportsParameter(this.paramNotSupported));
try {
this.resolver.supportsParameter(this.paramAlsoNotSupported);
fail();
}
catch (IllegalStateException ex) {
assertTrue("Unexpected error message:\n" + ex.getMessage(),
ex.getMessage().startsWith(
"ExpressionValueMethodArgumentResolver does not support reactive type wrapper"));
}
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.supportsParameter(this.paramAlsoNotSupported))
.withMessageStartingWith("ExpressionValueMethodArgumentResolver does not support reactive type wrapper");
}
@Test

View File

@@ -36,11 +36,12 @@ import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurationSupport;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
/**
*
@@ -88,13 +89,9 @@ public class GlobalCorsConfigIntegrationTests extends AbstractRequestMappingInte
@Test
public void actualRequestWithCorsRejected() throws Exception {
try {
performGet("/cors-restricted", this.headers, String.class);
fail();
}
catch (HttpClientErrorException e) {
assertEquals(HttpStatus.FORBIDDEN, e.getStatusCode());
}
assertThatExceptionOfType(HttpClientErrorException.class).isThrownBy(() ->
performGet("/cors-restricted", this.headers, String.class))
.satisfies(ex -> assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN));
}
@Test
@@ -125,26 +122,18 @@ public class GlobalCorsConfigIntegrationTests extends AbstractRequestMappingInte
@Test
public void preFlightRequestWithCorsRejected() throws Exception {
try {
this.headers.add(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
performOptions("/cors-restricted", this.headers, String.class);
fail();
}
catch (HttpClientErrorException e) {
assertEquals(HttpStatus.FORBIDDEN, e.getStatusCode());
}
this.headers.add(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
assertThatExceptionOfType(HttpClientErrorException.class).isThrownBy(() ->
performOptions("/cors-restricted", this.headers, String.class))
.satisfies(ex -> assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN));
}
@Test
public void preFlightRequestWithoutCorsEnabled() throws Exception {
try {
this.headers.add(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
performOptions("/welcome", this.headers, String.class);
fail();
}
catch (HttpClientErrorException e) {
assertEquals(HttpStatus.FORBIDDEN, e.getStatusCode());
}
this.headers.add(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
assertThatExceptionOfType(HttpClientErrorException.class).isThrownBy(() ->
performOptions("/welcome", this.headers, String.class))
.satisfies(ex -> assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN));
}
@Test

View File

@@ -47,12 +47,12 @@ import org.springframework.web.reactive.BindingContext;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.core.ResolvableType.forClassWithGenerics;
import static org.springframework.http.MediaType.TEXT_PLAIN;
import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.post;
@@ -102,15 +102,9 @@ public class HttpEntityMethodArgumentResolverTests {
public void doesNotSupport() {
assertFalse(this.resolver.supportsParameter(this.testMethod.arg(Mono.class, String.class)));
assertFalse(this.resolver.supportsParameter(this.testMethod.arg(String.class)));
try {
this.resolver.supportsParameter(this.testMethod.arg(Mono.class, httpEntityType(String.class)));
fail();
}
catch (IllegalStateException ex) {
assertTrue("Unexpected error message:\n" + ex.getMessage(),
ex.getMessage().startsWith(
"HttpEntityMethodArgumentResolver does not support reactive type wrapper"));
}
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.supportsParameter(this.testMethod.arg(Mono.class, httpEntityType(String.class))))
.withMessageStartingWith("HttpEntityMethodArgumentResolver does not support reactive type wrapper");
}
@Test

View File

@@ -37,6 +37,7 @@ import org.springframework.web.reactive.BindingContext;
import org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver;
import org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@@ -104,11 +105,12 @@ public class InitBinderBindingContextTests {
assertNull(dataBinder.getDisallowedFields());
}
@Test(expected = IllegalStateException.class)
@Test
public void returnValueNotExpected() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/"));
BindingContext context = createBindingContext("initBinderReturnValue", WebDataBinder.class);
context.createDataBinder(exchange, null, "invalidName");
assertThatIllegalStateException().isThrownBy(() ->
context.createDataBinder(exchange, null, "invalidName"));
}
@Test

View File

@@ -37,6 +37,7 @@ import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.server.ServerErrorException;
import org.springframework.web.server.ServerWebInputException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -104,19 +105,21 @@ public class MatrixVariablesMethodArgumentResolverTests {
assertEquals(2013, actual);
}
@Test(expected = ServerErrorException.class)
@Test
public void resolveArgumentMultipleMatches() throws Exception {
getVariablesFor("var1").add("colors", "red");
getVariablesFor("var2").add("colors", "green");
MethodParameter param = this.testMethod.annot(matrixAttribute().noName()).arg(List.class, String.class);
this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(Duration.ZERO);
assertThatExceptionOfType(ServerErrorException.class).isThrownBy(() ->
this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(Duration.ZERO));
}
@Test(expected = ServerWebInputException.class)
@Test
public void resolveArgumentRequired() throws Exception {
MethodParameter param = this.testMethod.annot(matrixAttribute().noName()).arg(List.class, String.class);
this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(Duration.ZERO);
assertThatExceptionOfType(ServerWebInputException.class).isThrownBy(() ->
this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(Duration.ZERO));
}
@Test

View File

@@ -53,9 +53,9 @@ import org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
/**
@@ -174,13 +174,9 @@ public class ModelInitializerTests {
Method method = ResolvableMethod.on(TestController.class).annotPresent(PostMapping.class).resolveMethod();
HandlerMethod handlerMethod = new HandlerMethod(controller, method);
try {
this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(Duration.ofMillis(5000));
fail();
}
catch (IllegalArgumentException ex) {
assertEquals("Required attribute 'missing-bean' is missing.", ex.getMessage());
}
assertThatIllegalArgumentException().isThrownBy(() ->
this.modelInitializer.initModel(handlerMethod, context, this.exchange).block(Duration.ofMillis(5000)))
.withMessage("Required attribute 'missing-bean' is missing.");
}
@Test

View File

@@ -34,10 +34,10 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.reactive.BindingContext;
import org.springframework.web.reactive.HandlerMapping;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link PathVariableMapMethodArgumentResolver}.
@@ -73,15 +73,9 @@ public class PathVariableMapMethodArgumentResolverTests {
assertTrue(resolver.supportsParameter(paramMap));
assertFalse(resolver.supportsParameter(paramNamedMap));
assertFalse(resolver.supportsParameter(paramMapNoAnnot));
try {
this.resolver.supportsParameter(this.paramMonoMap);
fail();
}
catch (IllegalStateException ex) {
assertTrue("Unexpected error message:\n" + ex.getMessage(),
ex.getMessage().startsWith(
"PathVariableMapMethodArgumentResolver does not support reactive type wrapper"));
}
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.supportsParameter(this.paramMonoMap))
.withMessageStartingWith("PathVariableMapMethodArgumentResolver does not support reactive type wrapper");
}
@Test

View File

@@ -39,10 +39,10 @@ import org.springframework.web.reactive.BindingContext;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.server.ServerErrorException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link PathVariableMethodArgumentResolver}.
@@ -80,15 +80,9 @@ public class PathVariableMethodArgumentResolverTests {
public void supportsParameter() {
assertTrue(this.resolver.supportsParameter(this.paramNamedString));
assertFalse(this.resolver.supportsParameter(this.paramString));
try {
this.resolver.supportsParameter(this.paramMono);
fail();
}
catch (IllegalStateException ex) {
assertTrue("Unexpected error message:\n" + ex.getMessage(),
ex.getMessage().startsWith(
"PathVariableMethodArgumentResolver does not support reactive type wrapper"));
}
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.supportsParameter(this.paramMono))
.withMessageStartingWith("PathVariableMethodArgumentResolver does not support reactive type wrapper");
}
@Test

View File

@@ -45,6 +45,7 @@ import org.springframework.web.reactive.BindingContext;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -94,10 +95,11 @@ public class RequestBodyMethodArgumentResolverTests {
assertEquals(body, value);
}
@Test(expected = ServerWebInputException.class)
@Test
public void emptyBodyWithString() {
MethodParameter param = this.testMethod.annot(requestBody()).arg(String.class);
resolveValueWithEmptyBody(param);
assertThatExceptionOfType(ServerWebInputException.class).isThrownBy(() ->
resolveValueWithEmptyBody(param));
}
@Test

View File

@@ -35,10 +35,10 @@ import org.springframework.util.MultiValueMap;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.RequestHeader;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link RequestHeaderMapMethodArgumentResolver}.
@@ -76,15 +76,9 @@ public class RequestHeaderMapMethodArgumentResolverTests {
assertTrue("MultiValueMap parameter not supported", resolver.supportsParameter(paramMultiValueMap));
assertTrue("HttpHeaders parameter not supported", resolver.supportsParameter(paramHttpHeaders));
assertFalse("non-@RequestParam map supported", resolver.supportsParameter(paramUnsupported));
try {
this.resolver.supportsParameter(this.paramAlsoUnsupported);
fail();
}
catch (IllegalStateException ex) {
assertTrue("Unexpected error message:\n" + ex.getMessage(),
ex.getMessage().startsWith(
"RequestHeaderMapMethodArgumentResolver does not support reactive type wrapper"));
}
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.supportsParameter(this.paramAlsoUnsupported))
.withMessageStartingWith("RequestHeaderMapMethodArgumentResolver does not support reactive type wrapper");
}
@Test

View File

@@ -41,11 +41,11 @@ import org.springframework.web.reactive.BindingContext;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link RequestHeaderMethodArgumentResolver}.
@@ -99,15 +99,9 @@ public class RequestHeaderMethodArgumentResolverTests {
assertTrue("String parameter not supported", resolver.supportsParameter(paramNamedDefaultValueStringHeader));
assertTrue("String array parameter not supported", resolver.supportsParameter(paramNamedValueStringArray));
assertFalse("non-@RequestParam parameter supported", resolver.supportsParameter(paramNamedValueMap));
try {
this.resolver.supportsParameter(this.paramMono);
fail();
}
catch (IllegalStateException ex) {
assertTrue("Unexpected error message:\n" + ex.getMessage(),
ex.getMessage().startsWith(
"RequestHeaderMethodArgumentResolver does not support reactive type wrapper"));
}
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.supportsParameter(this.paramMono))
.withMessageStartingWith("RequestHeaderMethodArgumentResolver does not support reactive type wrapper");
}
@Test

View File

@@ -37,10 +37,8 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.reactive.config.EnableWebFlux;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* {@code @RequestMapping} integration tests with exception handling scenarios.
@@ -81,30 +79,22 @@ public class RequestMappingExceptionHandlingIntegrationTests extends AbstractReq
@Test // SPR-16051
public void exceptionAfterSeveralItems() {
try {
performGet("/SPR-16051", new HttpHeaders(), String.class).getBody();
fail();
}
catch (Throwable ex) {
String message = ex.getMessage();
assertNotNull(message);
assertTrue("Actual: " + message, message.startsWith("Error while extracting response"));
}
assertThatExceptionOfType(Throwable.class).isThrownBy(() ->
performGet("/SPR-16051", new HttpHeaders(), String.class).getBody())
.withMessageStartingWith("Error while extracting response");
}
@Test // SPR-16318
public void exceptionFromMethodWithProducesCondition() throws Exception {
try {
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "text/plain, application/problem+json");
performGet("/SPR-16318", headers, String.class).getBody();
fail();
}
catch (HttpStatusCodeException ex) {
assertEquals(500, ex.getRawStatusCode());
assertEquals("application/problem+json", ex.getResponseHeaders().getContentType().toString());
assertEquals("{\"reason\":\"error\"}", ex.getResponseBodyAsString());
}
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "text/plain, application/problem+json");
assertThatExceptionOfType(HttpStatusCodeException.class).isThrownBy(() ->
performGet("/SPR-16318", headers, String.class).getBody())
.satisfies(ex -> {
assertEquals(500, ex.getRawStatusCode());
assertEquals("application/problem+json", ex.getResponseHeaders().getContentType().toString());
assertEquals("{\"reason\":\"error\"}", ex.getResponseBodyAsString());
});
}
private void doTest(String url, String expected) throws Exception {

View File

@@ -33,10 +33,10 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.method.ResolvableMethod;
import org.springframework.web.server.ServerWebExchange;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.web.method.MvcAnnotationPredicates.requestParam;
/**
@@ -66,16 +66,9 @@ public class RequestParamMapMethodArgumentResolverTests {
param = this.testMethod.annotNotPresent(RequestParam.class).arg(Map.class);
assertFalse(this.resolver.supportsParameter(param));
try {
param = this.testMethod.annot(requestParam()).arg(Mono.class, Map.class);
this.resolver.supportsParameter(param);
fail();
}
catch (IllegalStateException ex) {
assertTrue("Unexpected error message:\n" + ex.getMessage(),
ex.getMessage().startsWith(
"RequestParamMapMethodArgumentResolver does not support reactive type wrapper"));
}
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.supportsParameter(this.testMethod.annot(requestParam()).arg(Mono.class, Map.class)))
.withMessageStartingWith("RequestParamMapMethodArgumentResolver does not support reactive type wrapper");
}
@Test

View File

@@ -37,12 +37,12 @@ import org.springframework.web.reactive.BindingContext;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.core.ResolvableType.forClassWithGenerics;
import static org.springframework.web.method.MvcAnnotationPredicates.requestParam;
@@ -109,27 +109,12 @@ public class RequestParamMethodArgumentResolverTests {
@Test
public void doesNotSupportReactiveWrapper() {
MethodParameter param;
try {
param = this.testMethod.annot(requestParam()).arg(Mono.class, String.class);
this.resolver.supportsParameter(param);
fail();
}
catch (IllegalStateException ex) {
assertTrue("Unexpected error message:\n" + ex.getMessage(),
ex.getMessage().startsWith(
"RequestParamMethodArgumentResolver does not support reactive type wrapper"));
}
try {
param = this.testMethod.annotNotPresent(RequestParam.class).arg(Mono.class, String.class);
this.resolver.supportsParameter(param);
fail();
}
catch (IllegalStateException ex) {
assertTrue("Unexpected error message:\n" + ex.getMessage(),
ex.getMessage().startsWith(
"RequestParamMethodArgumentResolver does not support reactive type wrapper"));
}
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.supportsParameter(this.testMethod.annot(requestParam()).arg(Mono.class, String.class)))
.withMessageStartingWith("RequestParamMethodArgumentResolver does not support reactive type wrapper");
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.supportsParameter(this.testMethod.annotNotPresent(RequestParam.class).arg(Mono.class, String.class)))
.withMessageStartingWith("RequestParamMethodArgumentResolver does not support reactive type wrapper");
}
@Test

View File

@@ -37,11 +37,11 @@ import org.springframework.web.server.WebSession;
import org.springframework.web.util.UriBuilder;
import org.springframework.web.util.UriComponentsBuilder;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link ServerWebExchangeMethodArgumentResolver}.
@@ -73,15 +73,9 @@ public class ServerWebExchangeMethodArgumentResolverTests {
assertFalse(this.resolver.supportsParameter(this.testMethod.arg(WebSession.class)));
assertFalse(this.resolver.supportsParameter(this.testMethod.arg(String.class)));
try {
this.resolver.supportsParameter(this.testMethod.arg(Mono.class, ServerWebExchange.class));
fail();
}
catch (IllegalStateException ex) {
assertTrue("Unexpected error message:\n" + ex.getMessage(),
ex.getMessage().startsWith(
"ServerWebExchangeMethodArgumentResolver does not support reactive type wrapper"));
}
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.supportsParameter(this.testMethod.arg(Mono.class, ServerWebExchange.class)))
.withMessageStartingWith("ServerWebExchangeMethodArgumentResolver does not support reactive type wrapper");
}
@Test

View File

@@ -38,9 +38,8 @@ import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.ModelMap;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link HttpMessageWriterView}.
@@ -107,14 +106,9 @@ public class HttpMessageWriterViewTests {
this.model.addAttribute("foo1", "bar1");
this.model.addAttribute("foo2", "bar2");
try {
doRender();
fail();
}
catch (IllegalStateException ex) {
String message = ex.getMessage();
assertTrue(message, message.contains("Map rendering is not supported"));
}
assertThatIllegalStateException().isThrownBy(
this::doRender)
.withMessageContaining("Map rendering is not supported");
}
@Test

View File

@@ -30,6 +30,7 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.web.reactive.HandlerMapping;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -51,10 +52,11 @@ public class RedirectViewTests {
}
@Test(expected = IllegalArgumentException.class)
@Test
public void noUrlSet() throws Exception {
RedirectView rv = new RedirectView(null);
rv.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(
rv::afterPropertiesSet);
}
@Test