-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
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:
- spring-bug.zip (2.38 kB)
Referenced from: commits spring-attic/spring-framework-issues@321c4c3