Skip to content

Commit ad2931b

Browse files
committed
Migrate away from AssertJ's catchThrowable()
Closes gh-35003
1 parent 3aa3b81 commit ad2931b

File tree

3 files changed

+41
-41
lines changed

3 files changed

+41
-41
lines changed

spring-aop/src/test/java/org/springframework/aop/framework/AbstractProxyExceptionHandlingTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.mockito.stubbing.Answer;
3030

3131
import static org.assertj.core.api.Assertions.assertThat;
32-
import static org.assertj.core.api.Assertions.catchThrowable;
3332
import static org.mockito.BDDMockito.willAnswer;
3433
import static org.mockito.BDDMockito.willThrow;
3534
import static org.mockito.Mockito.mock;
@@ -69,7 +68,12 @@ void clear() {
6968

7069

7170
private void invokeProxy() {
72-
throwableSeenByCaller = catchThrowable(() -> Objects.requireNonNull(proxy).doSomething());
71+
try {
72+
Objects.requireNonNull(proxy).doSomething();
73+
}
74+
catch (Throwable throwable) {
75+
throwableSeenByCaller = throwable;
76+
}
7377
}
7478

7579
@SuppressWarnings("SameParameterValue")

spring-context/src/test/java/org/springframework/cache/annotation/ReactiveCachingTests.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343

4444
import static org.assertj.core.api.Assertions.assertThat;
4545
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
46-
import static org.assertj.core.api.Assertions.catchThrowable;
4746

4847
/**
4948
* Tests for annotation-based caching methods that use reactive operators.
@@ -154,16 +153,15 @@ void cacheErrorHandlerWithSimpleCacheErrorHandler() {
154153
ExceptionCacheManager.class, ReactiveCacheableService.class);
155154
ReactiveCacheableService service = ctx.getBean(ReactiveCacheableService.class);
156155

157-
Throwable completableFutureThrowable = catchThrowable(() -> service.cacheFuture(new Object()).join());
158-
assertThat(completableFutureThrowable).isInstanceOf(CompletionException.class)
159-
.extracting(Throwable::getCause)
160-
.isInstanceOf(UnsupportedOperationException.class);
156+
assertThatExceptionOfType(CompletionException.class)
157+
.isThrownBy(() -> service.cacheFuture(new Object()).join())
158+
.withCauseInstanceOf(UnsupportedOperationException.class);
161159

162-
Throwable monoThrowable = catchThrowable(() -> service.cacheMono(new Object()).block());
163-
assertThat(monoThrowable).isInstanceOf(UnsupportedOperationException.class);
160+
assertThatExceptionOfType(UnsupportedOperationException.class)
161+
.isThrownBy(() -> service.cacheMono(new Object()).block());
164162

165-
Throwable fluxThrowable = catchThrowable(() -> service.cacheFlux(new Object()).blockFirst());
166-
assertThat(fluxThrowable).isInstanceOf(UnsupportedOperationException.class);
163+
assertThatExceptionOfType(UnsupportedOperationException.class)
164+
.isThrownBy(() -> service.cacheFlux(new Object()).blockFirst());
167165
}
168166

169167
@Test
@@ -172,16 +170,15 @@ void cacheErrorHandlerWithSimpleCacheErrorHandlerAndSync() {
172170
ExceptionCacheManager.class, ReactiveSyncCacheableService.class);
173171
ReactiveSyncCacheableService service = ctx.getBean(ReactiveSyncCacheableService.class);
174172

175-
Throwable completableFutureThrowable = catchThrowable(() -> service.cacheFuture(new Object()).join());
176-
assertThat(completableFutureThrowable).isInstanceOf(CompletionException.class)
177-
.extracting(Throwable::getCause)
178-
.isInstanceOf(UnsupportedOperationException.class);
173+
assertThatExceptionOfType(CompletionException.class)
174+
.isThrownBy(() -> service.cacheFuture(new Object()).join())
175+
.withCauseInstanceOf(UnsupportedOperationException.class);
179176

180-
Throwable monoThrowable = catchThrowable(() -> service.cacheMono(new Object()).block());
181-
assertThat(monoThrowable).isInstanceOf(UnsupportedOperationException.class);
177+
assertThatExceptionOfType(UnsupportedOperationException.class)
178+
.isThrownBy(() -> service.cacheMono(new Object()).block());
182179

183-
Throwable fluxThrowable = catchThrowable(() -> service.cacheFlux(new Object()).blockFirst());
184-
assertThat(fluxThrowable).isInstanceOf(UnsupportedOperationException.class);
180+
assertThatExceptionOfType(UnsupportedOperationException.class)
181+
.isThrownBy(() -> service.cacheFlux(new Object()).blockFirst());
185182
}
186183

187184
@Test

spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,7 +34,6 @@
3434
import static java.nio.charset.StandardCharsets.UTF_8;
3535
import static org.assertj.core.api.Assertions.assertThat;
3636
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
37-
import static org.assertj.core.api.Assertions.catchThrowable;
3837
import static org.mockito.BDDMockito.given;
3938
import static org.mockito.Mockito.mock;
4039

@@ -197,16 +196,16 @@ void handleErrorForCustomClientError() throws Exception {
197196
given(response.getHeaders()).willReturn(headers);
198197
given(response.getBody()).willReturn(body);
199198

200-
Throwable throwable = catchThrowable(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response));
201-
202-
// validate exception
203-
assertThat(throwable).isInstanceOf(HttpClientErrorException.class);
204-
HttpClientErrorException actualHttpClientErrorException = (HttpClientErrorException) throwable;
205-
assertThat(actualHttpClientErrorException.getStatusCode()).isEqualTo(statusCode);
206-
assertThat(actualHttpClientErrorException.getStatusText()).isEqualTo(statusText);
207-
assertThat(actualHttpClientErrorException.getResponseHeaders()).isEqualTo(headers);
208-
assertThat(actualHttpClientErrorException.getMessage()).contains(responseBody);
209-
assertThat(actualHttpClientErrorException.getResponseBodyAsString()).isEqualTo(responseBody);
199+
assertThatExceptionOfType(HttpClientErrorException.class)
200+
.isThrownBy(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response))
201+
.satisfies(ex -> {
202+
// validate exception
203+
assertThat(ex.getStatusCode()).isEqualTo(statusCode);
204+
assertThat(ex.getStatusText()).isEqualTo(statusText);
205+
assertThat(ex.getResponseHeaders()).isEqualTo(headers);
206+
assertThat(ex.getMessage()).contains(responseBody);
207+
assertThat(ex.getResponseBodyAsString()).isEqualTo(responseBody);
208+
});
210209
}
211210

212211
@Test // SPR-17461
@@ -237,16 +236,16 @@ void handleErrorForCustomServerError() throws Exception {
237236
given(response.getHeaders()).willReturn(headers);
238237
given(response.getBody()).willReturn(body);
239238

240-
Throwable throwable = catchThrowable(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response));
241-
242-
// validate exception
243-
assertThat(throwable).isInstanceOf(HttpServerErrorException.class);
244-
HttpServerErrorException actualHttpServerErrorException = (HttpServerErrorException) throwable;
245-
assertThat(actualHttpServerErrorException.getStatusCode()).isEqualTo(statusCode);
246-
assertThat(actualHttpServerErrorException.getStatusText()).isEqualTo(statusText);
247-
assertThat(actualHttpServerErrorException.getResponseHeaders()).isEqualTo(headers);
248-
assertThat(actualHttpServerErrorException.getMessage()).contains(responseBody);
249-
assertThat(actualHttpServerErrorException.getResponseBodyAsString()).isEqualTo(responseBody);
239+
assertThatExceptionOfType(HttpServerErrorException.class)
240+
.isThrownBy(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response))
241+
.satisfies(ex -> {
242+
// validate exception
243+
assertThat(ex.getStatusCode()).isEqualTo(statusCode);
244+
assertThat(ex.getStatusText()).isEqualTo(statusText);
245+
assertThat(ex.getResponseHeaders()).isEqualTo(headers);
246+
assertThat(ex.getMessage()).contains(responseBody);
247+
assertThat(ex.getResponseBodyAsString()).isEqualTo(responseBody);
248+
});
250249
}
251250

252251
@Test // SPR-16604

0 commit comments

Comments
 (0)