Reputation: 785
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?
Upvotes: 6
Views: 31282
Reputation: 171
In kotlin you can use anonymous object like this
every { service.method() } throws object : FeignException(400, "message") {}
In java it should be something like this:
when(service.method()).thenThrow(new FeignException(400, "message") {} );
Upvotes: 0
Reputation: 619
Mock your FeignException
:
var ex = Mockito.mock(FeignException.class);
Mockito.when(ex.status()).thenReturn(404);
Mockito.when(mockedApiClient.userDataDelete(anyString()))
.thenThrow(ex);
Upvotes: 11
Reputation: 29
Request request = mock(Request.class);
when(someClient.someGetRequest(List.of(doc.getId())))
.thenThrow(new FeignException.NotFound("message", request, null, null));
You can avoid create Request, only mocking it.
Upvotes: -1
Reputation: 2662
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!
Upvotes: 15
Reputation: 61
Well, to me worked as below (i used EasyRandom as generator of some fields):
private EasyRandom easyRandom = new EasyRandom();
Map<String, Collection<String>> headersError = easyRandom.nextObject(HashMap.class);
byte[] bodyError = easyRandom.nextObject(byte[].class);
when(mockedApiClient.userDataDelete(anyString())
.thenThrow(FeignException.errorStatus(
"userDataDelete",
Response.builder()
.status(404)
.reason("message error")
.request(Request.create(
Request.HttpMethod.POST,
"foo/foo/bar/v1/delete-data-user",
headersError, //this field is required for construtor//
null,
null,
null))
.body(bodyError)//this field is required for construtor
.build())
);
Upvotes: 5
Reputation: 21
To return 404 Feign Exception on mocking the service, you can try out these things :
import feign.FeignException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class SampleFeignClientException extends FeignException {
public SampleFeignClientException (String message, Throwable cause) {
super(message, cause);
}
public SampleFeignClientException (String message, Throwable cause, byte[] content) {
super(message, cause, content);
}
public SampleFeignClientException (String message) {
super(message);
}
public SampleFeignClientException (int status, String message, byte[] content) {
super(status, message, content);
}
}
@Test
public void sampleTestMethod() {
SampleFeignClientException sampleFeignClientException = new sampleFeignClientException(404, "NOT FOUND", new byte[1]);
doThrow(sampleFeignClientException).when(mockService).method();
}
I hope this may helps you to resolve the problem.
Upvotes: 2
Reputation: 785
Unless any other better solution is provided, this is how I have overcome this...
when(mockedApiClient.userDataDelete(anyString()))
.thenThrow(FeignException.errorStatus(
"userDataDelete",
Response.builder()
.status(404)
.headers(new HashMap<>())
.reason("Not found").build()));
Upvotes: 2