Sunday, October 4, 2009

Mockito 1.8 - new useful features

I was once a happy EasyMock user. If asked, I think I would have even questioned the need for a new mocking framework – EasyMock did it all, didn’t it?
But after using Mockito for a short while I was impressed by its efficiency and ease of use. The key features for me were its simple and intuitive API and the way any Mock object will return sensible defaults for all of its methods – allowing you to concentrate on the behaviour of the methods you care about.
The framework continues to improve incrementally with each release. This post gives a quick view of two of the most useful new features in Mockito 1.8

BDD-Style Language Supported Natively

BDD (Behaviour Driven Development) is rapidly becoming mainstream. Standard BDD conventions recommend the use of given-when-then comments in your tests. This allows improved readability by giving clear delineation between test setup, execution and assertions.
Mockito’s choice of method names previously were in conflict with these BDD conventions. Version 1.8 allows you to stick with the BDD conventions while also remaining backward compatible will older Mockito tests.
   1: import static org.mockito.BDDMockito.*;
   2:  
   3: Seller seller = mock(Seller.class);
   4: Shop shop = new Shop(seller);
   5:  
   6: public void shouldBuyBread() throws Exception {
   7:   //given  
   8:   given(seller.askForBread()).willReturn(new Bread());
   9:   
  10:   //when
  11:   Goods goods = shop.buyBread();
  12:   
  13:   //then
  14:   assertThat(goods, containBread());
  15: } 



Capturing arguments


Mockito has always supported a wide range of matchers to allow verification that mocked methods are invoked with the expected arguments. However sometimes (especially when dealing with legacy code) this can limit the checks you can do without the overhead of writing a custom matcher. 


Version 1.8 introduces new functionality to allow you to capture and store the arguments passed the mocked methods. Standard jUnit assertions can then be applied to the captured arguments. Over reliance on capturing arguments would be a code smell in my opinion as most well abstracted code should not need to do this. However for testing legacy code and interactions with outside systems ArgumentCaptors can be very useful.



   1: ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
   2:    verify(mock).doSomething(argument.capture());
   3:    assertEquals("John", argument.getValue().getName())


More Info…



Downloads and more information can be found at mockito.org. For maven users – simply add the following dependency to your pom:



   1: <groupId>org.mockito</groupId>
   2: <artifactId>mockito-all</artifactId>
   3: <version>1.8.0</version> 


4 comments: