From 329143c28e8a09d2fca27b79f0c9a49273c5ce90 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Fri, 11 Apr 2025 17:18:04 -0400 Subject: [PATCH] User Assert4J --- .../gateway/route/builder/GatewayFilterSpec.java | 2 +- .../actuate/GatewayControllerEndpointTests.java | 3 +-- .../filter/RouteToRequestUrlFilterTests.java | 4 ++-- .../ratelimit/RedisRateLimiterUnitTests.java | 5 +++-- .../cloud/gateway/handler/AsyncPredicateTest.java | 7 ++++--- .../RoutePredicateHandlerMappingTests.java | 15 ++++++++------- .../support/ConfigurationServiceTests.java | 6 +++--- .../test/CustomBlockHoundIntegrationTest.java | 5 +++-- 8 files changed, 25 insertions(+), 22 deletions(-) diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java index 0e1a2df7..47723b74 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java @@ -31,7 +31,6 @@ import java.util.stream.Stream; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.core.ParameterizedTypeReference; import reactor.retry.Repeat; import reactor.retry.Retry; @@ -84,6 +83,7 @@ import org.springframework.cloud.gateway.filter.factory.rewrite.RewriteFunction; import org.springframework.cloud.gateway.filter.ratelimit.RateLimiter; import org.springframework.cloud.gateway.route.Route; import org.springframework.core.Ordered; +import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpStatus; import org.springframework.util.unit.DataSize; import org.springframework.web.server.ServerWebExchange; diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpointTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpointTests.java index ef599fb6..b24e7d1e 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpointTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpointTests.java @@ -26,7 +26,6 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import org.assertj.core.util.Maps; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -200,7 +199,7 @@ public class GatewayControllerEndpointTests { .expectBody(ResponseEntity.class) .consumeWith(result -> { HttpStatusCode httpStatus = result.getStatus(); - Assertions.assertEquals(HttpStatus.OK, httpStatus); + assertThat(HttpStatus.OK).isEqualTo(httpStatus); }); } diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilterTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilterTests.java index f48d6312..d2ea0b90 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilterTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilterTests.java @@ -18,7 +18,6 @@ package org.springframework.cloud.gateway.filter; import java.net.URI; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import reactor.core.publisher.Mono; @@ -32,6 +31,7 @@ import org.springframework.web.server.ServerWebExchange; import org.springframework.web.util.UriComponentsBuilder; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.junit.Assume.assumeTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -64,7 +64,7 @@ public class RouteToRequestUrlFilterTests { @Test public void invalidHost() { - Assertions.assertThrows(IllegalStateException.class, () -> { + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> { MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/getb").build(); testFilter(request, "lb://my_host"); }); diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterUnitTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterUnitTests.java index f2fd7b70..0e4fe3b4 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterUnitTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterUnitTests.java @@ -18,7 +18,6 @@ package org.springframework.cloud.gateway.filter.ratelimit; import io.lettuce.core.RedisException; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -32,6 +31,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.data.redis.core.ReactiveStringRedisTemplate; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.data.MapEntry.entry; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyList; @@ -78,7 +78,8 @@ public class RedisRateLimiterUnitTests { @Test public void shouldThrowWhenNotInitialized() { - Assertions.assertThrows(IllegalStateException.class, () -> redisRateLimiter.isAllowed(ROUTE_ID, REQUEST_ID)); + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(() -> redisRateLimiter.isAllowed(ROUTE_ID, REQUEST_ID)); } @Test diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/AsyncPredicateTest.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/AsyncPredicateTest.java index 876e5fed..321d5ceb 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/AsyncPredicateTest.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/AsyncPredicateTest.java @@ -18,13 +18,14 @@ package org.springframework.cloud.gateway.handler; import java.util.function.Predicate; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.reactivestreams.Publisher; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; +import static org.assertj.core.api.Assertions.assertThat; + public class AsyncPredicateTest { @Test @@ -110,12 +111,12 @@ public class AsyncPredicateTest { @DisplayName("predicate must have been tested") public void assertTested() { - Assertions.assertTrue(tested); + assertThat(tested).isTrue(); } @DisplayName("predicate must not have been tested") public void assertUntested() { - Assertions.assertFalse(tested); + assertThat(tested).isFalse(); } } diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMappingTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMappingTests.java index cee04bdb..fa6da9f9 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMappingTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMappingTests.java @@ -16,7 +16,6 @@ package org.springframework.cloud.gateway.handler; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; @@ -32,6 +31,8 @@ import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.mock.env.MockEnvironment; import org.springframework.web.server.ServerWebExchange; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author Simon Baslé */ @@ -57,8 +58,8 @@ public class RoutePredicateHandlerMappingTests { final Mono routeMono = mapping.lookupRoute(Mockito.mock(ServerWebExchange.class)); StepVerifier.create(routeMono.map(Route::getId)).expectNext("routeTrue").verifyComplete(); - Assertions.assertTrue(capturedOutput.getOut().contains("Error applying predicate for route: routeFail")); - Assertions.assertTrue(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom")); + assertThat(capturedOutput.getOut().contains("Error applying predicate for route: routeFail")).isTrue(); + assertThat(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom")).isTrue(); } @Test @@ -89,11 +90,11 @@ public class RoutePredicateHandlerMappingTests { StepVerifier.create(routeMono.map(Route::getId)).expectNext("routeTrue").verifyComplete(); - Assertions.assertTrue(capturedOutput.getOut().contains("Error applying predicate for route: routeError")); - Assertions.assertTrue(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom1")); + assertThat(capturedOutput.getOut().contains("Error applying predicate for route: routeError")).isTrue(); + assertThat(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom1")).isTrue(); - Assertions.assertTrue(capturedOutput.getOut().contains("Error applying predicate for route: routeFail")); - Assertions.assertTrue(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom2")); + assertThat(capturedOutput.getOut().contains("Error applying predicate for route: routeFail")).isTrue(); + assertThat(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom2")).isTrue(); } } diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ConfigurationServiceTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ConfigurationServiceTests.java index e4c8db84..82f7644e 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ConfigurationServiceTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ConfigurationServiceTests.java @@ -20,7 +20,6 @@ import java.util.Collections; import java.util.Map; import jakarta.validation.constraints.Max; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.boot.context.properties.bind.BindException; @@ -30,6 +29,7 @@ import org.springframework.context.support.GenericApplicationContext; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; public class ConfigurationServiceTests { @@ -37,7 +37,7 @@ public class ConfigurationServiceTests { public void validationOnCreateWorks() { Map map = Collections.singletonMap("config.value", 11); - Assertions.assertThrows(BindException.class, () -> ConfigurationService + assertThatExceptionOfType(BindException.class).isThrownBy(() -> ConfigurationService .bindOrCreate(Bindable.of(ValidatedConfig.class), map, "config", getValidator(), null)); } @@ -57,7 +57,7 @@ public class ConfigurationServiceTests { ValidatedConfig config = new ValidatedConfig(); - Assertions.assertThrows(BindException.class, () -> ConfigurationService + assertThatExceptionOfType(BindException.class).isThrownBy(() -> ConfigurationService .bindOrCreate(Bindable.ofInstance(config), map, "config", getValidator(), null)); } diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/test/CustomBlockHoundIntegrationTest.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/test/CustomBlockHoundIntegrationTest.java index 308c0b93..7b0119a7 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/test/CustomBlockHoundIntegrationTest.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/test/CustomBlockHoundIntegrationTest.java @@ -16,7 +16,6 @@ package org.springframework.cloud.gateway.test; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledForJreRange; @@ -24,6 +23,8 @@ import org.junit.jupiter.api.condition.JRE; import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + /** * @author Tim Ysewyn */ @@ -34,7 +35,7 @@ public class CustomBlockHoundIntegrationTest { // Disable this test for now flaky on GitHub Actions @Disabled public void shouldThrowErrorForBlockingCallWithCustomBlockHoundIntegration() { - Assertions.assertThrows(RuntimeException.class, () -> Mono.fromCallable(() -> { + assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> Mono.fromCallable(() -> { Thread.sleep(1); return null; }).subscribeOn(Schedulers.parallel()).block());