Skip to content

Printing of session with null attributes results in NullPointerException [SPR-16164] #20712

@spring-projects-issues

Description

@spring-projects-issues

Randy Cramp opened SPR-16164 and commented

After migrating from Spring 4.3.12, we're now seeing NullPointerExceptions as a result of the code for #19755. During debugging, we see that the attributes field on the session is null. As a result, the Collections.list(session.getAttributeNames)) throws the NPE.

Partial stacktrace:

java.lang.NullPointerException
at java.util.Collections.list(Collections.java:5239)
at org.springframework.test.web.servlet.result.PrintingResultHandler.getSessionAttributes(PrintingResultHandler.java:152)
at org.springframework.test.web.servlet.result.PrintingResultHandler.printRequest(PrintingResultHandler.java:120)
at org.springframework.test.web.servlet.result.PrintingResultHandler.handle(PrintingResultHandler.java:87)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:184)

Code snippet:

MockHttpSession session = Mockito.mock(MockHttpSession.class);

MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/dummy-url")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.header(HEADER_ENDPOINT, endpointUrl)
.content(someContent);
requestBuilder.session(session);

MockMvc mockMvc = MockMvcBuilders.standaloneSetup(endpointController).build();
mockMvc.perform(requestBuilder)
.andDo(print())
.andExpect(status().isOk());


Affects: 5.0.1

Reference URL: #19755

Issue Links:

Referenced from: commits 4ec60f0

Metadata

Metadata

Assignees

Labels

in: testIssues in the test moduletype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions