Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUNIT testing void methods

Tags:

java

junit

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 %.

like image 675
metraon Avatar asked Apr 16 '13 17:04

metraon


People also ask

What assert in void method?

Whenever we write unit test cases for any method, we expect a return value from the method. Generally, we use assert for checking if the method returns the value that we expect it to return, but in the case of void methods, they do not return any value.

Can we mock void methods?

Mockito provides following methods that can be used to mock void methods. doAnswer() : We can use this to perform some operations when a mocked object method is called that is returning void. doThrow() : We can use doThrow() when we want to stub a void method that throws exception.


1 Answers

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.

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

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.

like image 193
Jon Skeet Avatar answered Oct 03 '22 11:10

Jon Skeet