Use new AssertJ exception assertions

This commit is contained in:
Sam Brannen
2022-05-31 14:08:28 +02:00
parent 9d324e59a0
commit 1beb7068f6
45 changed files with 554 additions and 803 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@ import org.springframework.context.support.GenericApplicationContext;
import org.springframework.remoting.RemoteAccessException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* @author Juergen Hoeller
@@ -102,19 +102,17 @@ public class JaxWsSupportTests {
String order = orderService.getOrder(1000);
assertThat(order).isEqualTo("order 1000");
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
orderService.getOrder(0))
.matches(ex -> ex instanceof OrderNotFoundException ||
ex instanceof RemoteAccessException);
assertThatException()
.isThrownBy(() -> orderService.getOrder(0))
.matches(ex -> ex instanceof OrderNotFoundException || ex instanceof RemoteAccessException);
// ignore RemoteAccessException as probably setup issue with JAX-WS provider vs JAXB
ServiceAccessor serviceAccessor = ac.getBean("accessor", ServiceAccessor.class);
order = serviceAccessor.orderService.getOrder(1000);
assertThat(order).isEqualTo("order 1000");
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
serviceAccessor.orderService.getOrder(0))
.matches(ex -> ex instanceof OrderNotFoundException ||
ex instanceof WebServiceException);
assertThatException()
.isThrownBy(() -> serviceAccessor.orderService.getOrder(0))
.matches(ex -> ex instanceof OrderNotFoundException || ex instanceof WebServiceException);
// ignore WebServiceException as probably setup issue with JAX-WS provider vs JAXB
}
catch (BeanCreationException ex) {

View File

@@ -29,7 +29,7 @@ import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.ArgumentMatchers.any;
@@ -70,12 +70,12 @@ public class WebAsyncManagerTests {
public void startAsyncProcessingWithoutAsyncWebRequest() throws Exception {
WebAsyncManager manager = WebAsyncUtils.getAsyncManager(new MockHttpServletRequest());
assertThatIllegalStateException().isThrownBy(() ->
manager.startCallableProcessing(new StubCallable(1)))
assertThatIllegalStateException()
.isThrownBy(() -> manager.startCallableProcessing(new StubCallable(1)))
.withMessage("AsyncWebRequest must not be null");
assertThatIllegalStateException().isThrownBy(() ->
manager.startDeferredResultProcessing(new DeferredResult<String>()))
assertThatIllegalStateException()
.isThrownBy(() -> manager.startDeferredResultProcessing(new DeferredResult<String>()))
.withMessage("AsyncWebRequest must not be null");
}
@@ -94,13 +94,11 @@ public class WebAsyncManagerTests {
@Test
public void setAsyncWebRequestAfterAsyncStarted() {
this.asyncWebRequest.startAsync();
assertThatIllegalArgumentException().isThrownBy(() ->
this.asyncManager.setAsyncWebRequest(null));
assertThatIllegalArgumentException().isThrownBy(() -> this.asyncManager.setAsyncWebRequest(null));
}
@Test
public void startCallableProcessing() throws Exception {
int concurrentResult = 21;
Callable<Object> task = new StubCallable(concurrentResult);
@@ -122,7 +120,6 @@ public class WebAsyncManagerTests {
@Test
public void startCallableProcessingCallableException() throws Exception {
Exception concurrentResult = new Exception();
Callable<Object> task = new StubCallable(concurrentResult);
@@ -152,8 +149,8 @@ public class WebAsyncManagerTests {
this.asyncManager.registerCallableInterceptor("interceptor", interceptor);
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
this.asyncManager.startCallableProcessing(task))
assertThatException()
.isThrownBy(() -> this.asyncManager.startCallableProcessing(task))
.isEqualTo(exception);
assertThat(this.asyncManager.hasConcurrentResult()).isFalse();
@@ -248,8 +245,8 @@ public class WebAsyncManagerTests {
@Test
public void startCallableProcessingNullInput() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() ->
this.asyncManager.startCallableProcessing((Callable<?>) null))
assertThatIllegalArgumentException()
.isThrownBy(() -> this.asyncManager.startCallableProcessing((Callable<?>) null))
.withMessage("Callable must not be null");
}
@@ -285,8 +282,8 @@ public class WebAsyncManagerTests {
this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
this.asyncManager.startDeferredResultProcessing(deferredResult))
assertThatException()
.isThrownBy(() -> this.asyncManager.startDeferredResultProcessing(deferredResult))
.isEqualTo(exception);
assertThat(this.asyncManager.hasConcurrentResult()).isFalse();
@@ -298,7 +295,6 @@ public class WebAsyncManagerTests {
@Test
public void startDeferredResultProcessingPreProcessException() throws Exception {
DeferredResult<Integer> deferredResult = new DeferredResult<>();
Exception exception = new Exception();
@@ -340,8 +336,8 @@ public class WebAsyncManagerTests {
@Test
public void startDeferredResultProcessingNullInput() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() ->
this.asyncManager.startDeferredResultProcessing(null))
assertThatIllegalArgumentException()
.isThrownBy(() -> this.asyncManager.startDeferredResultProcessing(null))
.withMessage("DeferredResult must not be null");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@ import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -131,8 +131,7 @@ public class WebArgumentResolverAdapterTests {
public void resolveArgumentThrowsException() throws Exception {
given(adaptee.resolveArgument(parameter, webRequest)).willThrow(new Exception());
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
adapter.resolveArgument(parameter, null, webRequest, null));
assertThatException().isThrownBy(() -> adapter.resolveArgument(parameter, null, webRequest, null));
}
public void handle(int param) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,9 +30,11 @@ import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* Unit tests for {@link InvocableHandlerMethod}.
@@ -127,23 +129,23 @@ public class InvocableHandlerMethodTests {
@Test
public void invocationTargetException() throws Exception {
RuntimeException runtimeException = new RuntimeException("error");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
getInvocable(Throwable.class).invokeForRequest(this.request, null, runtimeException))
assertThatRuntimeException()
.isThrownBy(() -> getInvocable(Throwable.class).invokeForRequest(this.request, null, runtimeException))
.isSameAs(runtimeException);
Error error = new Error("error");
assertThatExceptionOfType(Error.class).isThrownBy(() ->
getInvocable(Throwable.class).invokeForRequest(this.request, null, error))
assertThatExceptionOfType(Error.class)
.isThrownBy(() -> getInvocable(Throwable.class).invokeForRequest(this.request, null, error))
.isSameAs(error);
Exception exception = new Exception("error");
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
getInvocable(Throwable.class).invokeForRequest(this.request, null, exception))
assertThatException()
.isThrownBy(() -> getInvocable(Throwable.class).invokeForRequest(this.request, null, exception))
.isSameAs(exception);
Throwable throwable = new Throwable("error");
assertThatIllegalStateException().isThrownBy(() ->
getInvocable(Throwable.class).invokeForRequest(this.request, null, throwable))
assertThatIllegalStateException()
.isThrownBy(() -> getInvocable(Throwable.class).invokeForRequest(this.request, null, throwable))
.withCause(throwable)
.withMessageContaining("Invocation failure");
}
@@ -151,8 +153,8 @@ public class InvocableHandlerMethodTests {
@Test // SPR-13917
public void invocationErrorMessage() throws Exception {
this.composite.addResolver(new StubArgumentResolver(double.class));
assertThatIllegalStateException().isThrownBy(() ->
getInvocable(double.class).invokeForRequest(this.request, null))
assertThatIllegalStateException()
.isThrownBy(() -> getInvocable(double.class).invokeForRequest(this.request, null))
.withMessageContaining("Illegal argument");
}