spring-projects / spring-projects/spring-security
Mutators throw NullPointerException when used with MockMvc setup
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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