Hi I have a spring project that has spring security to authenticate users using OAUTH provider, and I have to use Spring Cloud Contract to build up a mock server for consumer testing.
When I run ./gradlew clean :order-service:contractTest
. The following error is thrown
delegate cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
java.lang.IllegalStateException: delegate cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
If you look at my SecurityConfig
The security filter is already setup. So, how to resolve this error and have my contractTest
running properly??
There are some issues on the BaseClass.
@SpringBootTest
is enough. No need the @ExtendedWith
Mockito.reset
in cleanup is no need at all.In a WebMvc project, exclude the UserDetailsServiceAutoConfiguration
and SecurityAutoConfiguration
in your test context, check my example.
In a WebFlux project, just exclude ReactiveUserDetailsServiceAutoConfiguration
and ReactiveSecurityAutoConfiguration
and ReactiveOAuth2ResourceServerAutoConfiguration
(if oauth2 resource server is enabled.) on your testing codes.
@SpringBootTest
@ImportAutoConfiguration(exclude = {ReactiveUserDetailsServiceAutoConfiguration.class, ReactiveSecurityAutoConfiguration.class})
class YourTest{
}
Or test web controller.
@WebFluxTest(controller=YourController.calss, excludeAutoConfigurations = {ReactiveUserDetailsServiceAutoConfiguration.class, ReactiveSecurityAutoConfiguration.class})
class YourTest{
}
I have created a simple Microservice sample several years ago, which used Spring Cloud Contract and Pact to implement the CDC pattern in the API testing and verify, check spring-microservice-sample.