1
0
mirror of synced 2026-05-22 13:23:17 +00:00

Replace expected @Test attributes with AssertJ

Replace JUnit expected @Test attributes with AssertJ calls.
This commit is contained in:
Phillip Webb
2020-09-10 21:33:16 -07:00
committed by Josh Cummings
parent 20baa7d409
commit c502312719
243 changed files with 2115 additions and 1591 deletions
@@ -36,6 +36,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;
@RunWith(MockitoJUnitRunner.class)
@@ -69,9 +70,9 @@ public class DefaultMessageSecurityExpressionHandlerTests {
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isFalse();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void trustResolverNull() {
this.handler.setTrustResolver(null);
assertThatIllegalArgumentException().isThrownBy(() -> this.handler.setTrustResolver(null));
}
@Test
@@ -31,6 +31,7 @@ import org.springframework.security.messaging.util.matcher.MessageMatcher;
import org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -51,14 +52,15 @@ public class MessageExpressionConfigAttributeTests {
this.attribute = new MessageExpressionConfigAttribute(this.expression, this.matcher);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullExpression() {
new MessageExpressionConfigAttribute(null, this.matcher);
assertThatIllegalArgumentException().isThrownBy(() -> new MessageExpressionConfigAttribute(null, this.matcher));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullMatcher() {
new MessageExpressionConfigAttribute(this.expression, null);
assertThatIllegalArgumentException()
.isThrownBy(() -> new MessageExpressionConfigAttribute(this.expression, null));
}
@Test
@@ -36,6 +36,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.messaging.util.matcher.MessageMatcher;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
@@ -115,9 +116,9 @@ public class MessageExpressionVoterTests {
assertThat(this.voter.supports(new MessageExpressionConfigAttribute(this.expression, this.matcher))).isTrue();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setExpressionHandlerNull() {
this.voter.setExpressionHandler(null);
assertThatIllegalArgumentException().isThrownBy(() -> this.voter.setExpressionHandler(null));
}
@Test
@@ -39,6 +39,8 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
@@ -86,9 +88,9 @@ public class ChannelSecurityInterceptorTests {
SecurityContextHolder.clearContext();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorMessageSecurityMetadataSourceNull() {
new ChannelSecurityInterceptor(null);
assertThatIllegalArgumentException().isThrownBy(() -> new ChannelSecurityInterceptor(null));
}
@Test
@@ -113,12 +115,13 @@ public class ChannelSecurityInterceptorTests {
assertThat(result).isSameAs(this.message);
}
@Test(expected = AccessDeniedException.class)
@Test
public void preSendDeny() {
given(this.source.getAttributes(this.message)).willReturn(this.attrs);
willThrow(new AccessDeniedException("")).given(this.accessDecisionManager).decide(any(Authentication.class),
eq(this.message), eq(this.attrs));
this.interceptor.preSend(this.message, this.channel);
assertThatExceptionOfType(AccessDeniedException.class)
.isThrownBy(() -> this.interceptor.preSend(this.message, this.channel));
}
@SuppressWarnings("unchecked")
@@ -36,6 +36,7 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Rob Winch
@@ -138,16 +139,18 @@ public class AuthenticationPrincipalArgumentResolverTests {
assertThat(this.resolver.resolveArgument(showUserAnnotationString(), null)).isNull();
}
@Test(expected = ClassCastException.class)
@Test
public void resolveArgumentErrorOnInvalidType() throws Exception {
setAuthenticationPrincipal(new CustomUserPrincipal());
this.resolver.resolveArgument(showUserAnnotationErrorOnInvalidType(), null);
assertThatExceptionOfType(ClassCastException.class)
.isThrownBy(() -> this.resolver.resolveArgument(showUserAnnotationErrorOnInvalidType(), null));
}
@Test(expected = ClassCastException.class)
@Test
public void resolveArgumentCustomserErrorOnInvalidType() throws Exception {
setAuthenticationPrincipal(new CustomUserPrincipal());
this.resolver.resolveArgument(showUserAnnotationCurrentUserErrorOnInvalidType(), null);
assertThatExceptionOfType(ClassCastException.class).isThrownBy(
() -> this.resolver.resolveArgument(showUserAnnotationCurrentUserErrorOnInvalidType(), null));
}
@Test
@@ -36,6 +36,7 @@ import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@RunWith(MockitoJUnitRunner.class)
public class SecurityContextChannelInterceptorTests {
@@ -71,9 +72,9 @@ public class SecurityContextChannelInterceptorTests {
SecurityContextHolder.clearContext();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullHeader() {
new SecurityContextChannelInterceptor(null);
assertThatIllegalArgumentException().isThrownBy(() -> new SecurityContextChannelInterceptor(null));
}
@Test
@@ -92,9 +93,9 @@ public class SecurityContextChannelInterceptorTests {
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setAnonymousAuthenticationNull() {
this.interceptor.setAnonymousAuthentication(null);
assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.setAnonymousAuthentication(null));
}
@Test
@@ -28,6 +28,8 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.messaging.Message;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.mockito.BDDMockito.given;
@RunWith(MockitoJUnitRunner.class)
@@ -44,35 +46,37 @@ public class AndMessageMatcherTests {
private MessageMatcher<Object> matcher;
@Test(expected = NullPointerException.class)
@Test
public void constructorNullArray() {
new AndMessageMatcher<>((MessageMatcher<Object>[]) null);
assertThatNullPointerException().isThrownBy(() -> new AndMessageMatcher<>((MessageMatcher<Object>[]) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorArrayContainsNull() {
new AndMessageMatcher<>((MessageMatcher<Object>) null);
assertThatIllegalArgumentException().isThrownBy(() -> new AndMessageMatcher<>((MessageMatcher<Object>) null));
}
@Test
@SuppressWarnings("unchecked")
@Test(expected = IllegalArgumentException.class)
public void constructorEmptyArray() {
new AndMessageMatcher<>(new MessageMatcher[0]);
assertThatIllegalArgumentException().isThrownBy(() -> new AndMessageMatcher<>(new MessageMatcher[0]));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullList() {
new AndMessageMatcher<>((List<MessageMatcher<Object>>) null);
assertThatIllegalArgumentException()
.isThrownBy(() -> new AndMessageMatcher<>((List<MessageMatcher<Object>>) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorListContainsNull() {
new AndMessageMatcher<>(Arrays.asList((MessageMatcher<Object>) null));
assertThatIllegalArgumentException()
.isThrownBy(() -> new AndMessageMatcher<>(Arrays.asList((MessageMatcher<Object>) null)));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorEmptyList() {
new AndMessageMatcher<>(Collections.emptyList());
assertThatIllegalArgumentException().isThrownBy(() -> new AndMessageMatcher<>(Collections.emptyList()));
}
@Test
@@ -28,6 +28,8 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.messaging.Message;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.mockito.BDDMockito.given;
@RunWith(MockitoJUnitRunner.class)
@@ -44,35 +46,37 @@ public class OrMessageMatcherTests {
private MessageMatcher<Object> matcher;
@Test(expected = NullPointerException.class)
@Test
public void constructorNullArray() {
new OrMessageMatcher<>((MessageMatcher<Object>[]) null);
assertThatNullPointerException().isThrownBy(() -> new OrMessageMatcher<>((MessageMatcher<Object>[]) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorArrayContainsNull() {
new OrMessageMatcher<>((MessageMatcher<Object>) null);
assertThatIllegalArgumentException().isThrownBy(() -> new OrMessageMatcher<>((MessageMatcher<Object>) null));
}
@Test
@SuppressWarnings("unchecked")
@Test(expected = IllegalArgumentException.class)
public void constructorEmptyArray() {
new OrMessageMatcher<>(new MessageMatcher[0]);
assertThatIllegalArgumentException().isThrownBy(() -> new OrMessageMatcher<>(new MessageMatcher[0]));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullList() {
new OrMessageMatcher<>((List<MessageMatcher<Object>>) null);
assertThatIllegalArgumentException()
.isThrownBy(() -> new OrMessageMatcher<>((List<MessageMatcher<Object>>) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorListContainsNull() {
new OrMessageMatcher<>(Arrays.asList((MessageMatcher<Object>) null));
assertThatIllegalArgumentException()
.isThrownBy(() -> new OrMessageMatcher<>(Arrays.asList((MessageMatcher<Object>) null)));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorEmptyList() {
new OrMessageMatcher<>(Collections.emptyList());
assertThatIllegalArgumentException().isThrownBy(() -> new OrMessageMatcher<>(Collections.emptyList()));
}
@Test
@@ -26,6 +26,7 @@ import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
public class SimpDestinationMessageMatcherTests {
@@ -42,9 +43,9 @@ public class SimpDestinationMessageMatcherTests {
this.pathMatcher = new AntPathMatcher();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorPatternNull() {
new SimpDestinationMessageMatcher(null);
assertThatIllegalArgumentException().isThrownBy(() -> new SimpDestinationMessageMatcher(null));
}
public void constructorOnlyPathNoError() {
@@ -25,6 +25,7 @@ import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.support.MessageBuilder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
public class SimpMessageTypeMatcherTests {
@@ -35,9 +36,9 @@ public class SimpMessageTypeMatcherTests {
this.matcher = new SimpMessageTypeMatcher(SimpMessageType.MESSAGE);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullType() {
new SimpMessageTypeMatcher(null);
assertThatIllegalArgumentException().isThrownBy(() -> new SimpMessageTypeMatcher(null));
}
@Test
@@ -35,6 +35,8 @@ import org.springframework.security.web.csrf.DefaultCsrfToken;
import org.springframework.security.web.csrf.InvalidCsrfTokenException;
import org.springframework.security.web.csrf.MissingCsrfTokenException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@RunWith(MockitoJUnitRunner.class)
public class CsrfChannelInterceptorTests {
@@ -110,28 +112,32 @@ public class CsrfChannelInterceptorTests {
this.interceptor.preSend(message(), this.channel);
}
@Test(expected = InvalidCsrfTokenException.class)
@Test
public void preSendNoToken() {
this.messageHeaders.removeNativeHeader(this.token.getHeaderName());
this.interceptor.preSend(message(), this.channel);
assertThatExceptionOfType(InvalidCsrfTokenException.class)
.isThrownBy(() -> this.interceptor.preSend(message(), this.channel));
}
@Test(expected = InvalidCsrfTokenException.class)
@Test
public void preSendInvalidToken() {
this.messageHeaders.setNativeHeader(this.token.getHeaderName(), this.token.getToken() + "invalid");
this.interceptor.preSend(message(), this.channel);
assertThatExceptionOfType(InvalidCsrfTokenException.class)
.isThrownBy(() -> this.interceptor.preSend(message(), this.channel));
}
@Test(expected = MissingCsrfTokenException.class)
@Test
public void preSendMissingToken() {
this.messageHeaders.getSessionAttributes().clear();
this.interceptor.preSend(message(), this.channel);
assertThatExceptionOfType(MissingCsrfTokenException.class)
.isThrownBy(() -> this.interceptor.preSend(message(), this.channel));
}
@Test(expected = MissingCsrfTokenException.class)
@Test
public void preSendMissingTokenNullSessionAttributes() {
this.messageHeaders.setSessionAttributes(null);
this.interceptor.preSend(message(), this.channel);
assertThatExceptionOfType(MissingCsrfTokenException.class)
.isThrownBy(() -> this.interceptor.preSend(message(), this.channel));
}
private Message<String> message() {