1

I am unable to get the mocked response from Feign Client. I provide below the code.

In the service class, it has been written like this.

public String getInfo(HttpServletRequest request, String id, String type) {
        .... other code .....
        try {
            
            statusAsJsonString = myFeignClient.getStatus(cookie, id, type);
            System.out.println("statusAsJsonString--------->"+statusAsJsonString);
            ObjectNode node = new ObjectMapper().readValue(statusAsJsonString, ObjectNode.class);
            if (node.has(CommonConstants.STATUS)) {
                statusValue = node.get(CommonConstants.STATUS).asText();
            }
        } catch (FeignException fe) {
            byte[] contents = fe.content();
            String jsonContents  = null;
            if(contents != null) {
                jsonContents = new String(contents);
            }
            statusValue = getErrorParsedStatusValue(jsonContents);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        log.debug("status: " + statusValue);
        return statusValue;
    }

In the unit test, I am trying to write in the following manner.

String responseBody = "[]";
when(myFeignClient.getStatus("cookievalue", "id", "SOme-Value")).thenReturn(responseBody);

I have also used, WireMock to achieve it.

wireMockServer.stubFor(WireMock.get("/rest/v1/somna/{id}/phase").withRequestBody(WireMock.equalToJson("{ \"name\": \"Phone\", \"initialStock\": 3}"))
                .willReturn(WireMock.okJson(responseBody)));

The following piece of code is never covered and executed.

statusAsJsonString = myFeignClient.getStatus(cookie, id, type);
System.out.println("statusAsJsonString--------->"+statusAsJsonString);

Also the invocation of Feign client is inside a service method, first want to get the mocked result of that Feign client. PLease help me.

I provide below my Feign CLient

@FeignClient(name = CommonConstants.FEIGN_CLIENT_NAME, url = "${feign.service.url}", primary = false)
public interface MyFeignClient {

    @GetMapping(value = "/rest/v1/project/{id}/phaseName")
    String getStatus(@RequestHeader("Cookie") String cookie,
            @PathVariable("id") Stringid, @RequestParam("type") String type);

}

In my test class, I have added the followings.

@Autowired
    private MyServiceImpl readyService = new MyServiceImpl();
    
     @Mock
    private MyFeignClient myFeignClient;
    
    @ClassRule
    public static WireMockServer wireMockServer = new WireMockServer(new WireMockConfiguration().port(8088));
    
    @BeforeEach
    void setUp() {
        MockitoAnnotations.initMocks(this);
        httpServletRequest = Mockito.mock(HttpServletRequest.class);
        ReflectionTestUtils.setField(someService, "cookieName", "cookieName");
        
        wireMockServer.start();

    }
Deba
  • 859
  • 5
  • 9
  • 21
  • 1. Please show us how you setup the mock and object under test. 2. It seems that you have some other code before `getStatus` call. You also seem to swallow exceptions with `catch (Exception ex)`. Use a debugger to check if you enter method under test and why `getStatus` is not reached. – Lesiak Dec 24 '21 at 11:12
  • I have added my Feign Client – Deba Dec 24 '21 at 14:41
  • The implementation of the client is irrelevant - you said you are going to mock it. – Lesiak Dec 24 '21 at 15:03
  • Sir, I have added, please suggest me, I will learn. – Deba Dec 24 '21 at 15:15

0 Answers0