Программисты JAVA общаются здесь
Anonymous
Веб-тест Spring Boot RestClient
Сообщение
Anonymous » 17 янв 2025, 16:42
Реализация у меня такая:
Код: Выделить всё
@Service
class AbcService {
private final RestClient client;
public AbcService(@Value("url") final String url) {
this.restClient = RestClient.builder().baseUrl(url).build();
}
String getPlace(String id) {
return restClient.get()
.uri("places/{id}", id)
.retrieve()
.body(AbcResponse.class);
}
record AbcResponse(String id, boolean flag) {}
}
и я попробовал протестировать с помощью MockRestServiceServer, но у меня это не сработало — у меня ошибка
Код: Выделить всё
java.lang.IllegalStateException: Unable to use auto-configured MockRestServiceServer since a mock server customizer has not been bound to a RestTemplate or RestClient
тест
Код: Выделить всё
@RestClientTest(AbcService.class)
class AbcServiceTest {
@Autowired
AbcService instance;
@Autowired
MockRestServiceServer server;
@BeforeEach
void setup() {
System.setProperty("url", "server123");
}
@Test
void test() {
server
.expect(requestTo("places/id1"))
.andRespond(withSuccess(new AbcResponse("string", true), APPLICATION_JSON));
assertEquals(new AbcResponse("string", true), instance.getPlace("id1"));
}
}
Можете ли вы мне с этим помочь?
Подробнее здесь:
https://stackoverflow.com/questions/793 ... lient-test
1737121328
Anonymous
Реализация у меня такая: [code]@Service class AbcService { private final RestClient client; public AbcService(@Value("url") final String url) { this.restClient = RestClient.builder().baseUrl(url).build(); } String getPlace(String id) { return restClient.get() .uri("places/{id}", id) .retrieve() .body(AbcResponse.class); } record AbcResponse(String id, boolean flag) {} } [/code] и я попробовал протестировать с помощью MockRestServiceServer, но у меня это не сработало — у меня ошибка [code]java.lang.IllegalStateException: Unable to use auto-configured MockRestServiceServer since a mock server customizer has not been bound to a RestTemplate or RestClient [/code] тест [code]@RestClientTest(AbcService.class) class AbcServiceTest { @Autowired AbcService instance; @Autowired MockRestServiceServer server; @BeforeEach void setup() { System.setProperty("url", "server123"); } @Test void test() { server .expect(requestTo("places/id1")) .andRespond(withSuccess(new AbcResponse("string", true), APPLICATION_JSON)); assertEquals(new AbcResponse("string", true), instance.getPlace("id1")); } } [/code] Можете ли вы мне с этим помочь? Подробнее здесь: [url]https://stackoverflow.com/questions/79362209/spring-boot-web-restclient-test[/url]