From 8a4872b919e2659d796c7d122dd28b0bb80c2393 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Mon, 26 Nov 2018 10:02:40 +0100 Subject: [PATCH] Improve exception asserts --- .../java/sample/ApplicationTests.java | 6 +- .../java/sample/RestTests.java | 8 +- .../session/MapSessionTests.java | 14 +- .../ReactiveMapSessionRepositoryTests.java | 8 +- .../SpringHttpSessionConfigurationTests.java | 8 +- .../SpringSessionRememberMeServicesTests.java | 9 +- .../http/DefaultCookieSerializerTests.java | 20 +-- .../HeaderHttpSessionIdResolverTests.java | 8 +- .../http/SessionRepositoryFilterTests.java | 12 +- .../SpringSessionWebSessionStoreTests.java | 14 +- ...etConnectHandlerDecoratorFactoryTests.java | 8 +- ...sionRepositoryMessageInterceptorTests.java | 19 ++- ...RedisOperationsSessionRepositoryTests.java | 26 +-- ...RedisOperationsSessionRepositoryTests.java | 26 +-- .../RedisHttpSessionConfigurationTests.java | 10 +- .../RedisWebSessionConfigurationTests.java | 10 +- .../HazelcastSessionRepositoryTests.java | 8 +- ...azelcastHttpSessionConfigurationTests.java | 15 +- .../JdbcOperationsSessionRepositoryTests.java | 148 +++++++++--------- .../JdbcHttpSessionConfigurationTests.java | 15 +- 20 files changed, 196 insertions(+), 196 deletions(-) diff --git a/samples/boot/websocket/src/integration-test/java/sample/ApplicationTests.java b/samples/boot/websocket/src/integration-test/java/sample/ApplicationTests.java index 5d41842e..fea06559 100644 --- a/samples/boot/websocket/src/integration-test/java/sample/ApplicationTests.java +++ b/samples/boot/websocket/src/integration-test/java/sample/ApplicationTests.java @@ -42,7 +42,7 @@ import org.springframework.web.socket.sockjs.client.SockJsClient; import org.springframework.web.socket.sockjs.client.Transport; import org.springframework.web.socket.sockjs.client.WebSocketTransport; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Rob Winch @@ -70,8 +70,8 @@ public class ApplicationTests { ListenableFuture wsSession = sockJsClient.doHandshake( this.webSocketHandler, "ws://localhost:" + this.port + "/sockjs"); - assertThatThrownBy(() -> wsSession.get().sendMessage(new TextMessage("a"))) - .isInstanceOf(ExecutionException.class); + assertThatExceptionOfType(ExecutionException.class) + .isThrownBy(() -> wsSession.get().sendMessage(new TextMessage("a"))); } @TestConfiguration diff --git a/samples/javaconfig/rest/src/integration-test/java/sample/RestTests.java b/samples/javaconfig/rest/src/integration-test/java/sample/RestTests.java index 4239f9b7..d3670bd6 100644 --- a/samples/javaconfig/rest/src/integration-test/java/sample/RestTests.java +++ b/samples/javaconfig/rest/src/integration-test/java/sample/RestTests.java @@ -32,7 +32,7 @@ import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Pool Dolorier @@ -57,9 +57,9 @@ public class RestTests { public void unauthenticatedUserSentToLogInPage() { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); - assertThatThrownBy(() -> getForUser(this.baseUrl + "/", headers, String.class)) - .isInstanceOf(HttpClientErrorException.class) - .satisfies((e) -> assertThat(((HttpClientErrorException) e).getStatusCode()) + assertThatExceptionOfType(HttpClientErrorException.class) + .isThrownBy(() -> getForUser(this.baseUrl + "/", headers, String.class)) + .satisfies((e) -> assertThat(e.getStatusCode()) .isEqualTo(HttpStatus.UNAUTHORIZED)); } diff --git a/spring-session-core/src/test/java/org/springframework/session/MapSessionTests.java b/spring-session-core/src/test/java/org/springframework/session/MapSessionTests.java index d46bb0f7..3cd1fb39 100644 --- a/spring-session-core/src/test/java/org/springframework/session/MapSessionTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/MapSessionTests.java @@ -24,7 +24,7 @@ import org.junit.Before; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; public class MapSessionTests { @@ -38,9 +38,9 @@ public class MapSessionTests { @Test public void constructorNullSession() { - assertThatThrownBy(() -> new MapSession((Session) null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("session cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new MapSession((Session) null)) + .withMessage("session cannot be null"); } @Test @@ -70,9 +70,9 @@ public class MapSessionTests { @Test public void getRequiredAttributeWhenNullThenException() { - assertThatThrownBy(() -> this.session.getRequiredAttribute("attrName")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Required attribute 'attrName' is missing."); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.session.getRequiredAttribute("attrName")) + .withMessage("Required attribute 'attrName' is missing."); } @Test diff --git a/spring-session-core/src/test/java/org/springframework/session/ReactiveMapSessionRepositoryTests.java b/spring-session-core/src/test/java/org/springframework/session/ReactiveMapSessionRepositoryTests.java index 9c7bac19..7e10b8c8 100644 --- a/spring-session-core/src/test/java/org/springframework/session/ReactiveMapSessionRepositoryTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/ReactiveMapSessionRepositoryTests.java @@ -27,7 +27,7 @@ import org.junit.Before; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link ReactiveMapSessionRepository}. @@ -60,9 +60,9 @@ public class ReactiveMapSessionRepositoryTests { @Test public void constructorMapWhenNullThenThrowsIllegalArgumentException() { - assertThatThrownBy(() -> new ReactiveMapSessionRepository(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("sessions cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new ReactiveMapSessionRepository(null)) + .withMessage("sessions cannot be null"); } @Test diff --git a/spring-session-core/src/test/java/org/springframework/session/config/annotation/web/http/SpringHttpSessionConfigurationTests.java b/spring-session-core/src/test/java/org/springframework/session/config/annotation/web/http/SpringHttpSessionConfigurationTests.java index 9f12e085..c7670887 100644 --- a/spring-session-core/src/test/java/org/springframework/session/config/annotation/web/http/SpringHttpSessionConfigurationTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/config/annotation/web/http/SpringHttpSessionConfigurationTests.java @@ -38,7 +38,7 @@ import org.springframework.session.web.http.SessionRepositoryFilter; import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link SpringHttpSessionConfiguration}. @@ -63,9 +63,9 @@ public class SpringHttpSessionConfigurationTests { @Test public void noSessionRepositoryConfiguration() { - assertThatThrownBy(() -> registerAndRefresh(EmptyConfiguration.class)) - .isInstanceOf(UnsatisfiedDependencyException.class) - .hasMessageContaining("org.springframework.session.SessionRepository"); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> registerAndRefresh(EmptyConfiguration.class)) + .withMessageContaining("org.springframework.session.SessionRepository"); } @Test diff --git a/spring-session-core/src/test/java/org/springframework/session/security/web/authentication/SpringSessionRememberMeServicesTests.java b/spring-session-core/src/test/java/org/springframework/session/security/web/authentication/SpringSessionRememberMeServicesTests.java index 24b8fa03..e19e8db6 100644 --- a/spring-session-core/src/test/java/org/springframework/session/security/web/authentication/SpringSessionRememberMeServicesTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/security/web/authentication/SpringSessionRememberMeServicesTests.java @@ -27,7 +27,7 @@ import org.springframework.security.web.context.HttpSessionSecurityContextReposi import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -68,9 +68,10 @@ public class SpringSessionRememberMeServicesTests { @Test public void createWithNullParameter() { this.rememberMeServices = new SpringSessionRememberMeServices(); - assertThatThrownBy(() -> this.rememberMeServices.setRememberMeParameterName(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("rememberMeParameterName cannot be empty or null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy( + () -> this.rememberMeServices.setRememberMeParameterName(null)) + .withMessage("rememberMeParameterName cannot be empty or null"); } @Test diff --git a/spring-session-core/src/test/java/org/springframework/session/web/http/DefaultCookieSerializerTests.java b/spring-session-core/src/test/java/org/springframework/session/web/http/DefaultCookieSerializerTests.java index ad22e16a..bb5cd996 100644 --- a/spring-session-core/src/test/java/org/springframework/session/web/http/DefaultCookieSerializerTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/web/http/DefaultCookieSerializerTests.java @@ -33,7 +33,7 @@ import org.springframework.session.web.http.CookieSerializer.CookieValue; import org.springframework.util.StringUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link DefaultCookieSerializer}. @@ -214,9 +214,9 @@ public class DefaultCookieSerializerTests { @Test public void setDomainNameAndDomainNamePatternThrows() { this.serializer.setDomainName("example.com"); - assertThatThrownBy(() -> this.serializer.setDomainNamePattern(".*")) - .isInstanceOf(IllegalStateException.class) - .hasMessage("Cannot set both domainName and domainNamePattern"); + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(() -> this.serializer.setDomainNamePattern(".*")) + .withMessage("Cannot set both domainName and domainNamePattern"); } // --- domainNamePattern --- @@ -248,9 +248,9 @@ public class DefaultCookieSerializerTests { @Test public void setDomainNamePatternAndDomainNameThrows() { this.serializer.setDomainNamePattern(".*"); - assertThatThrownBy(() -> this.serializer.setDomainName("example.com")) - .isInstanceOf(IllegalStateException.class) - .hasMessage("Cannot set both domainName and domainNamePattern"); + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(() -> this.serializer.setDomainName("example.com")) + .withMessage("Cannot set both domainName and domainNamePattern"); } // --- cookieName --- @@ -274,9 +274,9 @@ public class DefaultCookieSerializerTests { @Test public void setCookieNameNullThrows() { - assertThatThrownBy(() -> this.serializer.setCookieName(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("cookieName cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.serializer.setCookieName(null)) + .withMessage("cookieName cannot be null"); } // --- cookiePath --- diff --git a/spring-session-core/src/test/java/org/springframework/session/web/http/HeaderHttpSessionIdResolverTests.java b/spring-session-core/src/test/java/org/springframework/session/web/http/HeaderHttpSessionIdResolverTests.java index 1c9a83fb..9491122b 100644 --- a/spring-session-core/src/test/java/org/springframework/session/web/http/HeaderHttpSessionIdResolverTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/web/http/HeaderHttpSessionIdResolverTests.java @@ -27,7 +27,7 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link HeaderHttpSessionIdResolver}. @@ -74,9 +74,9 @@ public class HeaderHttpSessionIdResolverTests { @Test public void createResolverWithNullHeaderName() { - assertThatThrownBy(() -> new HeaderHttpSessionIdResolver(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("headerName cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new HeaderHttpSessionIdResolver(null)) + .withMessage("headerName cannot be null"); } @Test diff --git a/spring-session-core/src/test/java/org/springframework/session/web/http/SessionRepositoryFilterTests.java b/spring-session-core/src/test/java/org/springframework/session/web/http/SessionRepositoryFilterTests.java index 439f6e90..36e9c60f 100644 --- a/spring-session-core/src/test/java/org/springframework/session/web/http/SessionRepositoryFilterTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/web/http/SessionRepositoryFilterTests.java @@ -62,7 +62,7 @@ import org.springframework.session.SessionRepository; import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -1418,16 +1418,16 @@ public class SessionRepositoryFilterTests { @Test @SuppressWarnings("unused") public void doesNotImplementOrdered() { - assertThatThrownBy(() -> { + assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> { Ordered o = (Ordered) this.filter; - }).isInstanceOf(ClassCastException.class); + }); } @Test public void setHttpSessionIdResolverNull() { - assertThatThrownBy(() -> this.filter.setHttpSessionIdResolver(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("httpSessionIdResolver cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.filter.setHttpSessionIdResolver(null)) + .withMessage("httpSessionIdResolver cannot be null"); } @Test diff --git a/spring-session-core/src/test/java/org/springframework/session/web/server/session/SpringSessionWebSessionStoreTests.java b/spring-session-core/src/test/java/org/springframework/session/web/server/session/SpringSessionWebSessionStoreTests.java index 82e81ca1..0263b00a 100644 --- a/spring-session-core/src/test/java/org/springframework/session/web/server/session/SpringSessionWebSessionStoreTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/web/server/session/SpringSessionWebSessionStoreTests.java @@ -33,7 +33,7 @@ import org.springframework.session.Session; import org.springframework.web.server.WebSession; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.verify; @@ -69,9 +69,9 @@ public class SpringSessionWebSessionStoreTests { @Test public void constructorWhenNullRepositoryThenThrowsIllegalArgumentException() { - assertThatThrownBy(() -> new SpringSessionWebSessionStore(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("reactiveSessionRepository cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new SpringSessionWebSessionStore(null)) + .withMessage("reactiveSessionRepository cannot be null"); } @Test @@ -275,9 +275,9 @@ public class SpringSessionWebSessionStoreTests { @Test public void setClockWhenNullThenException() { - assertThatThrownBy(() -> this.webSessionStore.setClock(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("clock cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.webSessionStore.setClock(null)) + .withMessage("clock cannot be null"); } @Test // gh-1114 diff --git a/spring-session-core/src/test/java/org/springframework/session/web/socket/handler/WebSocketConnectHandlerDecoratorFactoryTests.java b/spring-session-core/src/test/java/org/springframework/session/web/socket/handler/WebSocketConnectHandlerDecoratorFactoryTests.java index 0759bd45..166026cd 100644 --- a/spring-session-core/src/test/java/org/springframework/session/web/socket/handler/WebSocketConnectHandlerDecoratorFactoryTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/web/socket/handler/WebSocketConnectHandlerDecoratorFactoryTests.java @@ -31,7 +31,7 @@ import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.WebSocketSession; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.BDDMockito.willThrow; import static org.mockito.Mockito.any; import static org.mockito.Mockito.verify; @@ -56,9 +56,9 @@ public class WebSocketConnectHandlerDecoratorFactoryTests { @Test public void constructorNullEventPublisher() { - assertThatThrownBy(() -> new WebSocketConnectHandlerDecoratorFactory(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("eventPublisher cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new WebSocketConnectHandlerDecoratorFactory(null)) + .withMessage("eventPublisher cannot be null"); } @Test diff --git a/spring-session-core/src/test/java/org/springframework/session/web/socket/server/SessionRepositoryMessageInterceptorTests.java b/spring-session-core/src/test/java/org/springframework/session/web/socket/server/SessionRepositoryMessageInterceptorTests.java index 14950a63..8f5d0281 100644 --- a/spring-session-core/src/test/java/org/springframework/session/web/socket/server/SessionRepositoryMessageInterceptorTests.java +++ b/spring-session-core/src/test/java/org/springframework/session/web/socket/server/SessionRepositoryMessageInterceptorTests.java @@ -43,7 +43,7 @@ import org.springframework.session.Session; import org.springframework.session.SessionRepository; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.argThat; @@ -82,9 +82,9 @@ public class SessionRepositoryMessageInterceptorTests { @Test public void preSendconstructorNullRepository() { - assertThatThrownBy(() -> new SessionRepositoryMessageInterceptor<>(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("sessionRepository cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new SessionRepositoryMessageInterceptor<>(null)) + .withMessage("sessionRepository cannot be null"); } @Test @@ -134,17 +134,16 @@ public class SessionRepositoryMessageInterceptorTests { @Test public void setMatchingMessageTypesNull() { - assertThatThrownBy(() -> this.interceptor.setMatchingMessageTypes(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("matchingMessageTypes cannot be null or empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.interceptor.setMatchingMessageTypes(null)) + .withMessage("matchingMessageTypes cannot be null or empty"); } @Test public void setMatchingMessageTypesEmpty() { - assertThatThrownBy( + assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy( () -> this.interceptor.setMatchingMessageTypes(Collections.emptySet())) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("matchingMessageTypes cannot be null or empty"); + .withMessage("matchingMessageTypes cannot be null or empty"); } @Test diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepositoryTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepositoryTests.java index 81e87dee..f92dafcb 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepositoryTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/ReactiveRedisOperationsSessionRepositoryTests.java @@ -36,7 +36,7 @@ import org.springframework.session.data.redis.ReactiveRedisOperationsSessionRepo import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.BDDMockito.given; @@ -80,9 +80,9 @@ public class ReactiveRedisOperationsSessionRepositoryTests { @Test public void constructorWithNullReactiveRedisOperations() { - assertThatThrownBy(() -> new ReactiveRedisOperationsSessionRepository(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("sessionRedisOperations cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new ReactiveRedisOperationsSessionRepository(null)) + .withMessageContaining("sessionRedisOperations cannot be null"); } @Test @@ -95,16 +95,16 @@ public class ReactiveRedisOperationsSessionRepositoryTests { @Test public void nullRedisKeyNamespace() { - assertThatThrownBy(() -> this.repository.setRedisKeyNamespace(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("namespace cannot be null or empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setRedisKeyNamespace(null)) + .withMessage("namespace cannot be null or empty"); } @Test public void emptyRedisKeyNamespace() { - assertThatThrownBy(() -> this.repository.setRedisKeyNamespace("")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("namespace cannot be null or empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setRedisKeyNamespace("")) + .withMessage("namespace cannot be null or empty"); } @Test @@ -125,9 +125,9 @@ public class ReactiveRedisOperationsSessionRepositoryTests { @Test public void nullRedisFlushMode() { - assertThatThrownBy(() -> this.repository.setRedisFlushMode(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("redisFlushMode cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setRedisFlushMode(null)) + .withMessage("redisFlushMode cannot be null"); } @Test diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryTests.java index 3ebe344c..d45b07af 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/RedisOperationsSessionRepositoryTests.java @@ -59,7 +59,7 @@ import org.springframework.session.data.redis.RedisOperationsSessionRepository.R import org.springframework.session.events.AbstractSessionEvent; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; @@ -115,9 +115,9 @@ public class RedisOperationsSessionRepositoryTests { @Test public void setApplicationEventPublisherNull() { - assertThatThrownBy(() -> this.redisRepository.setApplicationEventPublisher(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("applicationEventPublisher cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.redisRepository.setApplicationEventPublisher(null)) + .withMessage("applicationEventPublisher cannot be null"); } @Test @@ -841,9 +841,9 @@ public class RedisOperationsSessionRepositoryTests { @Test public void setRedisFlushModeNull() { - assertThatThrownBy(() -> this.redisRepository.setRedisFlushMode(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("redisFlushMode cannot be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.redisRepository.setRedisFlushMode(null)) + .withMessage("redisFlushMode cannot be null"); } @Test @@ -868,16 +868,16 @@ public class RedisOperationsSessionRepositoryTests { @Test public void setRedisKeyNamespaceNullNamespace() { - assertThatThrownBy(() -> this.redisRepository.setRedisKeyNamespace(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("namespace cannot be null or empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.redisRepository.setRedisKeyNamespace(null)) + .withMessage("namespace cannot be null or empty"); } @Test public void setRedisKeyNamespaceEmptyNamespace() { - assertThatThrownBy(() -> this.redisRepository.setRedisKeyNamespace(" ")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("namespace cannot be null or empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.redisRepository.setRedisKeyNamespace(" ")) + .withMessage("namespace cannot be null or empty"); } @Test // gh-1120 diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfigurationTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfigurationTests.java index 6b152f96..7391fc29 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfigurationTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfigurationTests.java @@ -37,7 +37,7 @@ import org.springframework.session.data.redis.config.annotation.SpringSessionRed import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -184,10 +184,10 @@ public class RedisHttpSessionConfigurationTests { @Test public void multipleConnectionFactoryRedisConfig() { - assertThatThrownBy(() -> registerAndRefresh(RedisConfig.class, - MultipleConnectionFactoryRedisConfig.class)) - .isInstanceOf(BeanCreationException.class) - .hasMessageContaining("expected single matching bean but found 2"); + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> registerAndRefresh(RedisConfig.class, + MultipleConnectionFactoryRedisConfig.class)) + .withMessageContaining("expected single matching bean but found 2"); } private void registerAndRefresh(Class... annotatedClasses) { diff --git a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java index f462e25e..21ae05f3 100644 --- a/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java +++ b/spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java @@ -36,7 +36,7 @@ import org.springframework.session.data.redis.config.annotation.SpringSessionRed import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; /** @@ -194,10 +194,10 @@ public class RedisWebSessionConfigurationTests { @Test public void multipleConnectionFactoryRedisConfig() { - assertThatThrownBy(() -> registerAndRefresh(RedisConfig.class, - MultipleConnectionFactoryRedisConfig.class)) - .isInstanceOf(BeanCreationException.class) - .hasMessageContaining("expected single matching bean but found 2"); + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> registerAndRefresh(RedisConfig.class, + MultipleConnectionFactoryRedisConfig.class)) + .withMessageContaining("expected single matching bean but found 2"); } @Test diff --git a/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastSessionRepositoryTests.java b/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastSessionRepositoryTests.java index 9798047e..4ac713f2 100644 --- a/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastSessionRepositoryTests.java +++ b/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/HazelcastSessionRepositoryTests.java @@ -39,7 +39,7 @@ import org.springframework.session.MapSession; import org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; @@ -78,9 +78,9 @@ public class HazelcastSessionRepositoryTests { @Test public void constructorNullHazelcastInstance() { - assertThatThrownBy(() -> new HazelcastSessionRepository(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("HazelcastInstance must not be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new HazelcastSessionRepository(null)) + .withMessage("HazelcastInstance must not be null"); } @Test diff --git a/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfigurationTests.java b/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfigurationTests.java index f7710a74..fa0f085a 100644 --- a/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfigurationTests.java +++ b/spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfigurationTests.java @@ -32,7 +32,7 @@ import org.springframework.session.hazelcast.config.annotation.SpringSessionHaze import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -62,10 +62,10 @@ public class HazelcastHttpSessionConfigurationTests { @Test public void noHazelcastInstanceConfiguration() { - assertThatThrownBy( - () -> registerAndRefresh(NoHazelcastInstanceConfiguration.class)) - .isInstanceOf(BeanCreationException.class) - .hasMessageContaining("HazelcastInstance"); + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy( + () -> registerAndRefresh(NoHazelcastInstanceConfiguration.class)) + .withMessageContaining("HazelcastInstance"); } @Test @@ -206,10 +206,9 @@ public class HazelcastHttpSessionConfigurationTests { @Test public void multipleHazelcastInstanceConfiguration() { - assertThatThrownBy( + assertThatExceptionOfType(BeanCreationException.class).isThrownBy( () -> registerAndRefresh(MultipleHazelcastInstanceConfiguration.class)) - .isInstanceOf(BeanCreationException.class) - .hasMessageContaining("expected single matching bean but found 2"); + .withMessageContaining("expected single matching bean but found 2"); } private void registerAndRefresh(Class... annotatedClasses) { diff --git a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcOperationsSessionRepositoryTests.java b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcOperationsSessionRepositoryTests.java index de091b02..f63e7483 100644 --- a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcOperationsSessionRepositoryTests.java +++ b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/JdbcOperationsSessionRepositoryTests.java @@ -41,7 +41,7 @@ import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; @@ -80,173 +80,173 @@ public class JdbcOperationsSessionRepositoryTests { @Test public void constructorNullJdbcOperations() { - assertThatThrownBy( + assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy( () -> new JdbcOperationsSessionRepository(null, this.transactionManager)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("JdbcOperations must not be null"); + .withMessage("JdbcOperations must not be null"); } @Test public void constructorNullTransactionManager() { - assertThatThrownBy( + assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy( () -> new JdbcOperationsSessionRepository(this.jdbcOperations, null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("TransactionManager must not be null"); + .withMessage("TransactionManager must not be null"); } @Test public void setTableNameNull() { - assertThatThrownBy(() -> this.repository.setTableName(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Table name must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setTableName(null)) + .withMessage("Table name must not be empty"); } @Test public void setTableNameEmpty() { - assertThatThrownBy(() -> this.repository.setTableName(" ")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Table name must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setTableName(" ")) + .withMessage("Table name must not be empty"); } @Test public void setCreateSessionQueryNull() { - assertThatThrownBy(() -> this.repository.setCreateSessionQuery(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setCreateSessionQuery(null)) + .withMessage("Query must not be empty"); } @Test public void setCreateSessionQueryEmpty() { - assertThatThrownBy(() -> this.repository.setCreateSessionQuery(" ")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setCreateSessionQuery(" ")) + .withMessage("Query must not be empty"); } @Test public void setCreateSessionAttributeQueryNull() { - assertThatThrownBy(() -> this.repository.setCreateSessionAttributeQuery(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setCreateSessionAttributeQuery(null)) + .withMessage("Query must not be empty"); } @Test public void setCreateSessionAttributeQueryEmpty() { - assertThatThrownBy(() -> this.repository.setCreateSessionAttributeQuery(" ")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setCreateSessionAttributeQuery(" ")) + .withMessage("Query must not be empty"); } @Test public void setGetSessionQueryNull() { - assertThatThrownBy(() -> this.repository.setGetSessionQuery(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setGetSessionQuery(null)) + .withMessage("Query must not be empty"); } @Test public void setGetSessionQueryEmpty() { - assertThatThrownBy(() -> this.repository.setGetSessionQuery(" ")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setGetSessionQuery(" ")) + .withMessage("Query must not be empty"); } @Test public void setUpdateSessionQueryNull() { - assertThatThrownBy(() -> this.repository.setUpdateSessionQuery(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setUpdateSessionQuery(null)) + .withMessage("Query must not be empty"); } @Test public void setUpdateSessionQueryEmpty() { - assertThatThrownBy(() -> this.repository.setUpdateSessionQuery(" ")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setUpdateSessionQuery(" ")) + .withMessage("Query must not be empty"); } @Test public void setUpdateSessionAttributeQueryNull() { - assertThatThrownBy(() -> this.repository.setUpdateSessionAttributeQuery(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setUpdateSessionAttributeQuery(null)) + .withMessage("Query must not be empty"); } @Test public void setUpdateSessionAttributeQueryEmpty() { - assertThatThrownBy(() -> this.repository.setUpdateSessionAttributeQuery(" ")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setUpdateSessionAttributeQuery(" ")) + .withMessage("Query must not be empty"); } @Test public void setDeleteSessionAttributeQueryNull() { - assertThatThrownBy(() -> this.repository.setDeleteSessionAttributeQuery(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setDeleteSessionAttributeQuery(null)) + .withMessage("Query must not be empty"); } @Test public void setDeleteSessionAttributeQueryEmpty() { - assertThatThrownBy(() -> this.repository.setDeleteSessionAttributeQuery(" ")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setDeleteSessionAttributeQuery(" ")) + .withMessage("Query must not be empty"); } @Test public void setDeleteSessionQueryNull() { - assertThatThrownBy(() -> this.repository.setDeleteSessionQuery(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setDeleteSessionQuery(null)) + .withMessage("Query must not be empty"); } @Test public void setDeleteSessionQueryEmpty() { - assertThatThrownBy(() -> this.repository.setDeleteSessionQuery(" ")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setDeleteSessionQuery(" ")) + .withMessage("Query must not be empty"); } @Test public void setListSessionsByPrincipalNameQueryNull() { - assertThatThrownBy( - () -> this.repository.setListSessionsByPrincipalNameQuery(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy( + () -> this.repository.setListSessionsByPrincipalNameQuery(null)) + .withMessage("Query must not be empty"); } @Test public void setListSessionsByPrincipalNameQueryEmpty() { - assertThatThrownBy(() -> this.repository.setListSessionsByPrincipalNameQuery(" ")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy( + () -> this.repository.setListSessionsByPrincipalNameQuery(" ")) + .withMessage("Query must not be empty"); } @Test public void setDeleteSessionsByLastAccessTimeQueryNull() { - assertThatThrownBy(() -> this.repository.setDeleteSessionsByExpiryTimeQuery(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy( + () -> this.repository.setDeleteSessionsByExpiryTimeQuery(null)) + .withMessage("Query must not be empty"); } @Test public void setDeleteSessionsByLastAccessTimeQueryEmpty() { - assertThatThrownBy(() -> this.repository.setDeleteSessionsByExpiryTimeQuery(" ")) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Query must not be empty"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setDeleteSessionsByExpiryTimeQuery(" ")) + .withMessage("Query must not be empty"); } @Test public void setLobHandlerNull() { - assertThatThrownBy(() -> this.repository.setLobHandler(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("LobHandler must not be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setLobHandler(null)) + .withMessage("LobHandler must not be null"); } @Test public void setConversionServiceNull() { - assertThatThrownBy(() -> this.repository.setConversionService(null)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("conversionService must not be null"); + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.repository.setConversionService(null)) + .withMessage("conversionService must not be null"); } @Test diff --git a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java index 384fc3d3..9f54dd49 100644 --- a/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java +++ b/spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java @@ -37,7 +37,7 @@ import org.springframework.test.util.ReflectionTestUtils; import org.springframework.transaction.PlatformTransactionManager; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; /** @@ -66,8 +66,9 @@ public class JdbcHttpSessionConfigurationTests { @Test public void noDataSourceConfiguration() { - assertThatThrownBy(() -> registerAndRefresh(NoDataSourceConfiguration.class)) - .isInstanceOf(BeanCreationException.class).hasMessageContaining( + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> registerAndRefresh(NoDataSourceConfiguration.class)) + .withMessageContaining( "expected at least 1 bean which qualifies as autowire candidate"); } @@ -224,10 +225,10 @@ public class JdbcHttpSessionConfigurationTests { @Test public void multipleDataSourceConfiguration() { - assertThatThrownBy(() -> registerAndRefresh(DataSourceConfiguration.class, - MultipleDataSourceConfiguration.class)) - .isInstanceOf(BeanCreationException.class) - .hasMessageContaining("expected single matching bean but found 2"); + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> registerAndRefresh(DataSourceConfiguration.class, + MultipleDataSourceConfiguration.class)) + .withMessageContaining("expected single matching bean but found 2"); } @Test