Reputation: 2052
I got implementation like this:
@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) {}
}
and I tried test with MockRestServiceServer
but it doesn't work for me - I have error
java.lang.IllegalStateException: Unable to use auto-configured MockRestServiceServer since a mock server customizer has not been bound to a RestTemplate or RestClient
test
@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"));
}
}
Can you help me with that?
Upvotes: 0
Views: 72
Reputation: 68
I would suggest to create a bean for RestClient client; When spring load bean properly while starting the app.
@Bean
RestClient client() {
return RestClient.builder().baseUrl(url).build();
}
and use the bean in service directly instead of setting the url in service class
Upvotes: 1