I want to create a @SpringBootTest
that makes use of my full configuration structure.
Problem: I'm creating an @Bean SecurityWebFilterChain
that requires a ServerHttpSecurity
, which is somehow missing in a test:
@SpringBootApplication
public class MainApp { ... }
//for simple testing I started with anonymous auth
@Configuration
public class ReactiveSecurityConfiguration {
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http.anonymous()
.and().csrf().disable()
.build();
}
}
@SpringBootTest
public class TestClass {
@Test
public void test() {
}
}
Result:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'securityWebFilterChain' defined in class path resource [ReactiveSecurityConfiguration.class]:
Unsatisfied dependency expressed through method 'securityWebFilterChain' parameter 0;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.springframework.security.config.web.server.ServerHttpSecurity' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Update
I discovered if I add the following annotations to my Test Class, the testmethod works without errors. But is that intentional?
@EnableWebFlux
@EnableWebFluxSecurity
@SpringBootTest
public class TestClass { }
CodePudding user response:
I'm not certain how your project is arranged, but no, it's not intentional. You can take a look at Spring Security's Reactive Sample that demonstrates this.
It may be that your TestClass
is not finding the @SpringBootConfiguration
annotation attached to MainApp
.
CodePudding user response:
I have a spring-boot-starter-web
dependency pulled somewhere in the classpath. Removing it resolved the problem.
If both web and webflux dependency should be kept, it's still possible to run a test in reactive only, with:
@SpringBootTest(properties = "spring.main.web-application-type=REACTIVE")
class MyWebFluxTests {
// ...
}