如何在单元测试中模拟Jersey REST api调用

时间:2018-11-15 09:55:16

标签: rest unit-testing junit mockito rest-assured

我已经编写了2个运行良好的REST api。等级如下

@Path("/calculation")
public class CalculationResource {

@GET
@Path ("/{var1}")
@Produces(MediaType.APPLICATION_JSON)
public List<Result> CalculateNumber(@PathParam("var1") int number) {

    MyCalculationService service = new CalculationService();

    //Some number calculation here
    //construct new Result result_1

    //Calling my second REST api
    RestTemplate restTemplate = new RestTemplate();
    Result result_2 = restTemplate.getForObject("url to 2nd rest", Result.class);

    List<Result> resultList = new ArrayList<Result>();
    resultList.add(result_1);
    resultList.add(result_2);
    return resultList;
}   
}

我的单元测试如下

@Test
public void testCalculation() {

    Response response = given()
            .given().header("Content-Language", "en_US")
            .contentType("application/json").body("number").body("quote").when().get("url to calculation");
    JsonPath jsonPath = new JsonPath(response.asString()); 
    String value = jsonString.get("text").toString(); 
    Assert.assertEquals("[Hello]", value);
}

我的问题是,如何在单元测试中模拟对第二个REST api的调用? 也许是Mocktio?但我不知道如何模拟其余的呼叫。

0 个答案:

没有答案