I'm trying to implement a unit test involving a FeignClient call which should return a 404 Not Found.
As Feign is trowing a FeignException for 404, what is the proper way to implement this test case?
I'm trying something like this...
when(mockedApiClient.userDataDelete(anyString()))
.thenThrow( ... );
What should I throw?
Feign Already provides inner classes like NotFound and other typica HTTP response code types. An example is shown here.
Request request = Request.create(Request.HttpMethod.GET, "url",
new HashMap<>(), null, new RequestTemplate());
throw new FeignException.NotFound("", request, null);
Just modify the above as per your needs! Key point to note is that Request object is mandatory. As of 2021 few overloads like Request.create are deprecated. Look out for what you use!
Hope that helps! happy Coding!
Mock your FeignException:
var ex = Mockito.mock(FeignException.class);
Mockito.when(ex.status()).thenReturn(404);
Mockito.when(mockedApiClient.userDataDelete(anyString()))
.thenThrow(ex);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With