简体   繁体   中英

Play 2 - Mock Inter Service Communication for JUnit Testing

@Override
public JsonNode getCities() throws Exception {
    String cityListUrl; // get from properties file
    List<Integer> cityIds = dao.getCities();
    StringBuilder cityIds = new StringBuilder();
    for (Integer cityId : cityIds) {
        cityIds.append(cityId).append(",");
    }
    // gets city names.returns cityId,cityName.
    // How to mock this?
    Promise<JsonNode> jsonPromise = WS
            .url(cityListUrl+ cityIds.toString()).get()
            .map(wsresponse -> {
                return wsresponse.asJson();
            });
    JsonNode node = jsonPromise.get(100000);
    return node;
}

@Test
public void testGetCities() {
    JsonNode cities = Ws.url() call to the API /cities/.
    Assert statement.
}

When run as an application,it should call the other service.When run as JUnit,the REST call to the other service should be mocked.

I am only able to imagine mocking the dependencies of a class.How this can be achieved?

您可以在测试中启动MockServer并调用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM