mardi 5 mai 2015

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?

Aucun commentaire:

Enregistrer un commentaire