您好,我有一個 spring 專案,它具有 spring 安全性,可以使用 OAUTH 提供程式對用戶進行身份驗證,我必須使用 Spring Cloud Contract 來構建一個用于消費者測驗的模擬服務器。
回購:https ://github.com/Isaacwhyuenac/spring-cloud-contract-poc/blob/main/order-service/src/test/java/com/example/producer/BaseClass.java
當我跑的時候./gradlew clean :order-service:contractTest。拋出以下錯誤
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.
如果你看看我的 SecurityConfig
https://github.com/Isaacwhyuenac/spring-cloud-contract-poc/blob/main/order-service/src/main/java/com/example/producer/config/SecurityConfig.java
安全過濾器已經設定。那么,如何解決此錯誤并讓我contractTest正常運行?
uj5u.com熱心網友回復:
BaseClass 存在一些問題。
- 如果你使用的是最新的 Spring Boot,使用
@SpringBootTest就足夠了。不需要@ExtendedWith - 使用模擬環境,設定 webEnvironment = Mock
- 如果你決定使用 RestAssuredMockMvc,你應該在你的測驗中使用這個 mockMvc 而不是標準的 MockMVC。
Mockito.resetin 清理根本不需要。
在 WebMvc 專案中,排除UserDetailsServiceAutoConfiguration并SecurityAutoConfiguration在您的測驗背景關系中檢查我的示例。
在 WebFlux 專案中,只需在測驗代碼中排除ReactiveUserDetailsServiceAutoConfigurationandReactiveSecurityAutoConfiguration和ReactiveOAuth2ResourceServerAutoConfiguration(如果啟用了 oauth2 資源服務器。)。
@SpringBootTest
@ImportAutoConfiguration(exclude = {ReactiveUserDetailsServiceAutoConfiguration.class, ReactiveSecurityAutoConfiguration.class})
class YourTest{
}
或者測驗網路控制器。
@WebFluxTest(controller=YourController.calss, excludeAutoConfigurations = {ReactiveUserDetailsServiceAutoConfiguration.class, ReactiveSecurityAutoConfiguration.class})
class YourTest{
}
我幾年前創建了一個簡單的微服務示例,它使用 Spring Cloud Contract 和 Pact 在 API 測驗和驗證中實作了 CDC 模式,查看spring-microservice-sample。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/537466.html
