0

I have tried multiple ways to mock HttpResponse(custom class not related to any library) object but it's not mocking. Below is my code. please suggest if mistake. or any other suggestions to respond.

Note: I can't modify or write any factory method or any extra stuff on my MyClass

Class MyClass{
 public void methodtoTest(ListOfObjects obj){
   HttpResponse response = null;
   response = HttpOperationHelper.executeHttpPostResponse(client, uri, heade, postBoday);
   if(response.getStatusCode() == 200){
     //some code is there
  }
}

HttpOperationHelper is helper class in the project and executeHttpPostResponse is static method.

@PrepareForTest(HttpOperationHelper.class)
Class MyClassTest{
  HttpResponse response = Mockito.mock(HttpResponse.class);
  response = HttpResponse.getInstance(200, "body");
  Powermockito.mockStatic(HttpOperationHelper.class);
  Mockit.when(HttpOperationHelper.executeHttpPostResponse(anyObject(), anyObject(), anyObject(), anyString())).thenReturn(response);
}
xxz
  • 11
  • 4
  • "I have tried multiple ways" isn't a useful problem description. Thing is: you "simply" have to follow the relevant rules when using powermock(ito) to mock static methods. See https://stackoverflow.com/questions/10583202/powermockito-mock-single-static-method-and-return-object for example – GhostCat May 23 '22 at 14:28
  • Meaning: whatever you tried, you have to INCLUDE it in your request. But as said, the real answer for you is to step back: and carefully research the existing documentation. Make sure you have the "right" classes in your `@PrepareForTest` clause for example. – GhostCat May 23 '22 at 14:29
  • @GhostCat i have included PrepareForTest on my test class, if we dont define it will throw different error right. – xxz May 23 '22 at 14:35
  • i just update my question with @PrepareTest – xxz May 23 '22 at 14:36
  • here my response(of HttpResponse) object is initialized with null. i looked at https://stackoverflow.com/questions/10583202/powermockito-mock-single-static-method-and-return-object link. if the method directly return it would have been bit easy – xxz May 23 '22 at 14:39

0 Answers0