在 SpringBoot 应用程序中,我使用新的 RestClient 进行 REST 调用。 现在我想测试这个 REST 客户端。为此,我使用了 @RestClientTest 并注入了一个 MockRestServiceServer,例如here所述。
错误信息
java.lang.IllegalStateException:无法使用自动配置的 MockRestServiceServer,因为 MockServerRestTemplateCustomizer 尚未绑定到 RestTemplate
让我认为 MockRestServiceServer 只能与 RestTemplate 结合使用,而不能与 RestClient 结合使用。如果是这样,测试 RestClient 的正确方法是什么?我可以使用 MockServer,但我希望 Spring 能够提供合适的测试工具。
据我所知,MockRestServiceServer 与 RestClient 不兼容(我上次尝试过)。 您可以尝试使用 @SpringBootTest 和 @AutoConfigureWebClient:
@SpringBootTest
@AutoConfigureWebClient
public class MyRestClientIntegrationTest {
@Autowired
private WebClient.Builder webClientBuilder;
@Test
public void testRestClient() {
// make calls with a WebClient instance
// perform assertions as needed
}
}
集成测试是在真实服务器上完成的,通常是在每次运行之前和之后添加干净数据的测试服务器(取决于您的需要)
如果你想模拟调用,那么你正在进行单元测试来测试 API/控制器的逻辑(或者更确切地说是路由,因为 API 不应该包含逻辑):MockRestServiceServer 可以是一个选项,但它对 RestClient 的支持可能不支持有保证。
即使我没有完全给出您正在寻找的答案,我希望我可以提供一些帮助