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 ca7f1326..2e6cab43 100644 --- a/samples/javaconfig/rest/src/integration-test/java/sample/RestTests.java +++ b/samples/javaconfig/rest/src/integration-test/java/sample/RestTests.java @@ -32,6 +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; /** * @author Pool Dolorier @@ -52,13 +53,14 @@ public class RestTests { this.restTemplate = new RestTemplate(); } - @Test(expected = HttpClientErrorException.class) + @Test public void unauthenticatedUserSentToLogInPage() { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); - ResponseEntity entity = getForUser(this.baseUrl + "/", - headers, String.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); + assertThatThrownBy(() -> getForUser(this.baseUrl + "/", headers, String.class)) + .isInstanceOf(HttpClientErrorException.class) + .satisfies(e -> assertThat(((HttpClientErrorException) e).getStatusCode()) + .isEqualTo(HttpStatus.UNAUTHORIZED)); } @Test 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 6d2cb1c2..b7b0e7e9 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 @@ -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,6 +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; public class MapSessionTests { @@ -35,9 +36,11 @@ public class MapSessionTests { this.session.setLastAccessedTime(Instant.ofEpochMilli(1413258262962L)); } - @Test(expected = IllegalArgumentException.class) + @Test public void constructorNullSession() { - new MapSession((Session) null); + assertThatThrownBy(() -> new MapSession((Session) null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("session cannot be null"); } @Test @@ -65,9 +68,11 @@ public class MapSessionTests { assertThat(result).isEqualTo(attrValue); } - @Test(expected = IllegalArgumentException.class) + @Test public void getRequiredAttributeWhenNullThenException() { - this.session.getRequiredAttribute("attrName"); + assertThatThrownBy(() -> this.session.getRequiredAttribute("attrName")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("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 ab9f5314..633c4504 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 @@ -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. @@ -27,6 +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; /** * Tests for {@link ReactiveMapSessionRepository}. @@ -57,10 +58,11 @@ public class ReactiveMapSessionRepositoryTests { assertThat(findByIdSession.getId()).isEqualTo(this.session.getId()); } - @Test(expected = IllegalArgumentException.class) + @Test public void constructorMapWhenNullThenThrowsIllegalArgumentException() { - Map sessions = null; - new ReactiveMapSessionRepository(sessions); + assertThatThrownBy(() -> new ReactiveMapSessionRepository(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("sessions cannot be 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 633cc108..b7c2a28a 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 @@ -32,6 +32,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; /** * Tests for {@link DefaultCookieSerializer}. @@ -209,10 +210,12 @@ public class DefaultCookieSerializerTests { assertThat(getCookie().getDomain()).isEqualTo(domainName); } - @Test(expected = IllegalStateException.class) + @Test public void setDomainNameAndDomainNamePatternThrows() { this.serializer.setDomainName("example.com"); - this.serializer.setDomainNamePattern(".*"); + assertThatThrownBy(() -> this.serializer.setDomainNamePattern(".*")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Cannot set both domainName and domainNamePattern"); } // --- domainNamePattern --- @@ -241,10 +244,12 @@ public class DefaultCookieSerializerTests { } } - @Test(expected = IllegalStateException.class) + @Test public void setDomainNamePatternAndDomainNameThrows() { this.serializer.setDomainNamePattern(".*"); - this.serializer.setDomainName("example.com"); + assertThatThrownBy(() -> this.serializer.setDomainName("example.com")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Cannot set both domainName and domainNamePattern"); } // --- cookieName --- @@ -266,9 +271,11 @@ public class DefaultCookieSerializerTests { assertThat(getCookie().getName()).isEqualTo(cookieName); } - @Test(expected = IllegalArgumentException.class) + @Test public void setCookieNameNullThrows() { - this.serializer.setCookieName(null); + assertThatThrownBy(() -> this.serializer.setCookieName(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("cookieName cannot be null"); } // --- cookiePath --- 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 df239865..50e185ff 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 @@ -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. @@ -58,6 +58,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.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -1343,15 +1344,19 @@ public class SessionRepositoryFilterTests { } // We want the filter to work without any dependencies on Spring - @Test(expected = ClassCastException.class) + @Test @SuppressWarnings("unused") public void doesNotImplementOrdered() { - Ordered o = (Ordered) this.filter; + assertThatThrownBy(() -> { + Ordered o = (Ordered) this.filter; + }).isInstanceOf(ClassCastException.class); } - @Test(expected = IllegalArgumentException.class) + @Test public void setHttpSessionIdResolverNull() { - this.filter.setHttpSessionIdResolver(null); + assertThatThrownBy(() -> this.filter.setHttpSessionIdResolver(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("httpSessionIdResolver cannot be null"); } // --- helper methods 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 daed99d6..0759bd45 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 @@ -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. @@ -31,6 +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.mockito.BDDMockito.willThrow; import static org.mockito.Mockito.any; import static org.mockito.Mockito.verify; @@ -53,9 +54,11 @@ public class WebSocketConnectHandlerDecoratorFactoryTests { this.factory = new WebSocketConnectHandlerDecoratorFactory(this.eventPublisher); } - @Test(expected = IllegalArgumentException.class) + @Test public void constructorNullEventPublisher() { - new WebSocketConnectHandlerDecoratorFactory(null); + assertThatThrownBy(() -> new WebSocketConnectHandlerDecoratorFactory(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("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 815289f7..14950a63 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 @@ -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. @@ -43,6 +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.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.argThat; @@ -79,9 +80,11 @@ public class SessionRepositoryMessageInterceptorTests { given(this.sessionRepository.findById(sessionId)).willReturn(this.session); } - @Test(expected = IllegalArgumentException.class) + @Test public void preSendconstructorNullRepository() { - new SessionRepositoryMessageInterceptor<>(null); + assertThatThrownBy(() -> new SessionRepositoryMessageInterceptor<>(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("sessionRepository cannot be null"); } @Test @@ -129,14 +132,19 @@ public class SessionRepositoryMessageInterceptorTests { verifyZeroInteractions(this.sessionRepository); } - @Test(expected = IllegalArgumentException.class) + @Test public void setMatchingMessageTypesNull() { - this.interceptor.setMatchingMessageTypes(null); + assertThatThrownBy(() -> this.interceptor.setMatchingMessageTypes(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("matchingMessageTypes cannot be null or empty"); } - @Test(expected = IllegalArgumentException.class) + @Test public void setMatchingMessageTypesEmpty() { - this.interceptor.setMatchingMessageTypes(Collections.emptySet()); + assertThatThrownBy( + () -> this.interceptor.setMatchingMessageTypes(Collections.emptySet())) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("matchingMessageTypes cannot be null or empty"); } @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 b2b288c5..039ea37e 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 @@ -58,6 +58,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.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; @@ -111,9 +112,11 @@ public class RedisOperationsSessionRepositoryTests { this.cached.setLastAccessedTime(Instant.ofEpochMilli(1404360000000L)); } - @Test(expected = IllegalArgumentException.class) + @Test public void setApplicationEventPublisherNull() { - this.redisRepository.setApplicationEventPublisher(null); + assertThatThrownBy(() -> this.redisRepository.setApplicationEventPublisher(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("applicationEventPublisher cannot be null"); } @Test @@ -824,9 +827,11 @@ public class RedisOperationsSessionRepositoryTests { session.getLastAccessedTime().toEpochMilli())); } - @Test(expected = IllegalArgumentException.class) + @Test public void setRedisFlushModeNull() { - this.redisRepository.setRedisFlushMode(null); + assertThatThrownBy(() -> this.redisRepository.setRedisFlushMode(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("redisFlushMode cannot be null"); } @Test @@ -849,14 +854,18 @@ public class RedisOperationsSessionRepositoryTests { .boundValueOps(namespace + ":sessions:expires:" + id); } - @Test(expected = IllegalArgumentException.class) + @Test public void setRedisKeyNamespaceNullNamespace() { - this.redisRepository.setRedisKeyNamespace(null); + assertThatThrownBy(() -> this.redisRepository.setRedisKeyNamespace(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("namespace cannot be null or empty"); } - @Test(expected = IllegalArgumentException.class) + @Test public void setRedisKeyNamespaceEmptyNamespace() { - this.redisRepository.setRedisKeyNamespace(" "); + assertThatThrownBy(() -> this.redisRepository.setRedisKeyNamespace(" ")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("namespace cannot be null or empty"); } private String getKey(String id) {