Somebody recently asked how he could test a void method that is supposed to
close a connection.

I responded:

  • Do something with the connection, make sure it works
  • Call your method
  • Do something with the connection, make sure it throws an exception

Somebody else said:

Pass it a mock connection which has an expectation of a call on to the
close() method.

The problem with this answer is that if you upgrade your JDBC driver and that
in the new version, close() is broken, your test will keep passing but your
application will fail.

Mock objects can give you a deceptive sense of confidence, and that’s why you
should avoid them unless there is really no alternative.