Skip to content

DefaultMockMvcBuilder combined with @ContextHierarchy registers wrong context as ROOT [SPR-13075] #17667

@spring-projects-issues

Description

@spring-projects-issues

Joris Kuipers opened SPR-13075 and commented

When you have test class that looks like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy({
    @ContextConfiguration(classes = DemoApplicationTests.RootConfig.class),
    @ContextConfiguration(classes = DemoApplicationTests.ServletConfig.class)
})
@WebAppConfiguration
public class DemoApplicationTests {
    // ....

... and you bootstrap a MockMvc variable using MockMvcBuilders.webAppContextSetup(applicationContext).build(), you end up with the wrong WebApplicationContext getting registered as the ROOT context in the mocked ServletContext.

We found this when upgrading an app using the Thymeleaf Spring Security dialect: their code expects some security expression handler bean which we register in the root context, but since the wrong context is found the code fails.

The corresponding code change was introduced in Spring 4.1.4, probably as part of #17155.

There's a check missing to see if the given context is in fact the root context in DefaultMockMvcBuilder#initWebAppContext(). A similar check is done in other places of the test support (like the AbstractGenericWebContextLoader).

I've attached a project with (only) a test case to reproduce the issue. By commenting out the line of code which created the MockMvc or by lowering the Spring version to 4.1.3 the test case passes, but as given it fails. I think it should succeed, and the most likely solution seems to navigate the context hierarchy to find the real root context in DefaultMockMvcBuilder#initWebAppContext(): I'll leave that up to you guys to decide. ;)


Affects: 4.1.4, 4.1.5, 4.1.6

Attachments:

Referenced from: commits spring-attic/spring-framework-issues@321c4c3

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