Affects: spring-test:5.3.4


Hi, from time to time i get the following exception (Context: MockMvc of Spring):

java.lang.NullPointerException
  at org.springframework.mock.web.MockHttpServletResponse.doAddHeaderValue(MockHttpServletResponse.java:682)
  at org.springframework.mock.web.MockHttpServletResponse.setHeaderValue(MockHttpServletResponse.java:627)
  at org.springframework.mock.web.MockHttpServletResponse.setHeader(MockHttpServletResponse.java:601)

The according source (Spring) looks like:

private void doAddHeaderValue(String name, Object value, boolean replace) {
    Assert.notNull(value, "Header value must not be null");
    HeaderValueHolder header = this.headers.computeIfAbsent(name, key -> new HeaderValueHolder());
    if (replace) {
        header.setValue(value); // NPE here, line 682
    }
    else {
        header.addValue(value);
    }
}

In debugger, header is null. It seems impossible at the first glance, the only explanation i have is a race condition, since this.headers is

private final Map<String, HeaderValueHolder> headers = new LinkedCaseInsensitiveMap<>();

and LinkedCaseInsensitiveMap seems to be not thread safe.

Does anyone experienced similar issues and has a solution for that?

The problem is that i don't see an alternative and this makes our tests less stable

Comment From: johampel

Problem solve by setting HeaderWriterFilter.shouldWriteHeadersEagerly = true

Comment From: snicoll

Given that HeaderWriterFilter is in Spring Security, I am going to close this now as there's nothing actionable for the core framework.