Replace ExpectedException @Rules with AssertJ

Replace JUnit ExpectedException @Rules with AssertJ calls.
This commit is contained in:
Phillip Webb
2020-09-10 18:40:27 -07:00
committed by Josh Cummings
parent 910b81928f
commit 20baa7d409
24 changed files with 383 additions and 543 deletions

View File

@@ -22,9 +22,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@@ -35,6 +33,7 @@ import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.core.AuthenticationException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
@@ -48,9 +47,6 @@ import static org.mockito.Mockito.verifyZeroInteractions;
@RunWith(MockitoJUnitRunner.class)
public class DelegatingAuthenticationFailureHandlerTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Mock
private AuthenticationFailureHandler handler1;
@@ -110,24 +106,24 @@ public class DelegatingAuthenticationFailureHandlerTests {
@Test
public void handlersIsNull() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("handlers cannot be null or empty");
new DelegatingAuthenticationFailureHandler(null, this.defaultHandler);
assertThatIllegalArgumentException()
.isThrownBy(() -> new DelegatingAuthenticationFailureHandler(null, this.defaultHandler))
.withMessage("handlers cannot be null or empty");
}
@Test
public void handlersIsEmpty() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("handlers cannot be null or empty");
new DelegatingAuthenticationFailureHandler(this.handlers, this.defaultHandler);
assertThatIllegalArgumentException()
.isThrownBy(() -> new DelegatingAuthenticationFailureHandler(this.handlers, this.defaultHandler))
.withMessage("handlers cannot be null or empty");
}
@Test
public void defaultHandlerIsNull() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("defaultHandler cannot be null");
this.handlers.put(BadCredentialsException.class, this.handler1);
new DelegatingAuthenticationFailureHandler(this.handlers, null);
assertThatIllegalArgumentException()
.isThrownBy(() -> new DelegatingAuthenticationFailureHandler(this.handlers, null))
.withMessage("defaultHandler cannot be null");
}
}

View File

