JUNIT 测试无效方法 [英] JUNIT testing void methods

查看:33
本文介绍了JUNIT 测试无效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满 void 方法的 java 类,我想进行一些单元测试以获得最大的代码覆盖率.

I have a java class full of void methods, and I want to make some unit test to get maximal code coverage.

例如我有这个方法:

protected static void checkifValidElements(int arg1,  int arg2) {
    method1(arg1);
    method2(arg1);
    method3(arg1, arg2);
    method4(arg1, arg2);
    method5(arg1);
    method6(arg2);
    method7();
}

它的名字不好是有原因的,因为我翻译了代码以便更好地理解.每个方法都以某种方式验证参数是否有效并且编写得很好.

Its poorly named for a reason because I translated the code for better understanding. Each methods verify if the arguments are valid in some way and are well written.

示例:

private static void method1(arg1) {
    if (arg1.indexOf("$") == -1) {

        //Add an error message 
        ErrorFile.errorMessages.add("There is a dollar sign in the specified parameter");
    }
}

我的单元测试很好地涵盖了小方法,因为我要求他们检查 ErrorFile 是否包含错误消息,但我不知道如何测试我的方法 checkIfValidElements,它不返回任何内容或更改任何内容.当我使用 Maven 运行代码覆盖率时,它告诉我单元测试没有覆盖我班级的这一部分.

My unit test are covering the small methods fine because I ask them to check if the ErrorFile contains the error message, but I dont see how I can test my method checkIfValidElements, it returns nothing or change nothing. When I run code coverage with Maven, it tells me that the unit test doesent cover this part of my class.

我看到的唯一方法是更改​​此方法以返回 int 或 bollean 值,如下所示:

The only way I see is to change this method to return an int or bollean value, like this :

protected static int checkifValidElements(int arg1,  int arg2) {
    method1(arg1);
    method2(arg1);
    method3(arg1, arg2);
    method4(arg1, arg2);
    method5(arg1);
    method6(arg2);
    method7();
    return 0;
}

使用这种方法我可以做一个断言等于,但在我看来这样做是徒劳的.问题是我有几个类是这样设计的,它降低了我的单元测试覆盖率 %.

With this method I am able to do an assert equals, but it seems to me that it is futile to do this. The problem is that I have a couple of class that are designed like this and its lowering my unit test coverage %.

推荐答案

我想进行一些单元测试以获得最大的代码覆盖率

I want to make some unit test to get maximal code coverage

代码覆盖率永远不应成为编写单元测试的目标.您应该编写单元测试来证明您的代码是正确的,或者帮助您更好地设计它,或者帮助其他人理解代码的用途.

Code coverage should never be the goal of writing unit tests. You should write unit tests to prove that your code is correct, or help you design it better, or help someone else understand what the code is meant to do.

但我不知道如何测试我的方法 checkIfValidElements,它不返回任何内容或更改任何内容.

but I dont see how I can test my method checkIfValidElements, it returns nothing or change nothing.

好吧,您可能应该进行一些测试,在它们之间检查所有 7 种方法是否被正确调用 - 使用无效参数和有效参数,每次检查 ErrorFile 的结果.

Well you should probably give a few tests, which between them check that all 7 methods are called appropriately - both with an invalid argument and with a valid argument, checking the results of ErrorFile each time.

例如,假设有人删除了对以下内容的调用:

For example, suppose someone removed the call to:

method4(arg1, arg2);

... 或者不小心改变了参数顺序:

... or accidentally changed the argument order:

method4(arg2, arg1);

您如何注意到这些问题?以此为出发点,并设计测试来证明这一点.

How would you notice those problems? Go from that, and design tests to prove it.

这篇关于JUNIT 测试无效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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