spring-projects / spring-projects/spring-security

Mutators throw NullPointerException when used with MockMvc setup

Open
#10,899 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: test type: bug
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with MockMvcWebTestClient.bindTo(mockMvc) and the mutator calls shown in the Spring Boot tests. Add coverage for the listed mutators and verify that each unsupported MockMvc setup fails with IllegalStateException rather than NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.