【发布时间】:2022-06-24 01:48:54
【问题描述】:
我的 Spring Boot 应用程序在 application.yml 中定义了一个像这样的 OAuth2 资源服务器:
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: ${AUTH_SERVER_METADATA_URL:http://localhost:8080/auth/realms/demo}
这在实际运行应用程序时工作正常。
但是,当使用@WebMvcTest 和MockMvc 运行测试来测试@RestController 类时,测试会失败并出现以下错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
...
...
...
I/O error on GET request for "http://localhost:8080/auth/realms/demo/.well-known/openid-configuration": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
消息的根本原因很清楚,但我不知道如何在不启动真正的 Keycloak 或 WireMock 服务器的情况下“模拟”切片测试的连接。
在后台运行我的开发 Keycloak 服务器运行相同的测试工作正常。
【问题讨论】:
-
运行集成测试时,将 Spring Security 配置为read the public key from the class path,而不是尝试从授权服务器获取。
标签: spring spring-boot spring-security