All Posts
Using Reflection To Create Mock Objects
Well-established tools like Mockito and EasyMock offer a powerful range of features for defining and interacting with mock objects. I use both heavily in my unit tests, and would recommend them to anyone looking for a mocking framework. Sometimes, however, a real (i.e. non-proxied) collaborator is called for, or adding third-party libraries may not be an option. In most cases, a suitable object could be instantiated by calling the constructor and setting it as a dependency to the object under test. Suppose though that we have the following ‘Footballer’ class, and we want to write a test for the ‘getAvgGoalsPerGame()’ method: