如何为我的模拟实例化一个unirest HttpResponse <jsonnode>?

时间:2016-07-12 11:13:54

标签: 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) {
            throw new RuntimeException(e);
        }
    }
}

我有一节课:

public class Dao(){

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

    public Integer test(){
        Integer result = api.request().getInteger("result");
        return result + 100;
    }
}

在我的测试中,我想根据我的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);
    } 
}

如何使用Body&#34; {number:10}&#34;来实例化HttpResponse。能够用模拟返回吗?

3 个答案:

答案 0 :(得分:3)

我就是这样做的:

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);
    }
}

答案 1 :(得分:1)

适用于:

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);

答案 2 :(得分:0)

    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));

请参阅此github资源-https://github.com/thejamesthomas/javabank/blob/master/javabank-client/src/test/java/org/mbtest/javabank/ClientTest.java