How to test void method with Junit testing tools?

user152462 picture user152462 · Aug 7, 2009 · Viewed 81.9k times · Source

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?

Answer

Bill the Lizard picture Bill the Lizard · Aug 7, 2009

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());
}