Saturday, March 26, 2011
Mockito, static imports and Eclipse
I recently started using Mockito as a mocking framework. Mockito makes heavy use of static imports which by default in Eclipse are not accessible through code completion. It is possible to use the Mockito methods by accessing them through their class by writing Mockito.when but this would quickly clutter your code with a lot of Mockito references. Code completion has been around for ages so not having it feels like coding in Notepad or vi. Fortunately I have found out that it is actually possible and quite easy to configure Eclipse to add the static imports which is a great relief.
Simply open the Preferences in Eclipse and go to the menu item Java -> Editor -> Content Assist -> Favorites and press the button New Type.... Enter the text org.mockito.Mockito and press OK. Now you have all static methods on the Mockito class available to you through code completion.
Other useful Mockito classes to add are org.mockito.Matchers and org.mockito.BDDMockito (if you are into BDD).
posted by Peter Haldbæk @ 14:22,