I dont know if my configuration is wrong or something with mockwebserver. I have method in one of my services
public UserResponse getUsername(String username) {
return webClient.get()
.uri("http://user-service/api/user",
uriBuilder -> uriBuilder.queryParam("username", username).build())
.retrieve()
.bodyToMono(UserResponse.class)
.block();
}
And I want to test this method by MockWebServer as below
public class CardWebClientTest {
private MockWebServer server;
private CardService service;
@BeforeEach
void setup() {
server = new MockWebServer();
WebClient webClient = WebClient.builder().baseUrl(server.url("").toString()).build();
service = new CardService(webClient);
}
@Test
void test() {
server.enqueue(new MockResponse().setBody("{\"id\": 12345}")
.addHeader("Content-Type", "application/json")
.setResponseCode(200));
var result = service.getUsername("john");
assertThat(result.getId()).isEqualTo(12345L);
}
Am I missing something? It's propably something trivial but I can't find solution. CardService which is calling UserService are communicating in EurekaServer with loadbalancer but Im assuming MockWebServer can handle this without any further configuration or not - idk. I will appreciate any help.
UserResponse dto looks like this
public class UserResponse
private Long id;
I forgot to mention error message. It's always
Failed to resolve 'user-service' after 2 queries ; nested exception is java.net.UnknownHostException: Failed to resolve 'user-service' after 2 queries
Alright. Now I can see that mockwebserver has different hostname - it's calling http://kubernetes.docker.internal:52654/api/user and webclient is asking for http://user-service. But still I dont know how to configure mockwebserver to get response. I should set somehow hostname or maybe leave like that and just test webclient.get() to see if response is correct?