diff --git a/spring-aop/src/test/java/org/springframework/aop/framework/AbstractProxyExceptionHandlingTests.java b/spring-aop/src/test/java/org/springframework/aop/framework/AbstractProxyExceptionHandlingTests.java index f25b17a10b..bba53793cc 100644 --- a/spring-aop/src/test/java/org/springframework/aop/framework/AbstractProxyExceptionHandlingTests.java +++ b/spring-aop/src/test/java/org/springframework/aop/framework/AbstractProxyExceptionHandlingTests.java @@ -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") diff --git a/spring-context/src/test/java/org/springframework/cache/annotation/ReactiveCachingTests.java b/spring-context/src/test/java/org/springframework/cache/annotation/ReactiveCachingTests.java index 02620a8572..f8cc874eac 100644 --- a/spring-context/src/test/java/org/springframework/cache/annotation/ReactiveCachingTests.java +++ b/spring-context/src/test/java/org/springframework/cache/annotation/ReactiveCachingTests.java @@ -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 diff --git a/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java b/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java index d7f3fcbfbb..c6b377b49b 100644 --- a/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java @@ -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