Migrate away from AssertJ's catchThrowable()

Closes gh-35003
This commit is contained in:
Sam Brannen 2025-06-06 15:01:26 +02:00
parent 3aa3b81e1c
commit ad2931b51f
3 changed files with 41 additions and 41 deletions

View File

@ -29,7 +29,6 @@ import org.mockito.Mockito;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.BDDMockito.willAnswer; import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.BDDMockito.willThrow; import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -69,7 +68,12 @@ abstract class AbstractProxyExceptionHandlingTests {
private void invokeProxy() { private void invokeProxy() {
throwableSeenByCaller = catchThrowable(() -> Objects.requireNonNull(proxy).doSomething()); try {
Objects.requireNonNull(proxy).doSomething();
}
catch (Throwable throwable) {
throwableSeenByCaller = throwable;
}
} }
@SuppressWarnings("SameParameterValue") @SuppressWarnings("SameParameterValue")

View File

@ -43,7 +43,6 @@ import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.catchThrowable;
/** /**
* Tests for annotation-based caching methods that use reactive operators. * Tests for annotation-based caching methods that use reactive operators.
@ -154,16 +153,15 @@ class ReactiveCachingTests {
ExceptionCacheManager.class, ReactiveCacheableService.class); ExceptionCacheManager.class, ReactiveCacheableService.class);
ReactiveCacheableService service = ctx.getBean(ReactiveCacheableService.class); ReactiveCacheableService service = ctx.getBean(ReactiveCacheableService.class);
Throwable completableFutureThrowable = catchThrowable(() -> service.cacheFuture(new Object()).join()); assertThatExceptionOfType(CompletionException.class)
assertThat(completableFutureThrowable).isInstanceOf(CompletionException.class) .isThrownBy(() -> service.cacheFuture(new Object()).join())
.extracting(Throwable::getCause) .withCauseInstanceOf(UnsupportedOperationException.class);
.isInstanceOf(UnsupportedOperationException.class);
Throwable monoThrowable = catchThrowable(() -> service.cacheMono(new Object()).block()); assertThatExceptionOfType(UnsupportedOperationException.class)
assertThat(monoThrowable).isInstanceOf(UnsupportedOperationException.class); .isThrownBy(() -> service.cacheMono(new Object()).block());
Throwable fluxThrowable = catchThrowable(() -> service.cacheFlux(new Object()).blockFirst()); assertThatExceptionOfType(UnsupportedOperationException.class)
assertThat(fluxThrowable).isInstanceOf(UnsupportedOperationException.class); .isThrownBy(() -> service.cacheFlux(new Object()).blockFirst());
} }
@Test @Test
@ -172,16 +170,15 @@ class ReactiveCachingTests {
ExceptionCacheManager.class, ReactiveSyncCacheableService.class); ExceptionCacheManager.class, ReactiveSyncCacheableService.class);
ReactiveSyncCacheableService service = ctx.getBean(ReactiveSyncCacheableService.class); ReactiveSyncCacheableService service = ctx.getBean(ReactiveSyncCacheableService.class);
Throwable completableFutureThrowable = catchThrowable(() -> service.cacheFuture(new Object()).join()); assertThatExceptionOfType(CompletionException.class)
assertThat(completableFutureThrowable).isInstanceOf(CompletionException.class) .isThrownBy(() -> service.cacheFuture(new Object()).join())
.extracting(Throwable::getCause) .withCauseInstanceOf(UnsupportedOperationException.class);
.isInstanceOf(UnsupportedOperationException.class);
Throwable monoThrowable = catchThrowable(() -> service.cacheMono(new Object()).block()); assertThatExceptionOfType(UnsupportedOperationException.class)
assertThat(monoThrowable).isInstanceOf(UnsupportedOperationException.class); .isThrownBy(() -> service.cacheMono(new Object()).block());
Throwable fluxThrowable = catchThrowable(() -> service.cacheFlux(new Object()).blockFirst()); assertThatExceptionOfType(UnsupportedOperationException.class)
assertThat(fluxThrowable).isInstanceOf(UnsupportedOperationException.class); .isThrownBy(() -> service.cacheFlux(new Object()).blockFirst());
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,7 +34,6 @@ import org.springframework.util.StreamUtils;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -197,16 +196,16 @@ class DefaultResponseErrorHandlerTests {
given(response.getHeaders()).willReturn(headers); given(response.getHeaders()).willReturn(headers);
given(response.getBody()).willReturn(body); given(response.getBody()).willReturn(body);
Throwable throwable = catchThrowable(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response)); assertThatExceptionOfType(HttpClientErrorException.class)
.isThrownBy(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response))
.satisfies(ex -> {
// validate exception // validate exception
assertThat(throwable).isInstanceOf(HttpClientErrorException.class); assertThat(ex.getStatusCode()).isEqualTo(statusCode);
HttpClientErrorException actualHttpClientErrorException = (HttpClientErrorException) throwable; assertThat(ex.getStatusText()).isEqualTo(statusText);
assertThat(actualHttpClientErrorException.getStatusCode()).isEqualTo(statusCode); assertThat(ex.getResponseHeaders()).isEqualTo(headers);
assertThat(actualHttpClientErrorException.getStatusText()).isEqualTo(statusText); assertThat(ex.getMessage()).contains(responseBody);
assertThat(actualHttpClientErrorException.getResponseHeaders()).isEqualTo(headers); assertThat(ex.getResponseBodyAsString()).isEqualTo(responseBody);
assertThat(actualHttpClientErrorException.getMessage()).contains(responseBody); });
assertThat(actualHttpClientErrorException.getResponseBodyAsString()).isEqualTo(responseBody);
} }
@Test // SPR-17461 @Test // SPR-17461
@ -237,16 +236,16 @@ class DefaultResponseErrorHandlerTests {
given(response.getHeaders()).willReturn(headers); given(response.getHeaders()).willReturn(headers);
given(response.getBody()).willReturn(body); given(response.getBody()).willReturn(body);
Throwable throwable = catchThrowable(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response)); assertThatExceptionOfType(HttpServerErrorException.class)
.isThrownBy(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response))
.satisfies(ex -> {
// validate exception // validate exception
assertThat(throwable).isInstanceOf(HttpServerErrorException.class); assertThat(ex.getStatusCode()).isEqualTo(statusCode);
HttpServerErrorException actualHttpServerErrorException = (HttpServerErrorException) throwable; assertThat(ex.getStatusText()).isEqualTo(statusText);
assertThat(actualHttpServerErrorException.getStatusCode()).isEqualTo(statusCode); assertThat(ex.getResponseHeaders()).isEqualTo(headers);
assertThat(actualHttpServerErrorException.getStatusText()).isEqualTo(statusText); assertThat(ex.getMessage()).contains(responseBody);
assertThat(actualHttpServerErrorException.getResponseHeaders()).isEqualTo(headers); assertThat(ex.getResponseBodyAsString()).isEqualTo(responseBody);
assertThat(actualHttpServerErrorException.getMessage()).contains(responseBody); });
assertThat(actualHttpServerErrorException.getResponseBodyAsString()).isEqualTo(responseBody);
} }
@Test // SPR-16604 @Test // SPR-16604