Definition:
Mocking is replacing all the collaborators and dependencies of the class under test by mocks or fake objects so you can easily and efficiently unit test it.
Why do you need Mocking for?
- It gives you the ability to focus your test on the functionality of a code unit.
- It removes your dependency on the different components of your app, such as database...
- It makes your tests run faster.
- It allows you to have a clean and better design (interface driven design).
- Base your design on interfaces instead of inheriting from a class, because of the fact that classes may sometimes be final or have static or final methods and therefore will not allow you to override them.In the diagram above, we used the interface implemented by one of the collaborators of the class under test to create our mock.
- make your objects as small as possible so you can easily mock them (divide and conquer principle).
- remove the instantiation of the collaborators from within the class under test so you can mock them.
- http://www.jmock.org
- http://www.easymock.org
- http://code.google.com/p/mockito
No comments:
Post a Comment