有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何为我的mock实例化unirest HttpResponse<JsonNode>?

假设我有一个名为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的响应测试我的业务逻辑。请求方法返回

比如:

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返回它


共 (2) 个答案

  1. # 1 楼答案

    我就是这样做的:

    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);
        }
    }
    
  2. # 2 楼答案

        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