WebMvcTest: Migrating from WebSecurityConfigurerAdapter to SecurityFilterChain

781 Views Asked by At

As described here Spring Security deprecated WebSecurityConfigurerAdapter which I've been using for some time. I used the component based approach and introduced SecurityFilterChain and InMemoryUserDetailsManager beans (see: commit) but then one of my tests, which is using @WithMockUser failed.

Does @WebMvcTest tests work with @WithMockUser when using Spring Security component based approach (SecurityFilterChain)?

Whole project, with failing test, is located: https://github.com/pszemus/spring-security-test

1

There are 1 best solutions below

3
Marcus Hert da Coregio On BEST ANSWER

You should add @Import(YourSecurityConfiguration.class) in your test class. The @WebMvcTest is not picking up the configuration automatically, so you have to tell it explicitly which configuration to use.