pszemus
pszemus

Reputation: 403

WebMvcTest: Migrating from WebSecurityConfigurerAdapter to SecurityFilterChain

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

Upvotes: 4

Views: 1079

Answers (1)

Marcus Hert da Coregio
Marcus Hert da Coregio

Reputation: 6308

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.

Upvotes: 9

Related Questions