Question

I'm very new to junit mockito and trying to write junit test case using mockito.

Here is my method for which I have to write a 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();
}

The method arguments passed ChefService and ChefApi are from 3rdparty api

here the call chefService.listCookbookVersions() will return a iterator of type CookBookVersion class like Iterable<? extends CookbookVersion> I'm not getting how to pass the ChefService mock object in the method which will return some value to compare.

Please help .

Was it helpful?

Solution

You're not passing anything in. You're explicitly telling the framework that, when this particular call is invoked, then return something.

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);

When you're done, you'll want to ensure that it was called, so you would want to verify the call.

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

Then, make sure that you verify the actual result. I've seen tests which only validate that the mocks work, but the data that was actually being generated was garbage.

Be sure that it's data you also expect to receive from your list of inputs from the API.

OTHER TIPS

What you have to do (most likely) is:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top