Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutators throw NullPointerException when used with MockMvc setup #10899

Open
sjohnr opened this issue Feb 22, 2022 · 0 comments
Open

Mutators throw NullPointerException when used with MockMvc setup #10899

sjohnr opened this issue Feb 22, 2022 · 0 comments
Labels
in: test An issue in spring-security-test type: bug A general bug

Comments

@sjohnr
Copy link
Member

sjohnr commented Feb 22, 2022

When MockMvc is used to set up a WebTestClient, applying a mutator such as csrf() fails with a NullPointerException, but would ideally fail with a more meaningful error such as IllegalStateException to indicate it is not supported. The following Spring Boot tests illustrate the problem:

@WebMvcTest
public class WebTestClientTests {
	@Autowired
	private MockMvc mockMvc;

	@Test
	public void mockUserDetailsWhenMockMvcThenThrowsIllegalStateException() {
		UserDetails user = new User("user", "password", Collections.emptySet());
		assertThatIllegalStateException()
				.isThrownBy(() -> MockMvcWebTestClient.bindTo(mockMvc).apply(mockUser(user)).build());
	}

	@Test
	public void mockUserWhenMockMvcThenThrowsIllegalStateException() {
		assertThatIllegalStateException()
				.isThrownBy(() -> MockMvcWebTestClient.bindTo(mockMvc).apply(mockUser("user")).build());
	}

	@Test
	public void csrfWhenMockMvcThenThrowsIllegalStateException() {
		assertThatIllegalStateException()
				.isThrownBy(() -> MockMvcWebTestClient.bindTo(mockMvc).apply(csrf()).build());
	}

	@Test
	public void mockJwtWhenMockMvcThenThrowsIllegalStateException() {
		assertThatIllegalStateException()
				.isThrownBy(() -> MockMvcWebTestClient.bindTo(mockMvc).apply(mockJwt()).build());
	}

	@Test
	public void mockOpaqueTokenWhenMockMvcThenThrowsIllegalStateException() {
		assertThatIllegalStateException()
				.isThrownBy(() -> MockMvcWebTestClient.bindTo(mockMvc).apply(mockOpaqueToken()).build());
	}

	@Test
	public void mockOAuth2ClientWhenMockMvcThenThrowsIllegalStateException() {
		assertThatIllegalStateException()
				.isThrownBy(() -> MockMvcWebTestClient.bindTo(mockMvc).apply(mockOAuth2Client("test")).build());
	}

	@Test
	public void mockOAuth2LoginWhenMockMvcThenThrowsIllegalStateException() {
		assertThatIllegalStateException()
				.isThrownBy(() -> MockMvcWebTestClient.bindTo(mockMvc).apply(mockOAuth2Login()).build());
	}

	@Test
	public void mockOidcLoginWhenMockMvcThenThrowsIllegalStateException() {
		assertThatIllegalStateException()
				.isThrownBy(() -> MockMvcWebTestClient.bindTo(mockMvc).apply(mockOidcLogin()).build());
	}
}

Related gh-10841 gh-10900

@sjohnr sjohnr added in: test An issue in spring-security-test type: bug A general bug labels Feb 22, 2022
@rwinch rwinch added this to the 5.8.x milestone Jun 3, 2022
@rwinch rwinch removed this from the 5.8.x milestone Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test An issue in spring-security-test type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants