You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add protected method to StandaloneMockMvcBuilder to register additional MVC infrastructure components [SPR-17520] #22052
We're heavily using Spring MVC, and its awesome Spring MVC test. Our use case is fairly advanced, for we write our custom org.springframework.web.servlet.HandlerMapping and org.springframework.web.servlet.HandlerAdapter . To make use of Spring MVC test however, we needed to duplicate org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder just to allow customization of such beans.
I found a ticket (#16166) that similarly describes the same, albeit only for:
/** * Configure factory to create a custom {@link RequestMappingHandlerMapping}. * @param factory the factory * @since 5.0 */publicStandaloneMockMvcBuildersetCustomHandlerMapping(Supplier<RequestMappingHandlerMapping> factory) {
this.handlerMappingFactory = factory;
returnthis;
}
We cannot use this method, because we're not relying on RequestMappingHandlerMapping and it doesn't yet have a setter for custom HandlerAdapter s.
Thanks for the suggestion but the StandaloneMockMvcBuilder is focused on annotated controllers and sets up the infrastructure required for that based on RequestMappingHandlerMapping, RequestMappingHandlerAdapter. It's not clear to me how any other HandlerMapping and HandlerAdapter implementations relate to the StandaloneMockMvcBuilder setup. Please provide a little more context about your situation. How much of the setup in StandaloneMockMvcBuilder do you actually use or need?
Perhaps a separate MockMvcBuilder makes more sense to begin with. Also what about using the MockMvcBuilders.webAppContextSetup(..) with a simple configuration class declaring your HandlerMapping and HandlerAdapter?
Please provide a little more context about your situation. How much of the setup in StandaloneMockMvcBuilder do you actually use or need?
Right. So I guess what I really need is the configured defaults of StubWebApplicationContext. As of the moment, I override StandaloneMockMvcBuilder#initWebAppContext, and reflectively call addBean on the StubWebApplicationContext to register my custom HandlerMapping and HandlerAdapter. Then the usual spring web MockMvc tests.
Also what about using the MockMvcBuilders.webAppContextSetup(..) with a simple configuration class declaring your HandlerMapping and HandlerAdapter?
Hmmm. So I tried just now, that approach using StaticWebApplicationContext:
In your initial comment you wrote that you have your own custom HandlerMapping and HandlerAdapter and if I understood correctly that they do not extend RequestMappingHandlerMapping and RequestMappingHandlerAdapter, right? As most of the options in StandaloneMockMvcBuilder are for initializing RequestMappingHandlerMapping and RequestMappingHandlerAdapter, then many of those wouldn't apply then?
So if my assumption is correct then I think the best option is to go with your own custom MockMvcBuilder extending from AbstractMockMvcBuilder and using StubWebApplicationContext internally. The only beans you would have to configure, in addition to your custom HandlerMapping and HandlerAdapterare these, which isn't much and unless you need to expose options to customize them, it's literally 4-5 lines of code.
...that they do not extend RequestMappingHandlerMapping and RequestMappingHandlerAdapter...
Correct.
...to go with your own custom MockMvcBuilder extending from AbstractMockMvcBuilder...
Yes and no. So we actually have 2 use cases:
Both custom HandlerMapping and HandlerAdapter, so it does use a custom MockMvcBuilder
A custom HandlerMapping, but it creates a HandlerMethod, to be handled by RequestMappingHandlerAdapter
...and using StubWebApplicationContext internally.
Yes! Although as of the moment, it is package-private, so instead I extended StandaloneMockMvcBuilder and overridden initWebAppContext so that I can reflectively add my custom HandlerMapping and HandlerAdapter.
I created a sample proof-of-concept on how I do it at the moment, just so I'm communicating my use case properly: devcsrj/bug-reports@SPR-17520. I'm not sure where exactly I should put that 4-5 lines of code you mentioned. Would love to hear your thoughts
Yes! Although as of the moment, it is package-private, so instead I extended StandaloneMockMvcBuilder and overridden initWebAppContext so that I can reflectively add my custom HandlerMapping and HandlerAdapter.
I meant to write StaticWebApplicationContext. Indeed StubWebApplicationContext is private because it's really only for internal use. It doesn't work like a regular ApplicationContext that initializes and manages beans. It's merely to allow Spring MVC to look up the beans it needs to find.
Looking at your sample code, we could probably add a protected method in StandaloneMockMvcBuilder to allow for the registration of additional MVC singleton instances. Something like this:
Activity
spring-projects-issues commentedon Nov 20, 2018
Reijhanniel Jearl Campos commented
If the Spring team sees this as a valid use case, I can make a PR for this one!
spring-projects-issues commentedon Nov 21, 2018
Rossen Stoyanchev commented
Thanks for the suggestion but the StandaloneMockMvcBuilder is focused on annotated controllers and sets up the infrastructure required for that based on RequestMappingHandlerMapping, RequestMappingHandlerAdapter. It's not clear to me how any other HandlerMapping and HandlerAdapter implementations relate to the StandaloneMockMvcBuilder setup. Please provide a little more context about your situation. How much of the setup in StandaloneMockMvcBuilder do you actually use or need?
Perhaps a separate MockMvcBuilder makes more sense to begin with. Also what about using the
MockMvcBuilders.webAppContextSetup(..)with a simple configuration class declaring your HandlerMapping and HandlerAdapter?spring-projects-issues commentedon Nov 23, 2018
Reijhanniel Jearl Campos commented
Hi Rossen Stoyanchev!
Right. So I guess what I really need is the configured defaults of StubWebApplicationContext. As of the moment, I override
StandaloneMockMvcBuilder#initWebAppContext, and reflectively calladdBeanon theStubWebApplicationContextto register my customHandlerMappingandHandlerAdapter. Then the usual spring webMockMvctests.Hmmm. So I tried just now, that approach using
StaticWebApplicationContext:The above yet doesn't work, but it seems like I have to configure a lot more beans just to get Spring web's defaults?
spring-projects-issues commentedon Nov 26, 2018
Rossen Stoyanchev commented
In your initial comment you wrote that you have your own custom
HandlerMappingandHandlerAdapterand if I understood correctly that they do not extendRequestMappingHandlerMappingandRequestMappingHandlerAdapter, right? As most of the options inStandaloneMockMvcBuilderare for initializingRequestMappingHandlerMappingandRequestMappingHandlerAdapter, then many of those wouldn't apply then?So if my assumption is correct then I think the best option is to go with your own custom
MockMvcBuilderextending fromAbstractMockMvcBuilderand usingStubWebApplicationContextinternally. The only beans you would have to configure, in addition to your customHandlerMappingandHandlerAdapterare these, which isn't much and unless you need to expose options to customize them, it's literally 4-5 lines of code.spring-projects-issues commentedon Nov 27, 2018
Reijhanniel Jearl Campos commented
Correct.
Yes and no. So we actually have 2 use cases:
HandlerMappingandHandlerAdapter, so it does use a customMockMvcBuilderHandlerMapping, but it creates aHandlerMethod, to be handled byRequestMappingHandlerAdapterYes! Although as of the moment, it is package-private, so instead I extended
StandaloneMockMvcBuilderand overridden initWebAppContext so that I can reflectively add my customHandlerMappingandHandlerAdapter.I created a sample proof-of-concept on how I do it at the moment, just so I'm communicating my use case properly: devcsrj/bug-reports@SPR-17520. I'm not sure where exactly I should put that 4-5 lines of code you mentioned. Would love to hear your thoughts
spring-projects-issues commentedon Nov 28, 2018
Rossen Stoyanchev commented
I meant to write
StaticWebApplicationContext. Indeed StubWebApplicationContext is private because it's really only for internal use. It doesn't work like a regular ApplicationContext that initializes and manages beans. It's merely to allow Spring MVC to look up the beans it needs to find.Looking at your sample code, we could probably add a protected method in StandaloneMockMvcBuilder to allow for the registration of additional MVC singleton instances. Something like this:
spring-projects-issues commentedon Dec 3, 2018
Rossen Stoyanchev commented
Reijhanniel Jearl Campos, any comments from your side?
spring-projects-issues commentedon Dec 4, 2018
Reijhanniel Jearl Campos commented
Slr, for some reason I'm not notified via email.
—
But yes, any means for my boilerplate code to get reduced and usage more seamless would be nice!
spring-projects-issues commentedon Dec 12, 2018
Rossen Stoyanchev commented
I've added a protected method.