Testing Delegate Methods

Posted on Fri 15 April 2016 in General Software Development, Testing

We had an interesting discussion the other day: should we write unit tests for delegate methods, that is, methods that only delegate the call to a delegate object? Here's an example of such a method:

public void do(Object o1, Object o2) {

I said I did not see much value in a unit test for this method as all the test could do is verify whether the do() method of the mocked delegate object was called. Instead of testing behaviour, we would test the wiring which is close to trivial in this case and should be tested implicitly as part of an integration test involving the delegate object and the main object.

Some of my colleagues disagreed with me. Their argument was that a unit test would detect changes in the delegate call and indicate the intended usage of the delegate object. They were concerned an incorrect wiring could be introduced without a unit test. And this unit test would be cheap to write.

I did not find any of these arguments very convincing - and I'm in good company: the JUnit team's FAQ webpage has a section on Best Practices which comments on testing delegate methods. You can read it here. Thank you, J. B. Rainsberger!