0

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?

5
  • Can you please add the relevant part of the test code. Commented Jan 16 at 15:55
  • I added test (edited post) Commented Jan 16 at 16:42
  • You test is autowiring but your service is not autowired. If you place testing a service with autowired test, ensure the code is also following the same pattern. except if you did not post a more detailed code here. Commented Jan 16 at 18:48
  • Try Shahid's answer below, which echo's @Abiola Akinnubi and looks correct. Let me know if it doesn't work. Commented Jan 17 at 12:22
  • I've commented @Shahid's solution Commented Jan 17 at 13:28

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this solution with @Qualifier("abc-client"). After that I use @Qualifier("abc-client") in my AbcService class. Then use @Autowired @Qualifier("abc-client") + MockRestServiceServer with @BeforeEach implementation MockRestServiceServer.bindTo(restClient.mutate()).build() in test class. Now I get rid of this issue, but I have different one, it looks like whatever I put in andRespond is not returned using this endpoint.
about this andRespond let's assume that I want to return new AbcResponse("string", true) but I received AbcResponse("string", false) from this mocked endpoint.
MockRestServiceServer is not compatible with RestClient Use MockWebServer from OkHttp to mock HTTP requests for RestClient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.