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

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

问题描述

我有一个充满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测试void方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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