使用mockito的JUnit测试用例

时间:2014-05-10 04:05:53

标签: java junit mockito

我是junit mockito的新手,并尝试使用mockito编写junit测试用例。

这是我必须编写junit的方法。

public String getAllCookBooks(ChefService chefService, ChefApi chefApi) {
    JSONObject cookBooks = null;
    cookBooks = new JSONObject();
    JSONArray array = null;
    array = new JSONArray();

    try {
        if (null != chefService.listCookbookVersions()) {
            LOG.debug(SuccessCode.COOKBOOK_DETAILS_RETRIEVED_SUCCCESS
                    .getSuccessCode()
                    + "::"
                    + SuccessCode.COOKBOOK_DETAILS_RETRIEVED_SUCCCESS
                            .getMessage());
            for (CookbookVersion cookbookVersion : chefService
                    .listCookbookVersions()) {

                JSONObject cookBooksDetails = new JSONObject();

                cookBooksDetails.put("cookbook_name",
                        cookbookVersion.getCookbookName());
                cookBooksDetails.put("cookbook_version",
                        cookbookVersion.getVersion());

                cookBooksDetails.put("name", cookbookVersion.getName());
                array.put(cookBooksDetails);
            }
        } else {
            LOG.info("no cookbook present..."
                    + ErrorCode.COOKBOOK_LIST_EMPTY_ERROR.getErrorCode()
                    + " : "
                    + ErrorCode.COOKBOOK_LIST_EMPTY_ERROR.getMessage());
            cookBooks.put("error",
                    ErrorCode.COOKBOOK_LIST_EMPTY_ERROR.getMessage());
        }
        cookBooks.put("chef_cookbooks", array);
    } catch (JSONException e) {
        LOG.warn("JSON Exception "
                + ErrorCode.JSON_PARSE_ERROR.getErrorCode() + " "
                + ErrorCode.JSON_PARSE_ERROR.getMessage());
    }

    LOG.debug("cookbooks: " + cookBooks.toString());
    LOG.info("Ended getAllCookBooks method");
    return cookBooks.toString();
}

传递ChefService和ChefApi的方法参数来自3rdparty api

这里调用chefService.listCookbookVersions()将返回类似于Iterable<? extends CookbookVersion>的CookBookVersion类的迭代器 我没有得到如何在方法中传递ChefService模拟对象,该方法将返回一些值来进行比较。

请帮忙。

2 个答案:

答案 0 :(得分:0)

你没有传入任何内容。你明确地告诉框架,当调用这个特定的调用时,然后返回某些东西

ChefService chefServiceMock = mock(ChefService.class);
// some reasonable, expected entry value
List<CookbookVersion> cookbookVersions = ...; 
when(chefServiceMock.listCookbookVersions()).thenReturn(cookbookVersions);
// You're not actually using the API object, so you can pass in null.
// It'd be wiser to remove it altogether.
String actual = testObject.getAllCookBooks(chefServiceMock, null);

完成后,您需要确保调用它,因此您需要verify来电。

// later
verify(chefServiceMock).listCookbookVersions();

然后,确保验证实际结果。我见过的测试只能验证模拟是否有效,但实际生成的数据是垃圾。

确保您期望的数据从API的输入列表中接收。

答案 1 :(得分:0)

你必须做的(最有可能的)是:

ChefService chefService = mock(ChefService.class); // create the mock
when(chefService.listCookbookVersions()).thenReturn(aListOfValues); // establish what would be returned on method call, in this case 'aListOfValues'

// you could do the same for chefApi

String result = getAllCookbooks(chefService, chefApi); // call you method

//assert something on the result