Coder Social home page Coder Social logo

mockito's Introduction

Mockito

simplier & better mocking

Build Status Coverage Status Current release Maven Central

Current release

See the release notes page and latest documentation.

JDK8 status

Mockito should work fine with JDK8 if you stay away from defender methods. Lambda usage may work just as good for Answers. We're unsure about every JDK8 features at the moment, like serializing a mock that uses a lambda. Error report and pull request are welcome though (contributing guide).

Moving to github

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

For now only the code repository is moved, other stuff will follow like the wiki pages. Documentation may or may not follow. But discussion group, issue tracker will stay at google (unfortunatley GH PR counters incremented before we had the chance to move our issues).

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?

Recommended way of getting Mockito is declaring a dependency on "mockito-core" library (not "mockito-all"!) using your favorite build system. My favorite build system happens to be Gradle and so I do:

repositories {
  jcenter()
}

dependencies {
  testCompile "org.mockito:mockito-core:1.+"
}

For legacy builds with manual dependency management it might be useful to use mockito-all package that can be downloaded from Mockito's Bintray repository or Bintray's jcenter.

For more information please refer to the wiki page.

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 concrete 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 then David Wallace.

Special thanks to Erik Ramfelt, and Steve Christou for putting Mockito on there Jenkins server (continuous integration) for a few years. Now Travis and Coveralls took over.

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

mockito's People

Contributors

mockitoguy avatar bric3 avatar iczechowski avatar bbankowski avatar continuous-delivery-drone avatar marcingrzejszczak avatar dawood-ibn-kareem avatar lpandzic avatar szpak avatar alberskib avatar guw avatar hughwphamill avatar hmlnarik avatar ianparkinson avatar kamilszymanski avatar pimterry avatar michalborek avatar mariusvolkhart avatar afnrfcb7 avatar lucascs avatar jerzykrlk avatar ludomeurillon avatar jrrickard avatar andrerigon avatar philipa avatar markuswustenberg avatar meddahj avatar ludochane avatar tradingreason avatar mileszk avatar

Watchers

James Cloos avatar Narendra Pathai avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.