question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@SpringBootTest(webEnvironment = NONE) + @EnableWebSecurity causes exception

See original GitHub issue

After upgrade Spring Boot from 1.5.8.RELEASE to 1.5.10.RELEASE some of my previously working tests started to fail with very strange message.

***************************
APPLICATION FAILED TO START
***************************

Description:

Method springSecurityFilterChain in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration required a bean named 'A Bean named mvcHandlerMappingIntrospector of type org.springframework.web.servlet.handler.HandlerMappingIntrospector is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.' that could not be found.


Action:

Consider defining a bean named 'A Bean named mvcHandlerMappingIntrospector of type org.springframework.web.servlet.handler.HandlerMappingIntrospector is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.' in your configuration.

Consider defining a bean named ‘A Bean named mvcHandlerMappingIntrospector of type org.springframework.web.servlet.handler.HandlerMappingIntrospector is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.’

Very cool name for the bean I should say! 😃

Apart to the problem that description somehow became part of bean name, I can understand that probably the root problem is that with @SpringBootTest(webEnvironment = NONE) @EnableWebSecurity somehow can’t be fully initialized. But why it stopped to work in 1.5.10 (it worked in 1.5.8 and 1.5.9).

So, is it regression or I need to explicitly exclude all web-related beans from the test configuration from now? 😞

MVCE: https://github.com/xak2000/mvc-handler-interceptor-bug

Maybe #10943 is related. I think this commit causes the error (if it was backported to 4.x). But I’m not sure what to think about it.

Maybe we should register mvcHandlerMappingIntrospector in test context even when webEnvironment = NONE to skip this check, maybe it is bad… Or maybe the configuration bean, which extends WebSecurityConfigurerAdapter should be excluded from test context automatically. Or maybe it should be fixed somehow on spring-security side… But inability to throw away all WEB-related stuff in just one line (webEnvironment = NONE) is not very convinient. Especially if this worked before.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
philwebbcommented, Feb 14, 2018

OK, so here’s what I think is happening. Running with @SpringBootTest(webEnvironment = NONE) means that the WebMvcAutoConfiguration doesn’t kick in. This means WebMvcConfigurationSupport isn’t registered and no mvcHandlerMappingIntrospector bean is created.

The update to Spring Security means that you’re now getting a hard failure. I presume that previously the matcher would have failed if it were invoked, but obviously it never would be in such a test.

Since this is your own @Configuration class, I don’t think there’s much that we can do in Spring Boot to stop this error. The easiest quick fix is to guard your SecurityConfig with @ConditionalOnWebApplication.

@rwinch Is there any way to make those introspection lookups more lenient?

1reaction
philwebbcommented, Mar 2, 2018

OK, I’m sorry to say I don’t think there’s much we can do directly to help with this. Adding @ConditionalOnWebApplication in your own code seems to be the only option.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Disable security for unit tests with spring boot - Stack Overflow
I get an exception indicating that it can't find an ObjectPostProcessor and thus can't bring up the container. Caused by: org.springframework.beans.factory.
Read more >
Spring Boot Security Auto-Configuration - Baeldung
This causes problems because another auto-configuration class needs the one we've just excluded. So, the application will fail to start.
Read more >
Spring Boot Reference Documentation
Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, ...
Read more >
Guide to @SpringBootTest for Spring Boot Integration Tests
Understand when, why and how to use the @SpringBootTest annotation to write ... Rarely Applicable for Integration Tests: WebEnvironment.NONE.
Read more >
Java – Spring Boot extend existing properties class with new prefix ...
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = NONE) ... instantiateUsingFactoryMethod(ConstructorResolver.java:467) Caused by: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found