You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

注入多个RestClient Bean导致MockRestServiceServer自动配置失败求助

问题解决:多RestClient Bean下MockRestServiceServer自动配置失败

错误原因

当Spring上下文存在多个RestClient Bean时,@RestClientTest的自动配置无法确定将MockServerRestClientCustomizer绑定到哪一个RestClient实例,因此抛出错误:

Unable to use auto-configured MockRestServiceServer since MockServerRestClientCustomizer has been bound to more than one RestClient

解决方案

方案1:Mock掉不需要的RestClient Bean

在测试类中使用@MockBean标记测试不需要的RestClient Bean,让上下文仅保留测试依赖的那个实例,这样自动配置就能正常绑定MockRestServiceServer。

修改后的测试代码示例:

@RestClientTest({
        ViaCepApiService.class,
        RestClientConfiguration.class
})
class ViaCepApiServiceTest {

    @MockBean
    private RestClient otherRestClient; // 替换为你配置的另一个RestClient Bean的类型/名称

    @Autowired
    private MockRestServiceServer server;

    @Autowired
    private ObjectMapper mapper;

    @Autowired
    private ViaCepApiService viaCepApiService;

    private ViaCepResponse viaCepResponse;

    @BeforeEach
    void setUp() {
        viaCepResponse = new ViaCepResponse(
                "88804-576",
                "Rua Albina Milanez",
                "",
                "",
                "Milanese",
                "Criciúma",
                "SC",
                "Santa Catarina",
                "Sul",
                "4204608",
                "",
                "48",
                "8089"
        );
    }

    @Test
    void getCep() throws JsonProcessingException {
        String baseUrl = "https://viacep.com.br";
        String path = "/ws/88804576/json/";

        var jsonResponse = mapper.writeValueAsString(viaCepResponse);

        var requestTo = MockRestRequestMatchers
                .requestToUriTemplate(baseUrl.concat(path));

        var withSuccess = MockRestResponseCreators.withSuccess(jsonResponse, MediaType.APPLICATION_JSON);

        server.expect(requestTo).andRespond(withSuccess);

        Assertions
                .assertThat(viaCepApiService.getViaCep())
                .isNotNull()
                .isEqualTo(viaCepResponse);
    }
}

方案2:手动创建MockRestServiceServer

放弃自动注入的MockRestServiceServer,直接通过MockRestServiceServer.createServer()方法手动创建,指定测试目标的RestClient实例,绕开自动配置的冲突问题。

修改后的测试代码示例:

@RestClientTest({
        ViaCepApiService.class,
        RestClientConfiguration.class
})
class ViaCepApiServiceTest {

    @Autowired
    private RestClient viaCepRestClient; // 注入测试用到的目标RestClient

    @Autowired
    private ObjectMapper mapper;

    @Autowired
    private ViaCepApiService viaCepApiService;

    private MockRestServiceServer server;
    private ViaCepResponse viaCepResponse;

    @BeforeEach
    void setUp() {
        // 手动创建针对目标RestClient的Mock服务器
        server = MockRestServiceServer.createServer(viaCepRestClient);
        
        viaCepResponse = new ViaCepResponse(
                "88804-576",
                "Rua Albina Milanez",
                "",
                "",
                "Milanese",
                "Criciúma",
                "SC",
                "Santa Catarina",
                "Sul",
                "4204608",
                "",
                "48",
                "8089"
        );
    }

    @Test
    void getCep() throws JsonProcessingException {
        String baseUrl = "https://viacep.com.br";
        String path = "/ws/88804576/json/";

        var jsonResponse = mapper.writeValueAsString(viaCepResponse);

        var requestTo = MockRestRequestMatchers
                .requestToUriTemplate(baseUrl.concat(path));

        var withSuccess = MockRestResponseCreators.withSuccess(jsonResponse, MediaType.APPLICATION_JSON);

        server.expect(requestTo).andRespond(withSuccess);

        Assertions
                .assertThat(viaCepApiService.getViaCep())
                .isNotNull()
                .isEqualTo(viaCepResponse);
    }
}

内容的提问来源于stack exchange,提问作者Vitor Coelho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 09:58:21