Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test void method with Junit testing tools?

I just happen to implement a method void followlink(obj page,obj link) which simply adds page and link to queue. I have unsuccessfully tried to test this kind of method.

All I want is to test that in the queue contains page and link received from followlink method. My test class already extends TestCase. So what is the best way to test such a method?

like image 264
user152462 Avatar asked Aug 07 '09 12:08

user152462


People also ask

How do you test a void method in unit testing?

The most popular option to verify the output when testing the void method is to use the mock object. A mock object is an object that replicates the behavior of a real object for the purposes of unit testing. For example, you can observe how the class under test calls the dependency with a mock object.

How do I verify a void in Mockito?

Using the verify() Method Mockito provides us with a verify() method that lets us verify whether the mock void method is being called or not. It lets us check the number of methods invocations. So, if the method invocation returns to be zero, we would know that our mock method is not being called.

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.


3 Answers

The JUnit FAQ has a section on testing methods that return void. In your case, you want to test a side effect of the method called.

The example given in the FAQ tests that the size of a Collection changes after an item is added.

@Test public void testCollectionAdd() {     Collection collection = new ArrayList();     assertEquals(0, collection.size());     collection.add("itemA");     assertEquals(1, collection.size());     collection.add("itemB");     assertEquals(2, collection.size()); } 
like image 171
Bill the Lizard Avatar answered Sep 25 '22 15:09

Bill the Lizard


You could test the size if the queue before and after calling your method, something like:

int size = queue.length(); followLink(page, link); assertEquals(size+1, queue.length()); // or maybe size+2? 

another thing you might do is start off with an empty queue, call followLink, then dequeue the first item and test its values.

like image 33
Patrick McDonald Avatar answered Sep 22 '22 15:09

Patrick McDonald


Most likely, your queue is private, so checking the size isn't going to work. The "design for testing" solution I've seen is to make use of package private methods and members instead. Since your junit tests are likely to be in the same package, they'll have access.

That by itself lets the "queue.length()" side effect test work.

But I'd go further: really, you should consider checking that your method has inserted the correct page and link to your queue. The details for that require more knowledge about how you're representing (and combining) page and link.

The jMock solution is also very good, though in truth I'm much more familiar with writing my own test harnesses.

like image 20
CPerkins Avatar answered Sep 25 '22 15:09

CPerkins