31

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?

Rotek
  • 65
  • 1
  • 10
user152462
  • 311
  • 1
  • 3
  • 3

6 Answers6

43

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());
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
15

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.

Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
3

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.

CPerkins
  • 8,968
  • 3
  • 34
  • 47
0

So - check after the call of the method the queue, if the values passed to the method are added to the queue. You need access to the queue (something like getQueue()) for this.

Mnementh
  • 50,487
  • 48
  • 148
  • 202
  • If you need such a thing for testing-purposes it hints, that you may need it in productive use too. The method has a side-effect and these effect will be accessible in some sort. But getQueue() was an example. Getting the size/length of the queue as in the other answers is also some sort of access to the queue, but not a direct accessor. That is also OK. Let the test-case (based on a requirement) drive your design, that's why it is called test-driven-design. :-) – Mnementh Aug 07 '09 at 14:59
0

Use jMock library and mock a storage that holds your queue, jMock allows to assert whether mocked method was called and with what parameters.

Chris Ciesielski
  • 1,203
  • 2
  • 10
  • 19
0

We have two ways to check the following :

  1. Verify using the size of the Queue/Collection as in the program below : (As pointed out by Patrick)

    @Test
    public void testfollowlinkAdd() {
      int size = queue.length();
      queue.add(pageLink);   //Considering an Object of PageLink
      Assert.assertTrue(size+1, queue.length());
    }
    

OR

  1. Make use of MockObjects. Frameworks like Mockito can be helpful here.
Dhaval Simaria
  • 1,886
  • 3
  • 28
  • 36
Subodh Karwa
  • 2,495
  • 1
  • 15
  • 13