Questions tagged [mockito]

Mockito is a mocking framework for Java. It is inspired by EasyMock but aims to simplify mock stubbing, verification and tooling even further.

Mockito is a mocking framework for (recent versions support Android via ), and like all other mocking frameworks is used as a helper in creating mock-objects for the use of isolated unit tests.

Mockito is used to mock interfaces so that a dummy functionality can be added to a interface that can be used in unit testing.

Its creator, Szczepan Faber, wanted to create a mocking framework that is simpler to use. It is inspired and was first built upon 's code.

import static org.mockito.Mockito.*;

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

// using mock
mockedList.add("one");
mockedList.clear();

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

// stubbing and verification of stub
when(mockedList.get(0)).thenReturn("one");
assertEquals("one", mockedList.get(0));

As Android uses a different class format and a different VM, called Dalvik, running Mockito under Android requires (Dexmaker jars must be in the same classpath as Mockito jars). As of v 1.9.5, no other workarounds are required.

Mockito is generally thought not to support mocking of private, final, or static injection. Although this is mostly true, there is a caveat: from v2.1.0 onwards Mockito does support mocking final classes and methods although this feature is not available by default, instead it must be explicitly enabled.

It is more common to use as a Mockito extension to support mocking of private, final, or static injection.

The most common problem when using Mockito is setting up mocks that are not used by the class under test. Make sure to read Why is my class not calling my mocked methods in unit test? and check if it applies to your problem.

Resources

13548 questions
145
votes
2 answers

How do Mockito matchers work?

Mockito argument matchers (such as any, argThat, eq, same, and ArgumentCaptor.capture()) behave very differently from Hamcrest matchers. Mockito matchers frequently cause InvalidUseOfMatchersException, even in code that executes long after any…
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
140
votes
2 answers

How does mockito when() invocation work?

Given the following Mockito statement: when(mock.method()).thenReturn(someValue); How does Mockito go about creating a proxying something for a mock, given that the mock.method() statement will pass the return value to when()? I imagine that this…
marchaos
  • 3,316
  • 4
  • 26
  • 35
133
votes
19 answers

mock instance is null after @Mock annotation

I try to run this test: @Mock IRoutingObjHttpClient routingClientMock; @Mock IRoutingResponseRepository routingResponseRepositoryMock; @Test public void testSendRoutingRequest() throws Exception { CompleteRoutingResponse…
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
130
votes
5 answers

Comparison between Mockito vs JMockit - why is Mockito voted better than JMockit?

I'm investigating which mocking framework to use for my project and have narrowed it down to JMockit and Mockito. I notice that Mockito was voted "the best mock framework for Java" on Stackoverflow. In comparing features on JMockit's "Mocking Tool…
user63904
127
votes
3 answers

Using PowerMockito.whenNew() is not getting mocked and original method is called

I have a code somewhat like this below: Class A { public boolean myMethod(someargs) { MyQueryClass query = new MyQueryClass(); Long id = query.getNextId(); // some more code } } Class MyQueryClass { .... public Long…
user3942446
  • 1,273
  • 2
  • 9
  • 4
124
votes
10 answers

Mockito How to mock only the call of a method of the superclass

I'm using Mockito in some tests. I have the following classes: class BaseService { public void save() {...} } public Childservice extends BaseService { public void save(){ //some code super.save(); } } I…
mada
  • 1,485
  • 2
  • 12
  • 14
119
votes
1 answer

PowerMockito mock single static method and return object

I want to mock a static method m1 from a class which contains 2 static methods, m1 and m2. And I want the method m1 to return an object. I tried the following 1) PowerMockito.mockStatic(Static.class, new Answer() { @Override …
user1393653
  • 1,191
  • 2
  • 8
  • 3
117
votes
15 answers

How to mock method e in Log

Here Utils.java is my class to be tested and following is the method which is called in UtilsTest class. Even if I am mocking Log.e method as shown below @Before public void setUp() { …
user3762991
  • 1,431
  • 3
  • 11
  • 16
111
votes
7 answers

Mock a constructor with parameter

I have a class as below: public class A { public A(String test) { bla bla bla } public String check() { bla bla bla } } The logic in the constructor A(String test) and check() are the things I am trying to mock. I…
Shengjie
  • 12,336
  • 29
  • 98
  • 139
105
votes
5 answers

mock or stub for chained call

protected int parseExpire(CacheContext ctx) throws AttributeDefineException { Method targetMethod = ctx.getTargetMethod(); CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class); ExpireExpr cacheExpire =…
jilen
  • 5,633
  • 3
  • 35
  • 84
104
votes
5 answers

Can Mockito verify an argument has certain properties/fields?

Say I am mocking this class Foo class Foo { public void doThing(Bar bar) { // ... } } and this is Bar class Bar { private int i; public int getI() { return i; } public void setI(int i) { this.i = i; } } I know I can use Mockito's…
Captain Man
  • 6,997
  • 6
  • 48
  • 74
100
votes
3 answers

How can I mock methods of @InjectMocks class?

For example I have handler: @Component public class MyHandler { @AutoWired private MyDependency myDependency; public int someMethod() { ... return anotherMethod(); } public int anotherMethod() {...} } to testing it I want to…
Vova Yatsyk
  • 3,245
  • 3
  • 20
  • 34
98
votes
4 answers

mockito callbacks and getting argument values

I'm not having any luck getting Mockito to capture function argument values! I am mocking a search engine index and instead of building an index, I'm just using a hash. // Fake index for solr Hashmap fakeIndex; // Add a document…
nflacco
  • 4,972
  • 8
  • 45
  • 78
98
votes
7 answers

Test class with a new() call in it with Mockito

I have a legacy class that contains a new() call to instantiate a LoginContext object: public class TestedClass { public LoginContext login(String user, String password) { LoginContext lc = new LoginContext("login", callbackHandler); } } I…
bwobbones
  • 2,398
  • 3
  • 23
  • 32
98
votes
5 answers

Matchers.any() for null value in Mockito

Suppose I am having this object objectDemo which calls to the method objectDemoMethod with 2 parameters String and null. Now I want to verify with Mockito that this method was called: objectDemo.objectDemoMethod("SAMPLE_STRING", null); I have…
Avinash Jadhav
  • 1,243
  • 2
  • 12
  • 20