Java 如何为我的mock实例化unirest HttpResponse?

Java 如何为我的mock实例化unirest HttpResponse?,java,mockito,httpresponse,unirest,Java,Mockito,Httpresponse,Unirest,假设我有一个名为Api的类,它有一个方法: public class Api{ public HttpResponse<JsonNode> request() { try { return Unirest.get("http://localhost:8080").header("accept", "application/json").asJson(); } catch (UnirestException e) {

假设我有一个名为Api的类,它有一个方法:

public class Api{
    public HttpResponse<JsonNode> request() {
        try {
            return Unirest.get("http://localhost:8080").header("accept", "application/json").asJson();
        } catch (UnirestException e) {
            throw new RuntimeException(e);
        }
    }
}
在我的测试中,我想根据API.request方法返回的响应测试我的业务逻辑

比如:

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.stub;
import org.json.JSONObject;
import com.mashape.unirest.http.HttpResponse;

public class ApiTest {
    private API api = mock(API.class);
    public void test() {
        HttpResponse<JsonNode> response = null;
        JSONObject result = new JSONObject();
        response.getBody().getObject();
        stub(api.request("")).toReturn(response);
        Dao dao = new Dao(api);
        assertTrue(dao.test() > 100);
    } 
}
如何使用主体{number:10}实例化HttpResponse,以便能够使用mock返回它?

它可以使用:

import org.apache.http.impl.DefaultHttpResponseFactory;
import org.json.JSONObject;


HttpResponseFactory factory = new DefaultHttpResponseFactory();
org.apache.http.HttpResponse response = factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null);
JSONObject json = new JSONObject();
json.put("result", "10");
response.setEntity(new StringEntity(json.toString()));
com.mashape.unirest.http.HttpResponse<JsonNode> httpResponse = new HttpResponse<JsonNode>(response, JsonNode.class);
我就是这样做的:

public class Dao() {

    private Api api;

    public Dao(Api api){
        this.api = api;
    }

    public Integer test(){
        // this is probably not good style
        //    should iterate through pulling out each piece 
        Integer result = api.request().getBody().getObject().getInteger("result");
        return result + 100;
    }
}

public class ApiTest {
    private API api = mock(API.class);
    public void test() {
        JsonNode json = new JsonNode("{\"result\":10}");

        HttpResponse<JsonNode> mockResponse = mock(HttpResponse.class);
        when(mockResponse.getCode()).thenReturn(200);
        when(mockResponse.getBody()).thenReturn(json);

        when(api.request(anyString())).thenReturn(mockResponse);

        Dao dao = new Dao(api);
        // this should be done more carefully as well, check code/body/etc..
        assertTrue(dao.test() > 100);
    }
}
请参阅此github资源-

public class Dao() {

    private Api api;

    public Dao(Api api){
        this.api = api;
    }

    public Integer test(){
        // this is probably not good style
        //    should iterate through pulling out each piece 
        Integer result = api.request().getBody().getObject().getInteger("result");
        return result + 100;
    }
}

public class ApiTest {
    private API api = mock(API.class);
    public void test() {
        JsonNode json = new JsonNode("{\"result\":10}");

        HttpResponse<JsonNode> mockResponse = mock(HttpResponse.class);
        when(mockResponse.getCode()).thenReturn(200);
        when(mockResponse.getBody()).thenReturn(json);

        when(api.request(anyString())).thenReturn(mockResponse);

        Dao dao = new Dao(api);
        // this should be done more carefully as well, check code/body/etc..
        assertTrue(dao.test() > 100);
    }
}
    import org.mockito.Mock; // used for mock

          //On class 
        @RunWith(PowerMockRunner.class)
        @PrepareForTest(Unirest.class)

    // Mock the variable using mockito
          @Mock
          private HttpResponse<String> getPost;
          @Mock
          private HttpResponse<String> httpResponse;
          @Mock
          private HttpRequestWithBody httpRequestWithBody;
          @Mock
          private RequestBodyEntity requestBodyEntity;

        //In before setup function
        PowerMockito.mockStatic(Unirest.class);

// In test function - used mock variables in thenReturn and got the required mocked outcome
        when(Unirest.post(wdUrl + "/")).thenReturn(httpRequestWithBody);
        when(httpRequestWithBody.header("Content-Type", "application/json")).thenReturn(httpRequestWithBody);
        when(httpRequestWithBody.body(
                "{\"name\":\"TESTER NAME\",\"email\":\"testuser@yopmail.com\",\"username\":\"testinguser\"}"))
                    .thenReturn(requestBodyEntity);
        when(requestBodyEntity.asString()).thenReturn(httpResponse);
        when(httpResponse.getStatus()).thenReturn(Integer.valueOf(200));