我创建了一个
TestRestClient
并且它工作得很好,但是如果我将另一个 RestClient
bean 注入到我的 RestClientConfiguration
类中,它可以工作,但在测试中,它会给出以下错误:无法使用自动配置的 MockRestServiceServer
因为 MockServerRestClientCustomizer
已绑定到多个 RestClient
。
错误消息:无法使用自动配置的
MockRestServiceServer
,因为 MockServerRestClientCustomizer
已绑定到多个 RestClient
我的代码:https://github.com/vitheka/doubt-test-restclient-with-two-beans
@RestClientTest({
ViaCepApiService.class,
RestClientConfiguration.class
})
class ViaCepApiServiceTest {
@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);
}
}
我也有同样的问题。
你找到解决这个问题的方法了吗?