-
Notifications
You must be signed in to change notification settings - Fork 41.1k
Expose a TestDispatcherServlet bean in the MockMvcAutoConfiguration #13241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@mpryahin Does that mean you end up with two dispatcher servlets, one that's passed directly to the Could you provide a code snippet to help us understand the use-case better? |
No, it doesn't. The inspiration for my use case I found here The main point to pay attention to is that my controller autowires a I tried to test my batch controller with the standard I also saw that this servlet can be customized by customizers but it doesn't solve my main issue of not having a bean in the spring context. After that I mimicked the test configuration class that is provided by Spring with the only one difference - I exposed the
It would be great if Spring exposes a |
@mpryahin How are you able to call |
@philwebb I had to put my custom configuration class into the right package to be able to instantiate it. This is another inconvenience I wanted to ask about but decided to postpone, hoping the initial request will eventually be fixed. |
@mpryahin Thanks! That's the information we were missing. We'll not be able to do that ourselves so we'll need a framework change to support this. |
@rstoyanchev What are your thoughts on this. It would be nice if we could plug in our own dispatcher servlet to Another less invasive option might be to add a getter to |
This is a very specific scenario for which MockMvc wasn't designed for. I'm not saying there is anything wrong with it, nor that it shouldn't be supported, but just that it hasn't been considered. I'm still missing a few details about how this works. For example In MockMvc, the Does each sub-dispatch (i.e. per batched request/part) have its own request and response, and how are the multiple responses aggregated back into a single multipart response? |
@rstoyanchev Let me clarify the implementation details first to make it all clear. I have a controller which consumes a
The implementation iterates over the sub-requests objects, adapts each of them to the
The So from the It would be fantsatic if we had a chance to register a Thank you. |
Thanks @mpryahin for the extra detail ! The Given the response is multipart content, probably the built-in From the proposed options, I would go with the least invasive one, i.e. expose a getter on |
@rstoyanchev Thanks for your quick reply!
You're absolutely right, sorry for the typo, I've fixed it.
All of them work great unless I have to match a request body, in this case, a custom matcher is used to match against
The idea of this
Absolutely agree, but I would like to have a kind of integration test which insures the implementation works as expected with Spring Thanks, |
Did you mean response body? The response has multipart content, so I wouldn't expect any of the ContentResultMatchers to just work unless given only the content of a single part. I've created https://jira.spring.io/browse/SPR-16924. |
@rstoyanchev sure, I did mean response body. Thanks a lot for creating an issue. |
SPR-16924 has been resolved. |
This isn't as straightforward as I thought. @snicoll and I had a chat about this and as the customizer does part of what |
As @mbhave says above, there's a cycle because public ServletContextInitializerBeans(ListableBeanFactory beanFactory,
Class<? extends ServletContextInitializer>... initializerTypes) { And then being used like this: private void addServletContextInitializerBeans(ListableBeanFactory beanFactory) {
for (Class<? extends ServletContextInitializer> initializerType : this.initializerTypes) {
for (Entry<String, ? extends ServletContextInitializer> initializerBean : getOrderedBeansOfType(
beanFactory, initializerType)) {
addServletContextInitializerBean(initializerBean.getKey(),
initializerBean.getValue(), beanFactory);
}
}
} |
I came across an inconvenience while using
@AutoConfigureMockMvc
in my tests.I have a controller that accepts multipart requests and delegates each distinct part into a
Dispatcherservlet
instance which is autowired into that controller - just a kind of batch endpoint implementation. Everything works great until I tried to test it viaMockMvc
(@AutoConfigureMockMvc
).As it turned out a
MockMvc
bean is created in a single isolated factory method with no interaction with the application context (a dispatcher servlet instance is created as a local variable and passed directly toMockMvc
constructor instead of being looked up in the application context). This prevents my controller from being instantiated due to required been (DispatcherServlet
) is not found in the application context. I've overcome this by implementing aConfiguration
class which creates a bean of theTestDispatcherServlet
class and injects it into aMockMvc
bean.It would be great if such a behaviour is available out of the box.
Thanks in advance.
The text was updated successfully, but these errors were encountered: