I Have a Spring Boot 6 Rest application working and want to add unit and integration tests to it. During tests, a number of security conditions can be relaxed, specially at this moment.
So I added the following in WebSecurityConfig class to my test application:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.anyRequest().authenticated()
)
.httpBasic(withDefaults());
return http.build();
}
}
I expected that when the tests started to run, this security settings took place, what in fact happen. However, all tests fail because of this exception:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Unsatisfied dependency expressed through method 'setFilterChains' parameter 0: Error creating bean with name 'filterChain' defined in class path resource [br/ufpr/pessoas/config/WebSecurityConfig.class]: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'filterChain' threw exception with message: Cannot read the array length because "mvcPatterns" is null
What am I missing?
After some work I could fix the problem, but to understand the solution it's important to understand my setting:
To fix the problem it's enough to run Maven test.
I noticed Maven test started to download a number of dependencies and then the tests ran with no problem. After that, running the tests from the IDE started to work again.
As a double check, I Maven cleaned the project and tried to run a test from the IDE: same problem as before. I then re-ran Maven test and again everything got back to work.