0

How to write the Unit test cases for WebClient. I was trying to register the test case for refreshing the Data refreshData(). While writing the test cases I am getting NullPointerException "this.WebClient" is null

@Autowired
WebClient.Builder webClient

List<HumanDto> hdata = new ArrayList<>();

public List<HumanDto> refreshData(){
    List<HumanDto> hdata2 = getAllHuman();
    if(!hdata.isEmpty()){
    hdata.clear();
    hdata.addAll(hdata2)
   }
   return hdata;
}


private List<HumanDto> getAllHuman(){
  HumanLookup response = webClient.build().get.uri("https://user.asp/web").header("Authorization" , "Bearer" + token).retrieve().onStatus(HttpStatus::isError, clientResponse -> Mono.error(RunTimeException())).bodyToMono(new ParametrizedTypeReference<HumanLookup>(){}).block();
  return response.getUserInfo();

}

At webClient.build only it shows a null pointer exception. How can I pass mocked webclient and return mocked result

  • Can you specify whether you want to mock the HTTP call or the whole `WebClient`? – Vincent C. Oct 13 '22 at 19:50
  • @VincentC. i was looking whole WebClient – SpringLearner Oct 13 '22 at 20:23
  • @xerx593 for code brevity i have not added the whole code I edited the question this – SpringLearner Oct 13 '22 at 20:28
  • spring doesn't provide a default webclient afaik, but you can easily `new` one yourself – zapl Oct 13 '22 at 20:39
  • Ok, then it makes sense ..at least ;) 1. `@MockBean WebClient.Builder buildMock;` 2. `@Mock WebClient wcMock;` 3. `©Mock WebClient.RequestHeadersUriSpec getMock;` ... (We have to mock all objects between build() and block();(( – xerx593 Oct 13 '22 at 20:41
  • Then in/before test, we'd have to `when(buildMock.build()).thenReturn(wcMock);`...`when(wcMock.get(/* exact or 'any' matcher*/)).thenReturn(getMock);`..a.s.f. until we `when(someNextMock.block()).then...` we can return a "real object" or another mock (e.g. when it is an interface) ..or "answer" (dynamically/exceptionally...) – xerx593 Oct 13 '22 at 20:48
  • ..then test would invoke `refreshData()` (with mocked builder...+chain ..replaced in spring (test) context) – xerx593 Oct 13 '22 at 20:50
  • Then we would verify on a(ll) mock(s) (invocations) + returned (mocked) result (actual vs expected) – xerx593 Oct 13 '22 at 20:51
  • 1
    if you changed that code and made that url injectable (eg. via `@Value`) or the entire `WebClient` you could test the code like in https://reflectoring.io/spring-boot-testconfiguration/ or https://www.baeldung.com/spring-mocking-webclient - currently you'd have to mock all the method calls in `webClient.build().get.uri....block()` I think - Using spy https://stackoverflow.com/a/14970545/995891 to mock just some parts would also work and reduce the amount of things to mock – zapl Oct 13 '22 at 20:54
  • Another way to create reliable tests for WebClient is to use WireMock https://stackoverflow.com/questions/71356276/mocking-webclient-post-method-is-failing/71359000#71359000 – Alex Oct 14 '22 at 01:50

0 Answers0