Junit 使用mockito的测试用例

Junit 使用mockito的测试用例,junit,mockito,powermock,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 getPr

我有下面一节课的方法

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%的代码覆盖率。

这些是您可以测试并实现100%覆盖率的用例:

validateIdCheck返回true

validateIdCheck返回false

您的try块抛出JSONException

您的try块抛出ResourceException

您的try块抛出异常

返回响应的成功案例
值得一提的是,当您练习TDD时,您不必回答这个问题,因为在编写生产代码之前,您需要为行为编写测试。