使用mockito的测试用例

时间:2014-09-14 09:44:37

标签: junit mockito powermock

我有一个班级的方法。

public Response getProb(long Id) throws ResourceException
{
    final Response response = new Response();
    JSONObject headerObject = new JSONObject();
    String message = null;
    int status = 0;
    try
    {
        JSONObject getProb = new JSONObject();      
        getProb.put("id", Id);
        if (commonDao.validateIdCheck(getProb))
        {           
            headerObject.put(CommonConstants.LASTMOD_DATE, commonDao.lastModDate(Id));              
            response.setResource(headerObject);
            message="Retuned Successfully";
        }
        response.setStatus(message, status);
    }
    catch (JSONException exception)
    {
        throw new ResourceException(CommonErrorConstants.JSON_EXCEPTION, exception);
    }
    catch (ResourceException exception)
    {
        throw exception;
    }
    catch (Exception exception)
    {
        throw new ResourceException(CommonErrorConstants.GENERAL_EXCEPTION, exception);
    }
    return response;
}

使用mockito的junit测试用例将提供100%的代码覆盖率。

1 个答案:

答案 0 :(得分:0)

这些是您可以测试并达到100%覆盖范围的案例:

  • validateIdCheck返回true

  • validateIdCheck返回false

  • 你的try块抛出JSONException

  • 你的try块抛出ResourceException

  • 你的try块抛出异常

  • 您返回回复的成功案例