Mockito在私有方法中模拟对constuctor的调用

时间:2017-08-11 16:40:02

标签: constructor mockito

在I类测试中,有一个私有方法在Unirest中实例化GetRequest类。我如何使用Mockito以便在getResponse()中实例化GetRequest类会导致我在JUnit测试中使用的模拟对象?

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...
    }
}

谢谢你, 波多黎各

1 个答案:

答案 0 :(得分:-1)

一种解决方案是使用Use Mockito to mock some methods but not others中的部分模拟。我可以在私有方法中重构对构造函数的调用并模拟它。