From a780ee0264ac58bd412986f98f81b189b3277434 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Thu, 29 Mar 2018 23:09:51 +0200 Subject: [PATCH] Replace use of ExpectedException rule with AssertJ Closes gh-1032 --- .../test/java/sample/ApplicationTests.java | 13 +- .../SpringHttpSessionConfigurationTests.java | 18 +- .../SpringSessionRememberMeServicesTests.java | 17 +- .../http/DefaultCookieSerializerTests.java | 7 +- .../HeaderHttpSessionIdResolverTests.java | 16 +- ...RedisOperationsSessionRepositoryTests.java | 38 ++-- .../RedisHttpSessionConfigurationTests.java | 16 +- .../RedisWebSessionConfigurationTests.java | 16 +- .../HazelcastSessionRepositoryTests.java | 31 +-- ...azelcastHttpSessionConfigurationTests.java | 22 +- .../JdbcOperationsSessionRepositoryTests.java | 197 ++++++++---------- .../JdbcHttpSessionConfigurationTests.java | 25 +-- 12 files changed, 163 insertions(+), 253 deletions(-) diff --git a/samples/boot/websocket/src/test/java/sample/ApplicationTests.java b/samples/boot/websocket/src/test/java/sample/ApplicationTests.java index 53acb66e..903cef7e 100644 --- a/samples/boot/websocket/src/test/java/sample/ApplicationTests.java +++ b/samples/boot/websocket/src/test/java/sample/ApplicationTests.java @@ -21,9 +21,7 @@ import java.util.List; import java.util.concurrent.ExecutionException; import org.junit.ClassRule; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.testcontainers.containers.GenericContainer; @@ -46,6 +44,8 @@ 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; + /** * @author Rob Winch * @author Vedran Pavic @@ -61,9 +61,6 @@ public class ApplicationTests { public static GenericContainer redisContainer = new GenericContainer(DOCKER_IMAGE) .withExposedPorts(6379); - @Rule - public final ExpectedException thrown = ExpectedException.none(); - @Value("${local.server.port}") private String port; @@ -71,7 +68,7 @@ public class ApplicationTests { private WebSocketHandler webSocketHandler; @Test - public void run() throws Exception { + public void run() { List transports = new ArrayList<>(2); transports.add(new WebSocketTransport(new StandardWebSocketClient())); transports.add(new RestTemplateXhrTransport()); @@ -80,8 +77,8 @@ public class ApplicationTests { ListenableFuture wsSession = sockJsClient.doHandshake( this.webSocketHandler, "ws://localhost:" + this.port + "/sockjs"); - this.thrown.expect(ExecutionException.class); - wsSession.get().sendMessage(new TextMessage("a")); + assertThatThrownBy(() -> wsSession.get().sendMessage(new TextMessage("a"))) + .isInstanceOf(ExecutionException.class); } static class Initializer 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 18d27ef4..9f12e085 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -21,11 +21,7 @@ import java.util.concurrent.ConcurrentHashMap; import javax.servlet.ServletContext; import org.junit.After; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; import org.springframework.beans.factory.UnsatisfiedDependencyException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -42,18 +38,15 @@ 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; /** * Tests for {@link SpringHttpSessionConfiguration}. * * @author Vedran Pavic */ -@RunWith(MockitoJUnitRunner.class) public class SpringHttpSessionConfigurationTests { - @Rule - public final ExpectedException thrown = ExpectedException.none(); - private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); @After @@ -70,10 +63,9 @@ public class SpringHttpSessionConfigurationTests { @Test public void noSessionRepositoryConfiguration() { - this.thrown.expect(UnsatisfiedDependencyException.class); - this.thrown.expectMessage("org.springframework.session.SessionRepository"); - - registerAndRefresh(EmptyConfiguration.class); + assertThatThrownBy(() -> registerAndRefresh(EmptyConfiguration.class)) + .isInstanceOf(UnsatisfiedDependencyException.class) + .hasMessageContaining("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 6e90206f..24b8fa03 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -20,15 +20,14 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.security.core.Authentication; import org.springframework.security.web.context.HttpSessionSecurityContextRepository; 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.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -43,9 +42,6 @@ import static org.mockito.Mockito.verifyZeroInteractions; */ public class SpringSessionRememberMeServicesTests { - @Rule - public ExpectedException thrown = ExpectedException.none(); - private SpringSessionRememberMeServices rememberMeServices; @Test @@ -71,10 +67,10 @@ public class SpringSessionRememberMeServicesTests { @Test public void createWithNullParameter() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("rememberMeParameterName cannot be empty or null"); this.rememberMeServices = new SpringSessionRememberMeServices(); - this.rememberMeServices.setRememberMeParameterName(null); + assertThatThrownBy(() -> this.rememberMeServices.setRememberMeParameterName(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("rememberMeParameterName cannot be empty or null"); } @Test @@ -114,7 +110,8 @@ public class SpringSessionRememberMeServicesTests { this.rememberMeServices = new SpringSessionRememberMeServices(); this.rememberMeServices.loginFail(request, response); verify(request, times(1)).getSession(eq(false)); - verify(session, times(1)).removeAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY); + verify(session, times(1)).removeAttribute( + HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY); verifyZeroInteractions(request, response, session); } 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 2efabb3d..633cc108 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -21,9 +21,7 @@ import java.util.Base64; import javax.servlet.http.Cookie; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @@ -50,9 +48,6 @@ public class DefaultCookieSerializerTests { return new Object[] { false, true }; } - @Rule - public ExpectedException thrown = ExpectedException.none(); - private boolean useBase64Encoding; private String cookieName; 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 298be06f..1c9a83fb 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -20,15 +20,14 @@ import java.util.Collections; import java.util.UUID; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.mock.web.MockHttpServletRequest; 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; /** * Tests for {@link HeaderHttpSessionIdResolver}. @@ -37,9 +36,6 @@ public class HeaderHttpSessionIdResolverTests { private static final String HEADER_X_AUTH_TOKEN = "X-Auth-Token"; - @Rule - public ExpectedException thrown = ExpectedException.none(); - private MockHttpServletRequest request; private MockHttpServletResponse response; @@ -47,7 +43,7 @@ public class HeaderHttpSessionIdResolverTests { private HeaderHttpSessionIdResolver resolver; @Before - public void setup() throws Exception { + public void setup() { this.request = new MockHttpServletRequest(); this.response = new MockHttpServletResponse(); this.resolver = HeaderHttpSessionIdResolver.xAuthToken(); @@ -78,9 +74,9 @@ public class HeaderHttpSessionIdResolverTests { @Test public void createResolverWithNullHeaderName() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("headerName cannot be null"); - new HeaderHttpSessionIdResolver(null); + assertThatThrownBy(() -> new HeaderHttpSessionIdResolver(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("headerName cannot be null"); } @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 4c6d2ba0..3567da75 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -23,9 +23,7 @@ import java.util.HashMap; import java.util.Map; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -37,6 +35,7 @@ import org.springframework.session.MapSession; 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.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.BDDMockito.given; @@ -50,9 +49,6 @@ import static org.mockito.Mockito.verifyZeroInteractions; */ public class ReactiveRedisOperationsSessionRepositoryTests { - @Rule - public ExpectedException thrown = ExpectedException.none(); - @SuppressWarnings("unchecked") private ReactiveRedisOperations redisOperations = mock( ReactiveRedisOperations.class); @@ -68,17 +64,16 @@ public class ReactiveRedisOperationsSessionRepositoryTests { private ReactiveRedisOperationsSessionRepository repository; @Before - public void setUp() throws Exception { + public void setUp() { this.repository = new ReactiveRedisOperationsSessionRepository( this.redisOperations); } @Test public void constructorWithNullReactiveRedisOperations() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("sessionRedisOperations cannot be null"); - - new ReactiveRedisOperationsSessionRepository(null); + assertThatThrownBy(() -> new ReactiveRedisOperationsSessionRepository(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("sessionRedisOperations cannot be null"); } @Test @@ -91,18 +86,16 @@ public class ReactiveRedisOperationsSessionRepositoryTests { @Test public void nullRedisKeyNamespace() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("namespace cannot be null or empty"); - - this.repository.setRedisKeyNamespace(null); + assertThatThrownBy(() -> this.repository.setRedisKeyNamespace(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("namespace cannot be null or empty"); } @Test public void emptyRedisKeyNamespace() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("namespace cannot be null or empty"); - - this.repository.setRedisKeyNamespace(""); + assertThatThrownBy(() -> this.repository.setRedisKeyNamespace("")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("namespace cannot be null or empty"); } @Test @@ -123,10 +116,9 @@ public class ReactiveRedisOperationsSessionRepositoryTests { @Test public void nullRedisFlushMode() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("redisFlushMode cannot be null"); - - this.repository.setRedisFlushMode(null); + assertThatThrownBy(() -> this.repository.setRedisFlushMode(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("redisFlushMode cannot be null"); } @Test 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 da83540f..6b152f96 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -20,9 +20,7 @@ import java.util.Properties; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -39,6 +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.mockito.ArgumentMatchers.anyString; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -54,9 +53,6 @@ public class RedisHttpSessionConfigurationTests { private static final String CLEANUP_CRON_EXPRESSION = "0 0 * * * *"; - @Rule - public final ExpectedException thrown = ExpectedException.none(); - private AnnotationConfigApplicationContext context; @Before @@ -188,10 +184,10 @@ public class RedisHttpSessionConfigurationTests { @Test public void multipleConnectionFactoryRedisConfig() { - this.thrown.expect(BeanCreationException.class); - this.thrown.expectMessage("expected single matching bean but found 2"); - - registerAndRefresh(RedisConfig.class, MultipleConnectionFactoryRedisConfig.class); + assertThatThrownBy(() -> registerAndRefresh(RedisConfig.class, + MultipleConnectionFactoryRedisConfig.class)) + .isInstanceOf(BeanCreationException.class) + .hasMessageContaining("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 8a909cc9..26e470eb 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -18,9 +18,7 @@ package org.springframework.session.data.redis.config.annotation.web.server; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -35,6 +33,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.mockito.Mockito.mock; /** @@ -48,9 +47,6 @@ public class RedisWebSessionConfigurationTests { private static final int MAX_INACTIVE_INTERVAL_IN_SECONDS = 600; - @Rule - public final ExpectedException thrown = ExpectedException.none(); - private AnnotationConfigApplicationContext context; @Before @@ -179,10 +175,10 @@ public class RedisWebSessionConfigurationTests { @Test public void multipleConnectionFactoryRedisConfig() { - this.thrown.expect(BeanCreationException.class); - this.thrown.expectMessage("expected single matching bean but found 2"); - - registerAndRefresh(RedisConfig.class, MultipleConnectionFactoryRedisConfig.class); + assertThatThrownBy(() -> registerAndRefresh(RedisConfig.class, + MultipleConnectionFactoryRedisConfig.class)) + .isInstanceOf(BeanCreationException.class) + .hasMessageContaining("expected single matching bean but found 2"); } private void registerAndRefresh(Class... annotatedClasses) { 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 7d413d2b..f0ba72f8 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -29,12 +29,7 @@ import com.hazelcast.map.EntryProcessor; import com.hazelcast.map.listener.MapListener; import com.hazelcast.query.impl.predicates.EqualPredicate; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; @@ -44,12 +39,14 @@ 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.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; @@ -60,19 +57,14 @@ import static org.mockito.Mockito.verifyZeroInteractions; * @author Vedran Pavic * @author Aleksandar Stojsavljevic */ -@RunWith(MockitoJUnitRunner.class) public class HazelcastSessionRepositoryTests { private static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT"; - @Rule - public ExpectedException thrown = ExpectedException.none(); + private HazelcastInstance hazelcastInstance = mock(HazelcastInstance.class); - @Mock - private HazelcastInstance hazelcastInstance; - - @Mock - private IMap sessions; + @SuppressWarnings("unchecked") + private IMap sessions = mock(IMap.class); private HazelcastSessionRepository repository; @@ -86,14 +78,13 @@ public class HazelcastSessionRepositoryTests { @Test public void constructorNullHazelcastInstance() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("HazelcastInstance must not be null"); - - new HazelcastSessionRepository(null); + assertThatThrownBy(() -> new HazelcastSessionRepository(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("HazelcastInstance must not be null"); } @Test - public void createSessionDefaultMaxInactiveInterval() throws Exception { + public void createSessionDefaultMaxInactiveInterval() { verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean()); @@ -105,7 +96,7 @@ public class HazelcastSessionRepositoryTests { } @Test - public void createSessionCustomMaxInactiveInterval() throws Exception { + public void createSessionCustomMaxInactiveInterval() { verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean()); 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 e5e899a0..f7710a74 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 @@ -19,9 +19,7 @@ package org.springframework.session.hazelcast.config.annotation.web.http; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.IMap; import org.junit.After; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -34,6 +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.mockito.ArgumentMatchers.anyString; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -52,9 +51,6 @@ public class HazelcastHttpSessionConfigurationTests { private static final HazelcastFlushMode HAZELCAST_FLUSH_MODE = HazelcastFlushMode.IMMEDIATE; - @Rule - public final ExpectedException thrown = ExpectedException.none(); - private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); @After @@ -66,10 +62,10 @@ public class HazelcastHttpSessionConfigurationTests { @Test public void noHazelcastInstanceConfiguration() { - this.thrown.expect(BeanCreationException.class); - this.thrown.expectMessage("HazelcastInstance"); - - registerAndRefresh(NoHazelcastInstanceConfiguration.class); + assertThatThrownBy( + () -> registerAndRefresh(NoHazelcastInstanceConfiguration.class)) + .isInstanceOf(BeanCreationException.class) + .hasMessageContaining("HazelcastInstance"); } @Test @@ -210,10 +206,10 @@ public class HazelcastHttpSessionConfigurationTests { @Test public void multipleHazelcastInstanceConfiguration() { - this.thrown.expect(BeanCreationException.class); - this.thrown.expectMessage("expected single matching bean but found 2"); - - registerAndRefresh(MultipleHazelcastInstanceConfiguration.class); + assertThatThrownBy( + () -> registerAndRefresh(MultipleHazelcastInstanceConfiguration.class)) + .isInstanceOf(BeanCreationException.class) + .hasMessageContaining("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 b8790766..8314ae59 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -24,13 +24,8 @@ import java.util.List; import java.util.Map; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.JdbcOperations; @@ -46,6 +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.mockito.AdditionalMatchers.and; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.contains; @@ -54,6 +50,7 @@ import static org.mockito.ArgumentMatchers.isA; import static org.mockito.ArgumentMatchers.startsWith; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -65,222 +62,194 @@ import static org.mockito.Mockito.verifyZeroInteractions; * @author Vedran Pavic * @since 1.2.0 */ -@RunWith(MockitoJUnitRunner.class) public class JdbcOperationsSessionRepositoryTests { private static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT"; - @Rule - public ExpectedException thrown = ExpectedException.none(); + private JdbcOperations jdbcOperations = mock(JdbcOperations.class); - @Mock - private JdbcOperations jdbcOperations; - - @Mock - private PlatformTransactionManager transactionManager; + private PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class); private JdbcOperationsSessionRepository repository; @Before public void setUp() { - this.repository = new JdbcOperationsSessionRepository( - this.jdbcOperations, this.transactionManager); + this.repository = new JdbcOperationsSessionRepository(this.jdbcOperations, this.transactionManager); } @Test public void constructorNullJdbcOperations() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("JdbcOperations must not be null"); - - new JdbcOperationsSessionRepository((JdbcOperations) null, this.transactionManager); + assertThatThrownBy( + () -> new JdbcOperationsSessionRepository(null, this.transactionManager)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("JdbcOperations must not be null"); } @Test public void constructorNullTransactionManager() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Property 'transactionManager' is required"); - - new JdbcOperationsSessionRepository(this.jdbcOperations, null); + assertThatThrownBy( + () -> new JdbcOperationsSessionRepository(this.jdbcOperations, null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Property 'transactionManager' is required"); } @Test public void setTableNameNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Table name must not be empty"); - - this.repository.setTableName(null); + assertThatThrownBy(() -> this.repository.setTableName(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Table name must not be empty"); } @Test public void setTableNameEmpty() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Table name must not be empty"); - - this.repository.setTableName(" "); + assertThatThrownBy(() -> this.repository.setTableName(" ")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Table name must not be empty"); } @Test public void setCreateSessionQueryNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setCreateSessionQuery(null); + assertThatThrownBy(() -> this.repository.setCreateSessionQuery(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setCreateSessionQueryEmpty() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setCreateSessionQuery(" "); + assertThatThrownBy(() -> this.repository.setCreateSessionQuery(" ")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setCreateSessionAttributeQueryNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setCreateSessionAttributeQuery(null); + assertThatThrownBy(() -> this.repository.setCreateSessionAttributeQuery(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setCreateSessionAttributeQueryEmpty() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setCreateSessionAttributeQuery(" "); + assertThatThrownBy(() -> this.repository.setCreateSessionAttributeQuery(" ")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setGetSessionQueryNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setGetSessionQuery(null); + assertThatThrownBy(() -> this.repository.setGetSessionQuery(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setGetSessionQueryEmpty() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setGetSessionQuery(" "); + assertThatThrownBy(() -> this.repository.setGetSessionQuery(" ")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setUpdateSessionQueryNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setUpdateSessionQuery(null); + assertThatThrownBy(() -> this.repository.setUpdateSessionQuery(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setUpdateSessionQueryEmpty() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setUpdateSessionQuery(" "); + assertThatThrownBy(() -> this.repository.setUpdateSessionQuery(" ")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setUpdateSessionAttributeQueryNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setUpdateSessionAttributeQuery(null); + assertThatThrownBy(() -> this.repository.setUpdateSessionAttributeQuery(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setUpdateSessionAttributeQueryEmpty() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setUpdateSessionAttributeQuery(" "); + assertThatThrownBy(() -> this.repository.setUpdateSessionAttributeQuery(" ")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setDeleteSessionAttributeQueryNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setDeleteSessionAttributeQuery(null); + assertThatThrownBy(() -> this.repository.setDeleteSessionAttributeQuery(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setDeleteSessionAttributeQueryEmpty() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setDeleteSessionAttributeQuery(" "); + assertThatThrownBy(() -> this.repository.setDeleteSessionAttributeQuery(" ")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setDeleteSessionQueryNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setDeleteSessionQuery(null); + assertThatThrownBy(() -> this.repository.setDeleteSessionQuery(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setDeleteSessionQueryEmpty() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setDeleteSessionQuery(" "); + assertThatThrownBy(() -> this.repository.setDeleteSessionQuery(" ")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setListSessionsByPrincipalNameQueryNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setListSessionsByPrincipalNameQuery(null); + assertThatThrownBy( + () -> this.repository.setListSessionsByPrincipalNameQuery(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setListSessionsByPrincipalNameQueryEmpty() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setListSessionsByPrincipalNameQuery(" "); + assertThatThrownBy(() -> this.repository.setListSessionsByPrincipalNameQuery(" ")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setDeleteSessionsByLastAccessTimeQueryNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setDeleteSessionsByExpiryTimeQuery(null); + assertThatThrownBy(() -> this.repository.setDeleteSessionsByExpiryTimeQuery(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setDeleteSessionsByLastAccessTimeQueryEmpty() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Query must not be empty"); - - this.repository.setDeleteSessionsByExpiryTimeQuery(" "); + assertThatThrownBy(() -> this.repository.setDeleteSessionsByExpiryTimeQuery(" ")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Query must not be empty"); } @Test public void setLobHandlerNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("LobHandler must not be null"); - - this.repository.setLobHandler(null); + assertThatThrownBy(() -> this.repository.setLobHandler(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("LobHandler must not be null"); } @Test public void setConversionServiceNull() { - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("conversionService must not be null"); - - this.repository.setConversionService(null); + assertThatThrownBy(() -> this.repository.setConversionService(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("conversionService must not be null"); } @Test - public void createSessionDefaultMaxInactiveInterval() throws Exception { + public void createSessionDefaultMaxInactiveInterval() { JdbcOperationsSessionRepository.JdbcSession session = this.repository .createSession(); @@ -291,7 +260,7 @@ public class JdbcOperationsSessionRepositoryTests { } @Test - public void createSessionCustomMaxInactiveInterval() throws Exception { + public void createSessionCustomMaxInactiveInterval() { int interval = 1; this.repository.setDefaultMaxInactiveInterval(interval); 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 54c0a750..384fc3d3 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -19,9 +19,7 @@ package org.springframework.session.jdbc.config.annotation.web.http; import javax.sql.DataSource; import org.junit.After; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -39,6 +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.mockito.Mockito.mock; /** @@ -56,9 +55,6 @@ public class JdbcHttpSessionConfigurationTests { private static final String CLEANUP_CRON_EXPRESSION = "0 0 * * * *"; - @Rule - public final ExpectedException thrown = ExpectedException.none(); - private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); @After @@ -70,11 +66,9 @@ public class JdbcHttpSessionConfigurationTests { @Test public void noDataSourceConfiguration() { - this.thrown.expect(BeanCreationException.class); - this.thrown.expectMessage( - "expected at least 1 bean which qualifies as autowire candidate"); - - registerAndRefresh(NoDataSourceConfiguration.class); + assertThatThrownBy(() -> registerAndRefresh(NoDataSourceConfiguration.class)) + .isInstanceOf(BeanCreationException.class).hasMessageContaining( + "expected at least 1 bean which qualifies as autowire candidate"); } @Test @@ -230,11 +224,10 @@ public class JdbcHttpSessionConfigurationTests { @Test public void multipleDataSourceConfiguration() { - this.thrown.expect(BeanCreationException.class); - this.thrown.expectMessage("expected single matching bean but found 2"); - - registerAndRefresh(DataSourceConfiguration.class, - MultipleDataSourceConfiguration.class); + assertThatThrownBy(() -> registerAndRefresh(DataSourceConfiguration.class, + MultipleDataSourceConfiguration.class)) + .isInstanceOf(BeanCreationException.class) + .hasMessageContaining("expected single matching bean but found 2"); } @Test