Barry is an experienced Java developer and architect living and working in London. He has experience across a range of industries, Finance and Media in particular. Barry has posted 2 posts at DZone. View Full User Profile

Mockito 1.8 - New Useful Features

11.03.2009
| 11746 views |
  • submit to reddit

Mockito expands its impressive feature set with release 1.8.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.

  import static org.mockito.BDDMockito.*;

Seller seller = mock(Seller.class);
Shop shop = new Shop(seller);

public void shouldBuyBread() throws Exception {

//given
given(seller.askForBread()).willReturn(new Bread());

//when
Goods goods = shop.buyBread();

//then
assertThat(goods, containBread());
}

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.

   ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);  
verify(mock).doSomething(argument.capture());
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:

   <groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.0</version>

Taken from my blog at http://decodify.blogspot.com/2009/10/mockito-18-new-useful-features.html

Published at DZone with permission of its author, Barry Fitzgerald.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

David Lim replied on Tue, 2009/11/03 - 8:26pm

Yea, I used EasyMock prior to Mockito and I too agree that Mockito is less verbose and more natural to use. When I read your title as well as your first sentence, I thought mockito had released version 1.8.1. Turns out that the trailing ".1" is actually an "I" that doesn't have a space in front of it :D Silly me. Version 1.8 came out in July so it's not exactly new. I was hoping for a new release that adds support for var-args matching.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.