spring-projects / spring-boot

Spring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.
https://spring.io/projects/spring-boot
Apache License 2.0
75.22k stars 40.7k forks source link

Provide a Spring Boot-specific MockMvcBuilder that's auto-configured with Spring Security's filter, etc #2708

Closed wilkinsona closed 8 years ago

wilkinsona commented 9 years ago

The idea came from @rstoyanchev while discussing a Spring REST docs issue

rstoyanchev commented 9 years ago

I don't really know at this point if it will work out this way but it's a starting point at least for discussing ways to provide a better experience when using Spring MVC Test with Spring Boot applications.

I suppose the MockMvcBuilders#webAppContext setup (backed by DefaultMockMvcBuilder) should bring in much of the auto configuration but things like filters for example aren't picked up. Another route might be to explore whether we can use the MockServletContext to access the Filter registrations made by Boot. The best place to start would be to create a sample and identify a list of things along the lines of what the user did in https://github.com/spring-projects/spring-restdocs/issues/41 but more formally.

philwebb commented 9 years ago

Tentatively targeting to 1.3

rwinch commented 8 years ago

It might be cool if a MockMvc instance for the Spring Boot application could also be easily injected similar to @MockBean.

wilkinsona commented 8 years ago

@rwinch +1. I thought the same thing when I saw what Phil had done with @MockBean.

snicoll commented 8 years ago

+1 - I have an app that could use that feature.

philwebb commented 8 years ago

Fixed in bbc91cc03f5df31c1985811d07abe4f9a906355e

jgoldhammer commented 8 years ago

Hi Phil,

it is possible to apply springSecurity to the mockMvc anymore?

My custom code before Spring Boot 1.4 was:

MockMvc restMvc = MockMvcBuilders.webAppContextSetup(context) 
                .apply(springSecurity()).build();

Now MockMvc is autowired and I cannot add this? Or maybe I have overseen anything?

Thanks, Jens

wilkinsona commented 8 years ago

@jgoldhammer You can use a MockMvcBuilderCustomizer bean

rwinch commented 8 years ago

@wilkinsona Thanks for the response. I haven't played with the feature yet, but do we think that Spring Boot could automatically apply(springSecurity()) (if it does not already) if there is a springSecurityFilterChain bean?

wilkinsona commented 8 years ago

As things stand, any Filter beans in the context will be added to the auto-configured MockMvc instance. What does springSecurity() do beyond that?

rwinch commented 8 years ago

@wilkinsona Thanks for the response. It also registers testSecurityContext() to ensure that users can leverage features like @WithMockUser.

wilkinsona commented 8 years ago

@rwinch Thanks. I've opened #5702.

rwinch commented 8 years ago

@wilkinsona Thanks!

PS: I'd consider using apply(springSecurity()) as this should cover future use cases (i.e. if we later add other post processors that are necessary).