Spring-boot: @WebMvcTest does not include applicationTaskExecutor

Created on 4 Sep 2018  路  12Comments  路  Source: spring-projects/spring-boot

Hi, this is a first-timers-only issue. This means we've worked to make it more legible to folks who either haven't contributed to our codebase before, or even folks who haven't contributed to open source before.

If that's you, we're interested in helping you take the first step and can answer questions and help you out as you do. Note that we're especially interested in contributions from people from groups underrepresented in free and open source software!

If you have contributed before, consider leaving this one for someone new, and looking through our general ideal-for-contribution issues. Thanks!

Problem

When an MVC controller defines an async result (for instance a Mono or Flux from Project Reactor) it has to be processed in a background thread and Spring MVC couldn't have an opinion about the pool settings to use so it creates a SimpleAsyncTaskExecutor that's not suitable for production use. In such setup, the following warning is emitted:

An Executor is required to handle java.util.concurrent.Callable return values.
Please, configure a TaskExecutor in the MVC config under "async support".
The SimpleAsyncTaskExecutor currently in use is not suitable under load.

An earlier milestone of Spring Boot 2.1 introduced support for an applicationTaskExecutor bean and a general TaskExecutorAutoConfiguration. When such bean is present, the MVC configuration is automatically configured to use it. So far so good except that if you write a test with such setup and @WebMvcTest the warning is emitted again. The problem is that TaskExecutorAutoConfiguration is not part of the auto-configurations contributed by @WebMvcTest and therefore the applicationTaskExecutor bean is not created.

Solution

We should write a test that uses @WebMvcTest and verifies the MVC configuration has been updated properly. There is an existing test that could be used for inspiration. Another solution would be to update the ExampleController so that a method returns a Mono<String> rather than String and check with an OutputCapture that the warning is not emitted.

Either way, once the test fails, we can fix the code by adding TaskExecutorAutoConfiguration to @WebMvcTest. MVC is auto-configured via @AutoConfigureWebMvc and the list of auto-configurations are in META-INF/spring.factories under the org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc key. When you add the extra entry, pay attention that they ordered alphabetically.

Steps to Fix

  • [x] Claim this issue with a comment here, below, and ask any clarifying questions you need
  • [x] Set up a repository locally following the Contributing Guidelines
  • [x] Try to fix the issue following the steps above
  • [x] Commit your changes and start a pull request.
first-timers-only superseded bug

Most helpful comment

thank you, @wilkinsona. I'm on it.

All 12 comments

I think we could consider that a bug, flagging to see what the rest of the team think.

Considering the use case with MVC I agree that the auto-configuration for the MVC async executor should be applied.

I'd like to give this a shot.

@levippaul Great! Please feel free to ask any questions at all. We'll look forward to your pull request.

Come on I'll try too ;-)

I'll give it a whirl as well !

@toonborgers Thanks for the offer, but we'd like to let @levippaul handle this one please. I hope you'll pick up another first timers only issue in the near future.

@wilkinsona Sure thing, no problem!

thank you, @wilkinsona. I'm on it.

hey @wilkinsona, I just submitted this PR for the first pass at this; Looking forward to some feedbacks.

oops, looks like that previous PR had Checkstyle violations. Let me take care of those.

Thanks very much for the PR, @levippaul. Closing this issue in favour of your proposed changes.

Was this page helpful?
0 / 5 - 0 ratings