方法的输入参数为异常时的模拟语句 [英] Mocking statement when the input parameter of method as exception

查看:103
本文介绍了方法的输入参数为异常时的模拟语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为以下场景编写Junit测试用例,并且需要一些建议来覆盖以下代码段:

I'm writing Junit test case for the below scenario and need some suggestions to cover the below snippet:

ErrorHandlingUtils.manageException(new InvalidInputException("Ethernet Ring Name not found"),methodName);

我尝试将ringName传递为null或空,但不确定如何模拟异常块.有人可以提出建议吗?

I tried passing ringName as null or empty but not sure how to mock exception block. Can someone please give a suggestion?

public void deleteEthernetRing(String ringName, String userID) throws Exception {
    LOGGER.info("Delete Ethernet Ring ");
    String methodName = "Delete Ethernet Ring ";
    ResponsePayLoad returnVal = new ResponsePayLoad();
    HttpHeaders responseHeaders = new HttpHeaders();

    if (StringUtils.isBlank(ringName))
        // NOT COVERED
        ErrorHandlingUtils.manageException(new InvalidInputException("Ethernet Ring Name not found"),methodName);
    if (userID == null || userID.isEmpty()) {
        ErrorHandlingUtils.manageException(new InvalidInputException("UserID Must be provided to remove Ring"),methodName);
    } else {
        // The actual business logic 
    }
}

推荐答案

正如@AndyTurner所指出的那样,问题的答案与如何声明方法以及如何测量代码覆盖率有关.

As @AndyTurner pointed out the answer to your question is related on how you declare a method and how code coverage is measured.

在下面的Utils类中检查(基本上)相同方法的2个版本.

Check the Utils class below for 2 version of (basically) the same method.

static class Utils {

    public static void handleException1(Exception e) throws Exception {
        throw e;
    }

    public static Exception handleException2(Exception e) {
        return e;
    }
}


static class Example1 {
    public boolean test(boolean flag) throws Exception {
        if (flag) {
            Utils.handleException1(new Exception());
        }
        return true;
    }
}

使用代码覆盖率工具"执行Example1.test(true)会导致带有handleException方法的行标记为未覆盖.

Executing Example1.test(true) with a 'code coverage tool' leads to the line with the handleException method marked as not covered.

static class Example2 {
    public boolean test(boolean flag) throws Exception {
        if (flag) {
            throws Utils.handleException2(new Exception());
        }
        return true;
    }
}

使用代码覆盖率工具"执行Example2.test(true)会导致该行标记为已覆盖.

Executing Example2.test(true) with a 'code coverage tool' leads to the line marked as covered.

正如@AndyTurner所指出的,其原因是在Example1中,编译器"/代码覆盖率工具"不知道方法handleException1将永远不会返回.它希望存在这样的路径,因此不会将此行标记为已覆盖.

As @AndyTurner pointed out, the reason for that is that in Example1 the 'compiler' / 'code coverage tool' does not know that the method handleException1 will never return. It expects that such a path exists and therefore does not mark this line as covered.

Example2中,它看到throws关键字,并且知道如果代码中的这一点该方法在此处结束.因此,涵盖了所有可能的路径.

In Example2 it sees the throws keyword and knows that if this point in the code the method ends here. Therefore all possible pathes are covered.

您是否要(或需要)模拟该方法是一个完全不同的问题.但是从您的问题来看,您的目标是实现代码覆盖率,因此更改代码应该可以解决该问题.

Whether or not you want (or need) to mock the method is an entirely different question. But judging from your question(s) your aim was to achieve code coverage, so changing your code should solve that issue.

这篇关于方法的输入参数为异常时的模拟语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