0% found this document useful (0 votes)
127 views1 page

Mockito Vs EasyMock Mockito - Mockito Wiki

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views1 page

Mockito Vs EasyMock Mockito - Mockito Wiki

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Search or jump to… Pull requests Issues Marketplace Explore

mockito / mockito Sponsor Watch 435 Star 11k Fork 2k

Code Issues 272 Pull requests 19 Actions Projects 1 Wiki Security Insights

Mockito vs EasyMock Edit New Page

Brice Dutheil edited this page on Apr 21, 2014 · 1 revision

Again, hats down before the EasyMock gang (record-playback is one of the coolest ideas I came across) - THANKS!!! Mockito Pages 31
started off as an EasyMock fork but we evolved too much and we don't share any code with EasyMock. Most of the logic was
rewritten, some of the code was also inspired by jMock (like the excellent ClassImposterizer ).
Home

EasyMock
Main
import static org.easymock.classextension.EasyMock.*;
What's new in Mockito 2

List mock = createNiceMock(List.class); Continuous Delivery


Overview
expect(mock.get(0)).andStubReturn("one"); Continuous Delivery
expect(mock.get(1)).andStubReturn("two"); Details
mock.clear(); Semantic Versioning
Mockito Popularity and
replay(mock); User Base

someCodeThatInteractsWithMock(); Mocking

verify(mock); FAQ
How to write good tests
Greedy algorithm of
Mockito verification InOrder
Private methods
Object creation
import static org.mockito.Mockito.*; Declaring mockito
dependency
List mock = mock(List.class); Using Spies (and Fakes)

when(mock.get(0)).thenReturn("one"); Contributing
when(mock.get(1)).thenReturn("two");
Using git for a pull
someCodeThatInteractsWithMock(); request

verify(mock).clear(); About Mockito

Features and
motivations
Similarities License
Usage in other projects
Allow the same level verification as EasyMock (unexpected invocations, redundant invocations, verification in order)
Argument matchers ( anyInt() , anyObject() , etc.)
Related projects
Forks / Other languages
Differences Java 1.4
For other languages
No record/replay modes - no need for them. There only 2 things you can do with Mockito mocks - verify or stub. Stubbing For python
goes before execution and verification afterwards.

All mocks are nice (even somehow nicer, because collection-returning methods return empty collections instead of nulls).
Social
Even though mocks are nice, you can verify them as strictly as you want and detect any unwanted interaction. Quotes

Explicit language for better readability: verify() and when() VS the mixture of expect(mock.foo()) and mock.foo()
(plain method call without expect). I'm sure some of you will find this argument subjective :) Clone this wiki locally

https://github.com/mockit
Simplified stubbing model - stubbed methods replay all the time with stubbed value no matter how many times they are
called. Works exactly like EasyMock's andStubReturn() , andStubThrow() . Also, you can stub with different return values for
different arguments (like in EasyMock).

Verification of stubbed methods is optional because usually it's more important to test if the stubbed value is used correctly
rather than where's it come from.

Verification is explicit - verification errors point at line of code showing what interaction failed.

Verification in order is flexible and doesn't require to verify every single interaction.

Custom argument matchers use hamcrest matchers, so you can use your existing hamcrest matchers. (EasyMock can also
integrate with Hamcrest though it is not a part of EasyMock but Hamcrest. See the documentation of Hamcrest).

Verification in order

EasyMock

Control control = createStrictControl();

List one = control.createMock(List.class);


List two = control.createMock(List.class);

expect(one.add("one")).andReturn(true);
expect(two.add("two")).andReturn(true);

control.replay();

someCodeThatInteractsWithMocks();

control.verify();

Mockito

List one = mock(List.class);


List two = mock(List.class);

someCodeThatInteractsWithMocks();

InOrder inOrder = inOrder(one, two);

inOrder.verify(one).add("one");
inOrder.verify(two).add("two");

Stubbing void methods

EasyMock

List mock = createNiceMock(List.class);

mock.clear();
expectLastCall().andThrow(new RuntimeException());

replay(mock);

Mockito

List mock = mock(List.class);

doThrow(new RuntimeException()).when(mock).clear();

Exact number of times verification and argument matchers

EasyMock

List mock = createNiceMock(List.class);

mock.clear();
expectLastCall().times(3);

expect(mock.add(anyObject())).andReturn(true).atLeastOnce();

replay(mock);

someCodeThatInteractsWithMock();

verify(mock);

Mockito

List mock = mock(List.class);

someCodeThatInteractsWithMock();

verify(mock, times(3)).clear();
verify(mock, atLeastOnce()).add(anyObject());

Making mocking tastier!

Repository || Wiki || @MockitoJava

© 2020 GitHub, Inc. Terms Privacy Security Status Help Contact GitHub Pricing API Training Blog About

You might also like