Improve exception asserts

This commit is contained in:
Vedran Pavic
2018-11-26 10:02:40 +01:00
parent 6b6c6f27df
commit 8a4872b919
20 changed files with 196 additions and 196 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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<S extends Session> {
@Test
public void constructorWhenNullRepositoryThenThrowsIllegalArgumentException() {
assertThatThrownBy(() -> new SpringSessionWebSessionStore<S>(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("reactiveSessionRepository cannot be null");
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new SpringSessionWebSessionStore<S>(null))
.withMessage("reactiveSessionRepository cannot be null");
}
@Test
@@ -275,9 +275,9 @@ public class SpringSessionWebSessionStoreTests<S extends Session> {
@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

View File

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

View File

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