Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3637

Mocking RestClient using Mockito

$
0
0

I have a RestClient such as below:

SomeService.java:

String result = restClient.post().headers(httpHeaders -> httpHeaders.setBasicAuth(id,pwd))                       .contentType(MediaType.APPLICATION_FORM_URLENCODED)                       .body(requestBody)                       .retrieve()                       .onStatus(HttpStatusCode::is4xxClientError, (request,response) -> {                           throw new Exception(response.getStatusCode() +" : " + response.getHeaders());                       })                       .body(String.class);

I am trying to Mock this restClient call using Mockito to return a specific string. However this mock does not trigger. Am I missing anything?

SomeServiceTest.java:

@Mock(answer = Answers.RETURNS_DEEP_STUBS)RestClient restClient;@Testpublic void someTestMethod() {    when(restClient.post().headers(any()).contentType(any()).body(any()).retrieve()            .onStatus(any(),any()).body(String.class))        .thenReturn("Some String");}

On Execution, the mock does not trigger and the returned string is null in the service. Any help would be appreciated. Thank you.

I tried to mock the restClient call using Mockito and return a specific string. However, the mock does not trigger and hence a null string is returned. I am expecting the mock to trigger for the given method signature and return a specific string.


Viewing all articles
Browse latest Browse all 3637

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>