I have a java class full of void methods, and I want to make some unit test to get maximal code coverage.
For example I have this method :
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.
Example :
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");
}
}
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.
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 %.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…