Replace use of Test.expected with AssertJ

See gh-1032
This commit is contained in:
Vedran Pavic
2018-05-04 17:57:56 +02:00
parent bb1c099094
commit 941fdb46f2
8 changed files with 83 additions and 42 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* @author Pool Dolorier * @author Pool Dolorier
@@ -52,13 +53,14 @@ public class RestTests {
this.restTemplate = new RestTemplate(); this.restTemplate = new RestTemplate();
} }
@Test(expected = HttpClientErrorException.class) @Test
public void unauthenticatedUserSentToLogInPage() { public void unauthenticatedUserSentToLogInPage() {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
ResponseEntity<String> entity = getForUser(this.baseUrl + "/", assertThatThrownBy(() -> getForUser(this.baseUrl + "/", headers, String.class))
headers, String.class); .isInstanceOf(HttpClientErrorException.class)
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); .satisfies(e -> assertThat(((HttpClientErrorException) e).getStatusCode())
.isEqualTo(HttpStatus.UNAUTHORIZED));
} }
@Test @Test

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class MapSessionTests { public class MapSessionTests {
@@ -35,9 +36,11 @@ public class MapSessionTests {
this.session.setLastAccessedTime(Instant.ofEpochMilli(1413258262962L)); this.session.setLastAccessedTime(Instant.ofEpochMilli(1413258262962L));
} }
@Test(expected = IllegalArgumentException.class) @Test
public void constructorNullSession() { public void constructorNullSession() {
new MapSession((Session) null); assertThatThrownBy(() -> new MapSession((Session) null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("session cannot be null");
} }
@Test @Test
@@ -65,9 +68,11 @@ public class MapSessionTests {
assertThat(result).isEqualTo(attrValue); assertThat(result).isEqualTo(attrValue);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void getRequiredAttributeWhenNullThenException() { public void getRequiredAttributeWhenNullThenException() {
this.session.getRequiredAttribute("attrName"); assertThatThrownBy(() -> this.session.getRequiredAttribute("attrName"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Required attribute 'attrName' is missing.");
} }
@Test @Test

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* Tests for {@link ReactiveMapSessionRepository}. * Tests for {@link ReactiveMapSessionRepository}.
@@ -57,10 +58,11 @@ public class ReactiveMapSessionRepositoryTests {
assertThat(findByIdSession.getId()).isEqualTo(this.session.getId()); assertThat(findByIdSession.getId()).isEqualTo(this.session.getId());
} }
@Test(expected = IllegalArgumentException.class) @Test
public void constructorMapWhenNullThenThrowsIllegalArgumentException() { public void constructorMapWhenNullThenThrowsIllegalArgumentException() {
Map<String, Session> sessions = null; assertThatThrownBy(() -> new ReactiveMapSessionRepository(null))
new ReactiveMapSessionRepository(sessions); .isInstanceOf(IllegalArgumentException.class)
.hasMessage("sessions cannot be null");
} }
@Test @Test

View File

@@ -32,6 +32,7 @@ import org.springframework.session.web.http.CookieSerializer.CookieValue;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* Tests for {@link DefaultCookieSerializer}. * Tests for {@link DefaultCookieSerializer}.
@@ -209,10 +210,12 @@ public class DefaultCookieSerializerTests {
assertThat(getCookie().getDomain()).isEqualTo(domainName); assertThat(getCookie().getDomain()).isEqualTo(domainName);
} }
@Test(expected = IllegalStateException.class) @Test
public void setDomainNameAndDomainNamePatternThrows() { public void setDomainNameAndDomainNamePatternThrows() {
this.serializer.setDomainName("example.com"); this.serializer.setDomainName("example.com");
this.serializer.setDomainNamePattern(".*"); assertThatThrownBy(() -> this.serializer.setDomainNamePattern(".*"))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Cannot set both domainName and domainNamePattern");
} }
// --- domainNamePattern --- // --- domainNamePattern ---
@@ -241,10 +244,12 @@ public class DefaultCookieSerializerTests {
} }
} }
@Test(expected = IllegalStateException.class) @Test
public void setDomainNamePatternAndDomainNameThrows() { public void setDomainNamePatternAndDomainNameThrows() {
this.serializer.setDomainNamePattern(".*"); 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 --- // --- cookieName ---
@@ -266,9 +271,11 @@ public class DefaultCookieSerializerTests {
assertThat(getCookie().getName()).isEqualTo(cookieName); assertThat(getCookie().getName()).isEqualTo(cookieName);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void setCookieNameNullThrows() { public void setCookieNameNullThrows() {
this.serializer.setCookieName(null); assertThatThrownBy(() -> this.serializer.setCookieName(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("cookieName cannot be null");
} }
// --- cookiePath --- // --- cookiePath ---

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; 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.assertj.core.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
@@ -1343,15 +1344,19 @@ public class SessionRepositoryFilterTests {
} }
// We want the filter to work without any dependencies on Spring // We want the filter to work without any dependencies on Spring
@Test(expected = ClassCastException.class) @Test
@SuppressWarnings("unused") @SuppressWarnings("unused")
public void doesNotImplementOrdered() { 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() { public void setHttpSessionIdResolverNull() {
this.filter.setHttpSessionIdResolver(null); assertThatThrownBy(() -> this.filter.setHttpSessionIdResolver(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("httpSessionIdResolver cannot be null");
} }
// --- helper methods // --- helper methods

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 org.springframework.web.socket.WebSocketSession;
import static org.assertj.core.api.Assertions.assertThat; 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.BDDMockito.willThrow;
import static org.mockito.Mockito.any; import static org.mockito.Mockito.any;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@@ -53,9 +54,11 @@ public class WebSocketConnectHandlerDecoratorFactoryTests {
this.factory = new WebSocketConnectHandlerDecoratorFactory(this.eventPublisher); this.factory = new WebSocketConnectHandlerDecoratorFactory(this.eventPublisher);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void constructorNullEventPublisher() { public void constructorNullEventPublisher() {
new WebSocketConnectHandlerDecoratorFactory(null); assertThatThrownBy(() -> new WebSocketConnectHandlerDecoratorFactory(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("eventPublisher cannot be null");
} }
@Test @Test

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 org.springframework.session.SessionRepository;
import static org.assertj.core.api.Assertions.assertThat; 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.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat;
@@ -79,9 +80,11 @@ public class SessionRepositoryMessageInterceptorTests {
given(this.sessionRepository.findById(sessionId)).willReturn(this.session); given(this.sessionRepository.findById(sessionId)).willReturn(this.session);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void preSendconstructorNullRepository() { public void preSendconstructorNullRepository() {
new SessionRepositoryMessageInterceptor<>(null); assertThatThrownBy(() -> new SessionRepositoryMessageInterceptor<>(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("sessionRepository cannot be null");
} }
@Test @Test
@@ -129,14 +132,19 @@ public class SessionRepositoryMessageInterceptorTests {
verifyZeroInteractions(this.sessionRepository); verifyZeroInteractions(this.sessionRepository);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void setMatchingMessageTypesNull() { 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() { 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 @Test

View File

@@ -58,6 +58,7 @@ import org.springframework.session.data.redis.RedisOperationsSessionRepository.R
import org.springframework.session.events.AbstractSessionEvent; import org.springframework.session.events.AbstractSessionEvent;
import static org.assertj.core.api.Assertions.assertThat; 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.any;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
@@ -111,9 +112,11 @@ public class RedisOperationsSessionRepositoryTests {
this.cached.setLastAccessedTime(Instant.ofEpochMilli(1404360000000L)); this.cached.setLastAccessedTime(Instant.ofEpochMilli(1404360000000L));
} }
@Test(expected = IllegalArgumentException.class) @Test
public void setApplicationEventPublisherNull() { public void setApplicationEventPublisherNull() {
this.redisRepository.setApplicationEventPublisher(null); assertThatThrownBy(() -> this.redisRepository.setApplicationEventPublisher(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("applicationEventPublisher cannot be null");
} }
@Test @Test
@@ -824,9 +827,11 @@ public class RedisOperationsSessionRepositoryTests {
session.getLastAccessedTime().toEpochMilli())); session.getLastAccessedTime().toEpochMilli()));
} }
@Test(expected = IllegalArgumentException.class) @Test
public void setRedisFlushModeNull() { public void setRedisFlushModeNull() {
this.redisRepository.setRedisFlushMode(null); assertThatThrownBy(() -> this.redisRepository.setRedisFlushMode(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("redisFlushMode cannot be null");
} }
@Test @Test
@@ -849,14 +854,18 @@ public class RedisOperationsSessionRepositoryTests {
.boundValueOps(namespace + ":sessions:expires:" + id); .boundValueOps(namespace + ":sessions:expires:" + id);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void setRedisKeyNamespaceNullNamespace() { 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() { 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) { private String getKey(String id) {