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

@@ -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

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");
* 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<String, Session> sessions = null;
new ReactiveMapSessionRepository(sessions);
assertThatThrownBy(() -> new ReactiveMapSessionRepository(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("sessions cannot be null");
}
@Test

View File

@@ -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 ---

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");
* 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

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");
* 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

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");
* 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