Skip to content

Expose a TestDispatcherServlet bean in the MockMvcAutoConfiguration #13241

Closed
@mpryahin

Description

@mpryahin

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 via MockMvc (@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 to MockMvc 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 a Configuration class which creates a bean of the TestDispatcherServlet class and injects it into a MockMvc bean.
It would be great if such a behaviour is available out of the box.
Thanks in advance.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions