【问题标题】:Test cases using mockito使用 mockito 的测试用例
【发布时间】:2014-09-14 09:44:37
【问题描述】:

我有一个类的以下方法。

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

【问题讨论】:

    标签: junit mockito powermock


    【解决方案1】:

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

    • validateIdCheck 返回真

    • validateIdCheck 返回错误

    • 你的 try 块抛出 JSONException

    • 你的 try 块抛出 ResourceException

    • 你的 try 块抛出异常

    • 返回响应的成功案例

    【讨论】:

    • 值得一提的是,您练习 TDD,您不必回答这个问题,因为您在编写任何产品代码之前编写行为测试。
    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    相关资源
    最近更新 更多