Skip to content

MockMvc ignores HTTP status code overridden by filter [SPR-11760] #16382

@spring-projects-issues

Description

@spring-projects-issues

Gena Makhomed opened SPR-11760 and commented

The full source code for HttpStatusOverrideFilter is available in the attachment.

In production env., all works fine, but when I use JUnit + MockMvc, the HTTP Status Code overridden by my filter gets ignored.

Test code fragment:

@Test
public void badUri() throws Exception {
    mockMvc.perform(post("/unknown-request-uri")
            .andExpect(status().is(equalTo(230)));
}

Controller code fragment:

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(X_HTTP_STATUS_OVERRIDE, "230");
return new ResponseEntity<>("none", httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR);

In the production env., the filter works fine, and the HTTP status code is 230. With JUnit + MockMvc, the HTTP status code is 500, and the assertion fails.

Looks like this is bug in MockMvc.

Unit-test log fragment:

o.s.test.web.servlet.TestDispatcherServlet | Successfully completed request
c.p.i.util.filter.HttpStatusOverrideFilter | Status overridden '500' => '230'
.t.c.s.DirtiesContextTestExecutionListener | After test method: context
[DefaultTestContext@1895852 testClass = GateControllerTest, testInstance = com.pb.ivrcgate.controller.GateControllerTest@59e71, testMethod =
badUri@GateControllerTest, testException = java.lang.AssertionError: Response status
Expected: <230>
     but: was <500>, mergedContextConfiguration = [WebMergedContextConfiguration@f7e9dd testClass = GateControllerTest, locations =
'{classpath:/spring/context.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{dev}', resourceBasePath =
'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]], class dirties
context [false], class mode [null], method dirties context [false].

Affects: 4.0.4

Attachments:

Issue Links:

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions