Skip to content

Latest commit

 

History

History
115 lines (76 loc) · 5.72 KB

README.md

File metadata and controls

115 lines (76 loc) · 5.72 KB

Mockito

simplier & better mocking

Current release

06/10/2012 Mockito 1.9.5 released! See the release notes. Should appear in maven cdntral shortly.

Moving to github

We are currently moving a few stuff from Google Code to Github.

For now only the code repository is moved, issue tracker might follow, but other stuff might stay there like the wiki pages and documentation, discussion group will stay at google.

Why drink it?

Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with clean & simple API. Mockito doesn't give you hangover because the tests are very readable and they produce clean verification errors. Read more about features & motivations.

"We decided during the main conference that we should use JUnit 4 and Mockito because we think they are the future of TDD and mocking in Java" - Dan North, the originator of BDD

More quotes

Over 15000 downloads of 1.9.0 version ('12), excluding maven/Gradle users. For latest figures see the downloads.

More about the user base

How do I drink it?

Download mockito-all-x.x.x.jar and put it on the classpath. If you use a fancy build system with declarative dependencies like Gradle or Maven please -> Click HERE <-

Then you can verify interactions

import static org.mockito.Mockito.*;

// mock creation
List mockedList = mock(List.class);

// using mock object ; observe that it didn't throw any "unexpected interaction exception" exception
mockedList.add("one");
mockedList.clear();

// selective & explicit verification
verify(mockedList).add("one");
verify(mockedList).clear();

Or stub method calls

// you can mock conmcrete classes, not only interfaces
LinkedList mockedList = mock(LinkedList.class);

// stubbing; before the actual execution
when(mockedList.get(0)).thenReturn("first");

// the following prints "first"
System.out.println(mockedList.get(0));

// the following prints "null" because get(999) was not subbed
System.out.println(mockedList.get(999));

You can go further

  • Try the annotations @Mock, @Spy, @Captor, @InjectMocks
  • Try BDD syntax with BDDMockito
  • If the provided answers doesn't fit your needs, write one yourself extending the Answer interface
  • Try the Mockito on Android, thanks to the Google guys working on dexmaker (more on that later)

Remember

  • Do not mock types you don't own
  • Don't mock value objects
  • Don't mock everything
  • Show some love with your tests

Click here for more documentation and examples. Remember all documentation lives in javadocs so you don’t need to visit that page too often. You can grab the RefCard? here.

If you have any suggestions, find documentation unclear or you found a bug, write to our mailing list. You can report bugs here.

Who is your bartender?

Mockito is served to you by Szczepan Faber and friends. First people who tried Mockito were developers of the Guardian project in London in early 2008. Here is how Szczepan explained why we need another mocking framework?

Firstly, hats down before EasyMock folks for their ideas on beautiful and refactorable mocking syntax. First hacks on Mockito were done on top of the EasyMock? code.

Here are just some of my friends who contributed ideas to Mockito (apologize if I missed somebody): Igor Czechowski, Patric Fornasier, Jim Barritt, Felix Leipold, Liz Keogh, Bartosz Bańkowski.

Now some other people joined the gang : Brice Dutheil and David Wallace.

Special thanks to Erik Ramfelt, and Steve Christou for putting Mockito on his Jenkins server (continuous integration).

Thanks to Karol Poźniak for the logo :)

Finally, thanks to Erik Brakkee who helps us getting jars to maven central

links wrap-up

Wiki

Blogs

Other project links

Groups