@@ -22,9 +22,7 @@ import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.InOrder;
import org.springframework.security.core.Authentication;
@@ -45,14 +43,10 @@ import static org.mockito.Mockito.verify;
*/
public class CompositeLogoutHandlerTests {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void buildEmptyCompositeLogoutHandlerThrowsException() {
this.exception.expect(IllegalArgumentException.class);
this.exception.expectMessage("LogoutHandlers are required");
new CompositeLogoutHandler();
assertThatIllegalArgumentException().isThrownBy(() -> new CompositeLogoutHandler())
.withMessage("LogoutHandlers are required");
}
@Test

View File

@@ -16,15 +16,14 @@
package org.springframework.security.web.authentication.logout;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.core.Authentication;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
/**
@@ -34,23 +33,18 @@ import static org.mockito.Mockito.mock;
*/
public class ForwardLogoutSuccessHandlerTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void invalidTargetUrl() {
String targetUrl = "not.valid";
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("'" + targetUrl + "' is not a valid target URL");
new ForwardLogoutSuccessHandler(targetUrl);
assertThatIllegalArgumentException().isThrownBy(() -> new ForwardLogoutSuccessHandler(targetUrl))
.withMessage("'" + targetUrl + "' is not a valid target URL");
}
@Test
public void emptyTargetUrl() {
String targetUrl = " ";
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("'" + targetUrl + "' is not a valid target URL");
new ForwardLogoutSuccessHandler(targetUrl);
assertThatIllegalArgumentException().isThrownBy(() -> new ForwardLogoutSuccessHandler(targetUrl))
.withMessage("'" + targetUrl + "' is not a valid target URL");
}
@Test

View File

@@ -17,15 +17,14 @@
package org.springframework.security.web.authentication.logout;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.header.HeaderWriter;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -40,9 +39,6 @@ public class HeaderWriterLogoutHandlerTests {
private MockHttpServletRequest request;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void setup() {
this.response = new MockHttpServletResponse();
@@ -51,9 +47,8 @@ public class HeaderWriterLogoutHandlerTests {
@Test
public void constructorWhenHeaderWriterIsNullThenThrowsException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("headerWriter cannot be null");
new HeaderWriterLogoutHandler(null);
assertThatIllegalArgumentException().isThrownBy(() -> new HeaderWriterLogoutHandler(null))
.withMessage("headerWriter cannot be null");
}
@Test

View File

@@ -23,9 +23,7 @@ import javax.servlet.FilterChain;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -49,6 +47,7 @@ import org.springframework.security.web.authentication.SimpleUrlAuthenticationSu
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -64,9 +63,6 @@ public class SwitchUserFilterTests {
private static final List<GrantedAuthority> ROLES_12 = AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO");
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void authenticateCurrentUser() {
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("dano", "hawaii50");
@@ -437,9 +433,8 @@ public class SwitchUserFilterTests {
// gh-3697
@Test
public void switchAuthorityRoleCannotBeNull() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("switchAuthorityRole cannot be null");
switchToUserWithAuthorityRole("dano", null);
assertThatIllegalArgumentException().isThrownBy(() -> switchToUserWithAuthorityRole("dano", null))
.withMessage("switchAuthorityRole cannot be null");
}
// gh-3697

View File

@@ -20,9 +20,7 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
@@ -35,8 +33,7 @@ import static org.mockito.Mockito.verify;
*/
public class FirewalledResponseTests {
@Rule
public ExpectedException expectedException = ExpectedException.none();
private static final String CRLF_MESSAGE = "Invalid characters (CR/LF)";
private HttpServletResponse response;
@@ -62,8 +59,8 @@ public class FirewalledResponseTests {
@Test
public void sendRedirectWhenHasCrlfThenThrowsException() throws Exception {
expectCrlfValidationException();
this.fwResponse.sendRedirect("/theURL\r\nsomething");
assertThatIllegalArgumentException().isThrownBy(() -> this.fwResponse.sendRedirect("/theURL\r\nsomething"))
.withMessageContaining(CRLF_MESSAGE);
}
@Test
@@ -80,14 +77,16 @@ public class FirewalledResponseTests {
@Test
public void addHeaderWhenHeaderValueHasCrlfThenException() {
expectCrlfValidationException();
this.fwResponse.addHeader("foo", "abc\r\nContent-Length:100");
assertThatIllegalArgumentException()
.isThrownBy(() -> this.fwResponse.addHeader("foo", "abc\r\nContent-Length:100"))
.withMessageContaining(CRLF_MESSAGE);
}
@Test
public void addHeaderWhenHeaderNameHasCrlfThenException() {
expectCrlfValidationException();
this.fwResponse.addHeader("abc\r\nContent-Length:100", "bar");
assertThatIllegalArgumentException()
.isThrownBy(() -> this.fwResponse.addHeader("abc\r\nContent-Length:100", "bar"))
.withMessageContaining(CRLF_MESSAGE);
}
@Test
@@ -115,39 +114,39 @@ public class FirewalledResponseTests {
return "foo\r\nbar";
}
};
expectCrlfValidationException();
this.fwResponse.addCookie(cookie);
assertThatIllegalArgumentException().isThrownBy(() -> this.fwResponse.addCookie(cookie))
.withMessageContaining(CRLF_MESSAGE);
}
@Test
public void addCookieWhenCookieValueContainsCrlfThenException() {
Cookie cookie = new Cookie("foo", "foo\r\nbar");
expectCrlfValidationException();
this.fwResponse.addCookie(cookie);
assertThatIllegalArgumentException().isThrownBy(() -> this.fwResponse.addCookie(cookie))
.withMessageContaining(CRLF_MESSAGE);
}
@Test
public void addCookieWhenCookiePathContainsCrlfThenException() {
Cookie cookie = new Cookie("foo", "bar");
cookie.setPath("/foo\r\nbar");
expectCrlfValidationException();
this.fwResponse.addCookie(cookie);
assertThatIllegalArgumentException().isThrownBy(() -> this.fwResponse.addCookie(cookie))
.withMessageContaining(CRLF_MESSAGE);
}
@Test
public void addCookieWhenCookieDomainContainsCrlfThenException() {
Cookie cookie = new Cookie("foo", "bar");
cookie.setDomain("foo\r\nbar");
expectCrlfValidationException();
this.fwResponse.addCookie(cookie);
assertThatIllegalArgumentException().isThrownBy(() -> this.fwResponse.addCookie(cookie))
.withMessageContaining(CRLF_MESSAGE);
}
@Test
public void addCookieWhenCookieCommentContainsCrlfThenException() {
Cookie cookie = new Cookie("foo", "bar");
cookie.setComment("foo\r\nbar");
expectCrlfValidationException();
this.fwResponse.addCookie(cookie);
assertThatIllegalArgumentException().isThrownBy(() -> this.fwResponse.addCookie(cookie))
.withMessageContaining(CRLF_MESSAGE);
}
@Test
@@ -160,11 +159,6 @@ public class FirewalledResponseTests {
validateLineEnding("foo\nbar", "bar");
}
private void expectCrlfValidationException() {
this.expectedException.expect(IllegalArgumentException.class);
this.expectedException.expectMessage("Invalid characters (CR/LF)");
}
private void validateLineEnding(String name, String value) {
assertThatIllegalArgumentException().isThrownBy(() -> this.fwResponse.validateCrlf(name, value));
}

View File

@@ -17,15 +17,14 @@
package org.springframework.security.web.header.writers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter.Directive;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Rafiullah Hamedy
@@ -40,9 +39,6 @@ public class ClearSiteDataHeaderWriterTests {
private MockHttpServletResponse response;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void setup() {
this.request = new MockHttpServletRequest();
@@ -52,9 +48,8 @@ public class ClearSiteDataHeaderWriterTests {
@Test
public void createInstanceWhenMissingSourceThenThrowsException() {
this.thrown.expect(Exception.class);
this.thrown.expectMessage("directives cannot be empty or null");
new ClearSiteDataHeaderWriter();
assertThatExceptionOfType(Exception.class).isThrownBy(() -> new ClearSiteDataHeaderWriter())
.withMessage("directives cannot be empty or null");
}
@Test

View File

@@ -20,9 +20,7 @@ import java.security.Principal;
import java.util.Collections;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
@@ -55,7 +53,8 @@ import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.server.WebFilterChain;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
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;
@@ -83,9 +82,6 @@ public class SwitchUserWebFilterTests {
@Mock
private ServerSecurityContextRepository serverSecurityContextRepository;
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@Before
public void setUp() {
this.switchUserWebFilter = new SwitchUserWebFilter(this.userDetailsService, this.successHandler,
@@ -183,11 +179,12 @@ public class SwitchUserWebFilterTests {
.from(MockServerHttpRequest.post("/login/impersonate"));
final WebFilterChain chain = mock(WebFilterChain.class);
final SecurityContextImpl securityContext = new SecurityContextImpl(mock(Authentication.class));
this.exceptionRule.expect(IllegalArgumentException.class);
this.exceptionRule.expectMessage("The userName can not be null.");
this.switchUserWebFilter.filter(exchange, chain)
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(securityContext)))
.block();
assertThatIllegalArgumentException()
.isThrownBy(() -> this.switchUserWebFilter.filter(exchange, chain)
.subscriberContext(
ReactiveSecurityContextHolder.withSecurityContext(Mono.just(securityContext)))
.block())
.withMessage("The userName can not be null.");
verifyNoInteractions(chain);
}
@@ -219,10 +216,12 @@ public class SwitchUserWebFilterTests {
final SecurityContextImpl securityContext = new SecurityContextImpl(mock(Authentication.class));
final UserDetails switchUserDetails = switchUserDetails(targetUsername, false);
given(this.userDetailsService.findByUsername(any(String.class))).willReturn(Mono.just(switchUserDetails));
this.exceptionRule.expect(DisabledException.class);
this.switchUserWebFilter.filter(exchange, chain)
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(securityContext)))
.block();
assertThatExceptionOfType(DisabledException.class)
.isThrownBy(
() -> this.switchUserWebFilter.filter(exchange, chain)
.subscriberContext(
ReactiveSecurityContextHolder.withSecurityContext(Mono.just(securityContext)))
.block());
verifyNoInteractions(chain);
}
@@ -265,11 +264,12 @@ public class SwitchUserWebFilterTests {
"origCredentials");
final WebFilterChain chain = mock(WebFilterChain.class);
final SecurityContextImpl securityContext = new SecurityContextImpl(originalAuthentication);
this.exceptionRule.expect(AuthenticationCredentialsNotFoundException.class);
this.exceptionRule.expectMessage("Could not find original Authentication object");
this.switchUserWebFilter.filter(exchange, chain)
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(securityContext)))
.block();
assertThatExceptionOfType(AuthenticationCredentialsNotFoundException.class)
.isThrownBy(() -> this.switchUserWebFilter.filter(exchange, chain)
.subscriberContext(
ReactiveSecurityContextHolder.withSecurityContext(Mono.just(securityContext)))
.block())
.withMessage("Could not find original Authentication object");
verifyNoInteractions(chain);
}
@@ -278,34 +278,35 @@ public class SwitchUserWebFilterTests {
final MockServerWebExchange exchange = MockServerWebExchange
.from(MockServerHttpRequest.post("/logout/impersonate"));
final WebFilterChain chain = mock(WebFilterChain.class);
this.exceptionRule.expect(AuthenticationCredentialsNotFoundException.class);
this.exceptionRule.expectMessage("No current user associated with this request");
this.switchUserWebFilter.filter(exchange, chain).block();
assertThatExceptionOfType(AuthenticationCredentialsNotFoundException.class)
.isThrownBy(() -> this.switchUserWebFilter.filter(exchange, chain).block())
.withMessage("No current user associated with this request");
verifyNoInteractions(chain);
}
@Test
public void constructorUserDetailsServiceRequired() {
this.exceptionRule.expect(IllegalArgumentException.class);
this.exceptionRule.expectMessage("userDetailsService must be specified");
this.switchUserWebFilter = new SwitchUserWebFilter(null, mock(ServerAuthenticationSuccessHandler.class),
mock(ServerAuthenticationFailureHandler.class));
assertThatIllegalArgumentException()
.isThrownBy(() -> this.switchUserWebFilter = new SwitchUserWebFilter(null,
mock(ServerAuthenticationSuccessHandler.class), mock(ServerAuthenticationFailureHandler.class)))
.withMessage("userDetailsService must be specified");
}
@Test
public void constructorServerAuthenticationSuccessHandlerRequired() {
this.exceptionRule.expect(IllegalArgumentException.class);
this.exceptionRule.expectMessage("successHandler must be specified");
this.switchUserWebFilter = new SwitchUserWebFilter(mock(ReactiveUserDetailsService.class), null,
mock(ServerAuthenticationFailureHandler.class));
assertThatIllegalArgumentException()
.isThrownBy(
() -> this.switchUserWebFilter = new SwitchUserWebFilter(mock(ReactiveUserDetailsService.class),
null, mock(ServerAuthenticationFailureHandler.class)))
.withMessage("successHandler must be specified");
}
@Test
public void constructorSuccessTargetUrlRequired() {
this.exceptionRule.expect(IllegalArgumentException.class);
this.exceptionRule.expectMessage("successTargetUrl must be specified");
this.switchUserWebFilter = new SwitchUserWebFilter(mock(ReactiveUserDetailsService.class), null,
"failure/target/url");
assertThatIllegalArgumentException().isThrownBy(
() -> this.switchUserWebFilter = new SwitchUserWebFilter(mock(ReactiveUserDetailsService.class), null,
"failure/target/url"))
.withMessage("successTargetUrl must be specified");
}
@Test
@@ -336,10 +337,9 @@ public class SwitchUserWebFilterTests {
@Test
public void setSecurityContextRepositoryWhenNullThenThrowException() {
this.exceptionRule.expect(IllegalArgumentException.class);
this.exceptionRule.expectMessage("securityContextRepository cannot be null");
this.switchUserWebFilter.setSecurityContextRepository(null);
fail("Test should fail with exception");
assertThatIllegalArgumentException()
.isThrownBy(() -> this.switchUserWebFilter.setSecurityContextRepository(null))
.withMessage("securityContextRepository cannot be null");
}
@Test
@@ -357,18 +357,14 @@ public class SwitchUserWebFilterTests {
@Test
public void setExitUserUrlWhenNullThenThrowException() {
this.exceptionRule.expect(IllegalArgumentException.class);
this.exceptionRule.expectMessage("exitUserUrl cannot be empty and must be a valid redirect URL");
this.switchUserWebFilter.setExitUserUrl(null);
fail("Test should fail with exception");
assertThatIllegalArgumentException().isThrownBy(() -> this.switchUserWebFilter.setExitUserUrl(null))
.withMessage("exitUserUrl cannot be empty and must be a valid redirect URL");
}
@Test
public void setExitUserUrlWhenInvalidUrlThenThrowException() {
this.exceptionRule.expect(IllegalArgumentException.class);
this.exceptionRule.expectMessage("exitUserUrl cannot be empty and must be a valid redirect URL");
this.switchUserWebFilter.setExitUserUrl("wrongUrl");
fail("Test should fail with exception");
assertThatIllegalArgumentException().isThrownBy(() -> this.switchUserWebFilter.setExitUserUrl("wrongUrl"))
.withMessage("exitUserUrl cannot be empty and must be a valid redirect URL");
}
@Test
@@ -387,10 +383,8 @@ public class SwitchUserWebFilterTests {
@Test
public void setExitUserMatcherWhenNullThenThrowException() {
this.exceptionRule.expect(IllegalArgumentException.class);
this.exceptionRule.expectMessage("exitUserMatcher cannot be null");
this.switchUserWebFilter.setExitUserMatcher(null);
fail("Test should fail with exception");
assertThatIllegalArgumentException().isThrownBy(() -> this.switchUserWebFilter.setExitUserMatcher(null))
.withMessage("exitUserMatcher cannot be null");
}
@Test
@@ -410,18 +404,14 @@ public class SwitchUserWebFilterTests {
@Test
public void setSwitchUserUrlWhenNullThenThrowException() {
this.exceptionRule.expect(IllegalArgumentException.class);
this.exceptionRule.expectMessage("switchUserUrl cannot be empty and must be a valid redirect URL");
this.switchUserWebFilter.setSwitchUserUrl(null);
fail("Test should fail with exception");
assertThatIllegalArgumentException().isThrownBy(() -> this.switchUserWebFilter.setSwitchUserUrl(null))
.withMessage("switchUserUrl cannot be empty and must be a valid redirect URL");
}
@Test
public void setSwitchUserUrlWhenInvalidThenThrowException() {
this.exceptionRule.expect(IllegalArgumentException.class);
this.exceptionRule.expectMessage("switchUserUrl cannot be empty and must be a valid redirect URL");
this.switchUserWebFilter.setSwitchUserUrl("wrongUrl");
fail("Test should fail with exception");
assertThatIllegalArgumentException().isThrownBy(() -> this.switchUserWebFilter.setSwitchUserUrl("wrongUrl"))
.withMessage("switchUserUrl cannot be empty and must be a valid redirect URL");
}
@Test
@@ -440,10 +430,8 @@ public class SwitchUserWebFilterTests {
@Test
public void setSwitchUserMatcherWhenNullThenThrowException() {
this.exceptionRule.expect(IllegalArgumentException.class);
this.exceptionRule.expectMessage("switchUserMatcher cannot be null");
this.switchUserWebFilter.setSwitchUserMatcher(null);
fail("Test should fail with exception");
assertThatIllegalArgumentException().isThrownBy(() -> this.switchUserWebFilter.setSwitchUserMatcher(null))
.withMessage("switchUserMatcher cannot be null");
}
@Test