0

In a class I'm testing, there is a private method that instantiates the GetRequest class in Unirest. How do I use Mockito so that the instantiation of GetRequest class in getResponse() results in a mock object that I use in my JUnit test?

public ClassUnderTest {
    public String methodToTest() {
        String url = "http://localhost:8080";
        String result = getResponse(url);
        return result;
    }

    private String getResponse(String url) {
        GetRequest getRequest = new GetRequest(HttpMethod.GET, url);
        ... = getRequest.header(...).headers(...).asJson();
        ...etc...
    }
}

Thank you, Rico

  • ...etc... really ?? And where is your work. Did you try anything ? – want2learn Aug 11 '17 at 19:12
  • Tried looking in Mockito website and googling. After several hours of no success I thought I would pose a question here and then proceed to do more research. I will post an answer if I find a solution that works. – user2939305 Aug 11 '17 at 20:52
  • Possible duplicate of [Mockito : how to verify method was called on an object created within a method?](https://stackoverflow.com/questions/9841623/mockito-how-to-verify-method-was-called-on-an-object-created-within-a-method) – David Rawson Aug 16 '17 at 00:59

1 Answers1

-1

One solution is to use a partial mock as in Use Mockito to mock some methods but not others. I can refactor the call to the constructor in a private method and mock that.