Replace expected @Test attributes with AssertJ
Replace JUnit expected @Test attributes with AssertJ calls.
This commit is contained in:
committed by
Josh Cummings
parent
20baa7d409
commit
c502312719
+4
-2
@@ -31,6 +31,7 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
@@ -42,9 +43,10 @@ public class WithMockUserTests {
|
||||
@Autowired
|
||||
private MessageService messageService;
|
||||
|
||||
@Test(expected = AuthenticationCredentialsNotFoundException.class)
|
||||
@Test
|
||||
public void getMessageUnauthenticated() {
|
||||
this.messageService.getMessage();
|
||||
assertThatExceptionOfType(AuthenticationCredentialsNotFoundException.class)
|
||||
.isThrownBy(() -> this.messageService.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+4
-2
@@ -36,6 +36,7 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
@@ -47,9 +48,10 @@ public class WithUserDetailsTests {
|
||||
@Autowired
|
||||
private MessageService messageService;
|
||||
|
||||
@Test(expected = AuthenticationCredentialsNotFoundException.class)
|
||||
@Test
|
||||
public void getMessageUnauthenticated() {
|
||||
this.messageService.getMessage();
|
||||
assertThatExceptionOfType(AuthenticationCredentialsNotFoundException.class)
|
||||
.isThrownBy(() -> this.messageService.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+8
-6
@@ -23,6 +23,8 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@@ -38,9 +40,9 @@ public class WithMockUserSecurityContextFactoryTests {
|
||||
this.factory = new WithMockUserSecurityContextFactory();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void usernameNull() {
|
||||
this.factory.createSecurityContext(this.withUser);
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.factory.createSecurityContext(this.withUser));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -83,20 +85,20 @@ public class WithMockUserSecurityContextFactoryTests {
|
||||
.extracting("authority").containsOnly("USER", "CUSTOM");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
@Test
|
||||
public void authoritiesAndRolesInvalid() {
|
||||
given(this.withUser.value()).willReturn("valueUser");
|
||||
given(this.withUser.roles()).willReturn(new String[] { "CUSTOM" });
|
||||
given(this.withUser.authorities()).willReturn(new String[] { "USER", "CUSTOM" });
|
||||
this.factory.createSecurityContext(this.withUser);
|
||||
assertThatIllegalStateException().isThrownBy(() -> this.factory.createSecurityContext(this.withUser));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void rolesWithRolePrefixFails() {
|
||||
given(this.withUser.value()).willReturn("valueUser");
|
||||
given(this.withUser.roles()).willReturn(new String[] { "ROLE_FAIL" });
|
||||
given(this.withUser.authorities()).willReturn(new String[] {});
|
||||
this.factory.createSecurityContext(this.withUser);
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.factory.createSecurityContext(this.withUser));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-4
@@ -33,6 +33,7 @@ import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
|
||||
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.verify;
|
||||
|
||||
@@ -61,15 +62,15 @@ public class WithUserDetailsSecurityContextFactoryTests {
|
||||
this.factory = new WithUserDetailsSecurityContextFactory(this.beans);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void createSecurityContextNullValue() {
|
||||
this.factory.createSecurityContext(this.withUserDetails);
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.factory.createSecurityContext(this.withUserDetails));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void createSecurityContextEmptyValue() {
|
||||
given(this.withUserDetails.value()).willReturn("");
|
||||
this.factory.createSecurityContext(this.withUserDetails);
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.factory.createSecurityContext(this.withUserDetails));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+4
-2
@@ -42,6 +42,7 @@ import org.springframework.security.test.web.support.WebTestUtils;
|
||||
import org.springframework.security.web.context.SecurityContextRepository;
|
||||
|
||||
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.Mockito.verify;
|
||||
@@ -116,9 +117,10 @@ public class SecurityMockMvcRequestPostProcessorsUserTests {
|
||||
this.authority2);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void userRolesWithRolePrefixErrors() {
|
||||
user("user").roles("ROLE_INVALID").postProcessRequest(this.request);
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> user("user").roles("ROLE_INVALID").postProcessRequest(this.request));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+10
-7
@@ -40,6 +40,7 @@ import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
|
||||
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
|
||||
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
||||
@@ -70,10 +71,10 @@ public class SecurityMockMvcResultMatchersTests {
|
||||
(auth) -> assertThat(auth).isInstanceOf(UsernamePasswordAuthenticationToken.class)));
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
@Test
|
||||
public void withAuthenticationWhenNotMatchesThenFails() throws Exception {
|
||||
this.mockMvc.perform(formLogin()).andExpect(
|
||||
authenticated().withAuthentication((auth) -> assertThat(auth.getName()).isEqualTo("notmatch")));
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> this.mockMvc.perform(formLogin()).andExpect(
|
||||
authenticated().withAuthentication((auth) -> assertThat(auth.getName()).isEqualTo("notmatch"))));
|
||||
}
|
||||
|
||||
// SEC-2719
|
||||
@@ -87,13 +88,15 @@ public class SecurityMockMvcResultMatchersTests {
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
@Test
|
||||
public void withRolesFailsIfNotAllRoles() throws Exception {
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
|
||||
// @formatter:off
|
||||
this.mockMvc
|
||||
.perform(formLogin())
|
||||
.andExpect(authenticated().withRoles("USER"));
|
||||
this.mockMvc
|
||||
.perform(formLogin())
|
||||
.andExpect(authenticated().withRoles("USER"))
|
||||
// @formatter:on
|
||||
);
|
||||
}
|
||||
|
||||
@EnableWebSecurity
|
||||
|
||||
+4
-2
@@ -42,6 +42,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
|
||||
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
|
||||
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
||||
@@ -69,11 +70,12 @@ public class SecurityMockWithAuthoritiesMvcResultMatchersTests {
|
||||
this.mockMvc.perform(formLogin()).andExpect(authenticated().withAuthorities(grantedAuthorities));
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
@Test
|
||||
public void withAuthoritiesFailsIfNotAllRoles() throws Exception {
|
||||
List<SimpleGrantedAuthority> grantedAuthorities = new ArrayList<>();
|
||||
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
|
||||
this.mockMvc.perform(formLogin()).andExpect(authenticated().withAuthorities(grantedAuthorities));
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(
|
||||
() -> this.mockMvc.perform(formLogin()).andExpect(authenticated().withAuthorities(grantedAuthorities)));
|
||||
}
|
||||
|
||||
@EnableWebSecurity
|
||||
|
||||
+3
-2
@@ -31,6 +31,7 @@ import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
@@ -86,11 +87,11 @@ public class SecurityMockMvcConfigurerTests {
|
||||
assertFilterAdded(this.filter);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
@Test
|
||||
public void beforeMockMvcCreatedNoFilter() {
|
||||
SecurityMockMvcConfigurer configurer = new SecurityMockMvcConfigurer();
|
||||
configurer.afterConfigurerAdded(this.builder);
|
||||
configurer.beforeMockMvcCreated(this.builder, this.context);
|
||||
assertThatIllegalStateException().isThrownBy(() -> configurer.beforeMockMvcCreated(this.builder, this.context));
|
||||
}
|
||||
|
||||
private void assertFilterAdded(Filter filter) {
|
||||
|
||||
Reference in New Issue
Block a user