You have brand new class. Let's call it
Being a nice human being, you want to test your implementation. You might even be a disciple of TDD and haven't done your implementation yet. You want your test first!
Your tested class will depend on others so you figured you need a mocking framework. Good for you. Add EasyMock dependency to your POM file.
addDocument should do stuff and then notify a dependency. Let's
Now, a word of warning. I will mock an interface in this example. That doesn't mean you should only mock interfaces. I hate useless interfaces. And you want me to be happy. Please don't create an interface just for the pleasure of mocking it. Just mock the concrete class. Thank you.
So, we want to make sure
addDocument is notifying
Collaborator by calling
documentAdded with the right title in argument. Our todo list to do that:
Then the code fulfilling it:
And that's all you need to get you started. Some comments though:
EasyMockSupport is useful but not mandatory.
It allows to call
replayAll instead of
replay(mock1, mock2, ...) for instance
EasyMockRule) so you won't need a setter only used for testing
documentAdded is called only once and receiving this exact parameter.
Any other call to our mock is a test failure