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 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.willThrow;
import static org.mockito.Mockito.mock;
@ -69,7 +68,12 @@ abstract class AbstractProxyExceptionHandlingTests {
private void invokeProxy() {
throwableSeenByCaller = catchThrowable(() -> Objects.requireNonNull(proxy).doSomething());
try {
Objects.requireNonNull(proxy).doSomething();
}
catch (Throwable throwable) {
throwableSeenByCaller = throwable;
}
}
@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.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.catchThrowable;
/**
* Tests for annotation-based caching methods that use reactive operators.
@ -154,16 +153,15 @@ class ReactiveCachingTests {
ExceptionCacheManager.class, ReactiveCacheableService.class);
ReactiveCacheableService service = ctx.getBean(ReactiveCacheableService.class);
Throwable completableFutureThrowable = catchThrowable(() -> service.cacheFuture(new Object()).join());
assertThat(completableFutureThrowable).isInstanceOf(CompletionException.class)
.extracting(Throwable::getCause)
.isInstanceOf(UnsupportedOperationException.class);
assertThatExceptionOfType(CompletionException.class)
.isThrownBy(() -> service.cacheFuture(new Object()).join())
.withCauseInstanceOf(UnsupportedOperationException.class);
Throwable monoThrowable = catchThrowable(() -> service.cacheMono(new Object()).block());
assertThat(monoThrowable).isInstanceOf(UnsupportedOperationException.class);
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> service.cacheMono(new Object()).block());
Throwable fluxThrowable = catchThrowable(() -> service.cacheFlux(new Object()).blockFirst());
assertThat(fluxThrowable).isInstanceOf(UnsupportedOperationException.class);
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> service.cacheFlux(new Object()).blockFirst());
}
@Test
@ -172,16 +170,15 @@ class ReactiveCachingTests {
ExceptionCacheManager.class, ReactiveSyncCacheableService.class);
ReactiveSyncCacheableService service = ctx.getBean(ReactiveSyncCacheableService.class);
Throwable completableFutureThrowable = catchThrowable(() -> service.cacheFuture(new Object()).join());
assertThat(completableFutureThrowable).isInstanceOf(CompletionException.class)
.extracting(Throwable::getCause)
.isInstanceOf(UnsupportedOperationException.class);
assertThatExceptionOfType(CompletionException.class)
.isThrownBy(() -> service.cacheFuture(new Object()).join())
.withCauseInstanceOf(UnsupportedOperationException.class);
Throwable monoThrowable = catchThrowable(() -> service.cacheMono(new Object()).block());
assertThat(monoThrowable).isInstanceOf(UnsupportedOperationException.class);
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> service.cacheMono(new Object()).block());
Throwable fluxThrowable = catchThrowable(() -> service.cacheFlux(new Object()).blockFirst());
assertThat(fluxThrowable).isInstanceOf(UnsupportedOperationException.class);
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> service.cacheFlux(new Object()).blockFirst());
}
@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");
* 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 org.assertj.core.api.Assertions.assertThat;
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.Mockito.mock;
@ -197,16 +196,16 @@ class DefaultResponseErrorHandlerTests {
given(response.getHeaders()).willReturn(headers);
given(response.getBody()).willReturn(body);
Throwable throwable = catchThrowable(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response));
// validate exception
assertThat(throwable).isInstanceOf(HttpClientErrorException.class);
HttpClientErrorException actualHttpClientErrorException = (HttpClientErrorException) throwable;
assertThat(actualHttpClientErrorException.getStatusCode()).isEqualTo(statusCode);
assertThat(actualHttpClientErrorException.getStatusText()).isEqualTo(statusText);
assertThat(actualHttpClientErrorException.getResponseHeaders()).isEqualTo(headers);
assertThat(actualHttpClientErrorException.getMessage()).contains(responseBody);
assertThat(actualHttpClientErrorException.getResponseBodyAsString()).isEqualTo(responseBody);
assertThatExceptionOfType(HttpClientErrorException.class)
.isThrownBy(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response))
.satisfies(ex -> {
// validate exception
assertThat(ex.getStatusCode()).isEqualTo(statusCode);
assertThat(ex.getStatusText()).isEqualTo(statusText);
assertThat(ex.getResponseHeaders()).isEqualTo(headers);
assertThat(ex.getMessage()).contains(responseBody);
assertThat(ex.getResponseBodyAsString()).isEqualTo(responseBody);
});
}
@Test // SPR-17461
@ -237,16 +236,16 @@ class DefaultResponseErrorHandlerTests {
given(response.getHeaders()).willReturn(headers);
given(response.getBody()).willReturn(body);
Throwable throwable = catchThrowable(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response));
// validate exception
assertThat(throwable).isInstanceOf(HttpServerErrorException.class);
HttpServerErrorException actualHttpServerErrorException = (HttpServerErrorException) throwable;
assertThat(actualHttpServerErrorException.getStatusCode()).isEqualTo(statusCode);
assertThat(actualHttpServerErrorException.getStatusText()).isEqualTo(statusText);
assertThat(actualHttpServerErrorException.getResponseHeaders()).isEqualTo(headers);
assertThat(actualHttpServerErrorException.getMessage()).contains(responseBody);
assertThat(actualHttpServerErrorException.getResponseBodyAsString()).isEqualTo(responseBody);
assertThatExceptionOfType(HttpServerErrorException.class)
.isThrownBy(() -> handler.handleError(URI.create("/"), HttpMethod.GET, response))
.satisfies(ex -> {
// validate exception
assertThat(ex.getStatusCode()).isEqualTo(statusCode);
assertThat(ex.getStatusText()).isEqualTo(statusText);
assertThat(ex.getResponseHeaders()).isEqualTo(headers);
assertThat(ex.getMessage()).contains(responseBody);
assertThat(ex.getResponseBodyAsString()).isEqualTo(responseBody);
});
}
@Test // SPR-16604