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
250
votes
8 answers

Mockito.any() pass Interface with Generics

is it possible to pass the type of an interface with generics? The interface: public interface AsyncCallback In my test method: Mockito.any(AsyncCallback.class) Putting behind or for .class didnt work.
lrxw
  • 3,576
  • 7
  • 32
  • 46
242
votes
5 answers

Unfinished Stubbing Detected in Mockito

I am getting following exception while running the tests. I am using Mockito for mocking. The hints mentioned by Mockito library are not helping. org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: ->…
Royal Rose
  • 2,531
  • 2
  • 11
  • 5
231
votes
4 answers

Using Mockito's generic "any()" method

I have an interface with a method that expects an array of Foo: public interface IBar { void doStuff(Foo[] arr); } I am mocking this interface using Mockito, and I'd like to assert that doStuff() is called, but I don't want to validate what…
ripper234
  • 222,824
  • 274
  • 634
  • 905
215
votes
7 answers

Example of Mockito's argumentCaptor

Can anyone please provide me an example showing how to use the org.mockito.ArgumentCaptor class and how it is different from simple matchers that are provided with mockito. I read the provided mockito documents but those doesn't illustrate it…
Ujjwal
  • 2,365
  • 2
  • 11
  • 7
210
votes
14 answers

How to resolve Unneccessary Stubbing exception

My Code is as below, @RunWith(MockitoJUnitRunner.class) public class MyClass { private static final String code ="Test"; @Mock private MyClassDAO dao; @InjectMocks private MyClassService Service = new…
VHS
  • 2,109
  • 2
  • 8
  • 4
209
votes
4 answers

How to use Mockito with JUnit5

How can I use injection with Mockito and JUnit 5? In JUnit4 I can just use the @RunWith(MockitoJUnitRunner.class) Annotation. In JUnit5 is no @RunWith Annotation?
Daniel Käfer
  • 4,458
  • 3
  • 32
  • 41
209
votes
6 answers

When to use Mockito.verify()?

I write jUnit test cases for 3 purposes: To ensure that my code satisfies all of the required functionality, under all (or most of) the input combinations/values. To ensure that I can change the implementation, and rely on JUnit test cases to tell…
Russell
  • 2,091
  • 2
  • 13
  • 5
205
votes
6 answers

Mockito match any class argument

Is there a way to match any class argument of the below sample routine? class A { public B method(Class a) {} } How can I always return a new B() regardless of which class is passed into method? The following attempt only works…
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
204
votes
6 answers

Simulate first call fails, second call succeeds

I want to use Mockito to test the (simplified) code below. I don't know how to tell Mockito to fail the first time, then succeed the second time. for(int i = 1; i < 3; i++) { String ret = myMock.doTheCall(); if("Success".equals(ret)) { …
jb.
  • 9,921
  • 12
  • 54
  • 90
203
votes
4 answers

Mockito: List Matchers with generics

Mockito offers: when(mock.process(Matchers.any(List.class))); How to avoid warning if process takes a List instead?
Philippe Blayo
  • 10,610
  • 14
  • 48
  • 65
199
votes
7 answers

Mockito + PowerMock LinkageError while mocking system class

I've got such a code snippet: @RunWith(PowerMockRunner.class) @PrepareForTest({Thread.class}) public class AllMeasuresDataTest { @Before public void setUp() throws Exception { } @Test public void testGetMeasures() { AllMeasuresData measure =…
Wojciech Reszelewski
  • 2,656
  • 2
  • 18
  • 27
194
votes
12 answers

How to properly match varargs in Mockito

I've been trying to get to mock a method with vararg parameters using Mockito: interface A { B b(int x, int y, C... c); } A a = mock(A.class); B b = mock(B.class); when(a.b(anyInt(), anyInt(), any(C[].class))).thenReturn(b); assertEquals(b,…
qualidafial
  • 6,674
  • 3
  • 28
  • 38
189
votes
3 answers

Mockito : doAnswer Vs thenReturn

I am using Mockito for service later unit testing. I am confused when to use doAnswer vs thenReturn. Can anyone help me in detail? So far, I have tried it with thenReturn.
Rajkumar Thambu
  • 2,061
  • 2
  • 15
  • 7
188
votes
5 answers

What is the difference between mocking and spying when using Mockito?

What would be a use case for a use of a Mockito spy? It seems to me that every spy use case can be handled with a mock, using callRealMethod. One difference I can see is if you want most method calls to be real, it saves some lines of code to use a…
Victor Grazi
  • 15,563
  • 14
  • 61
  • 94
187
votes
8 answers

Mockito: InvalidUseOfMatchersException

I have a command line tool that performs a DNS check. If the DNS check succeeds, the command proceeds with further tasks. I am trying to write unit tests for this using Mockito. Here's my code: public class Command() { // .... void…
devang
  • 5,376
  • 7
  • 34
  • 49