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
184
votes
1 answer

Java verify void method calls n times with Mockito

I'm trying to verify that a (void) method is being called inside of a DAO - I'm using a commit point that sends a list of results up to that point, resets the list and continues. Say I have 4 things in the list and I have a commit point of 1, I…
nbpeth
  • 2,967
  • 4
  • 24
  • 34
183
votes
9 answers

Mockito - @Spy vs @Mock

I understand a spy calls the real methods on an object, while a mock calls methods on the double object. Also spies are to be avoided unless there is a code smell. However, how do spies work and when should I actually use them? How are they…
Abhinav
  • 1,911
  • 2
  • 13
  • 11
180
votes
15 answers

Mockito How to mock and assert a thrown exception?

I'm using mockito in a junit test. How do you make an exception happen and then assert that it has (generic pseudo-code)
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
179
votes
33 answers

Mockito - NullpointerException when stubbing Method

So I started writing tests for our Java-Spring-project. What I use is JUnit and Mockito. It's said, that when I use the when()...thenReturn() option I can mock services, without simulating them or so. So what I want to do is, to…
user5417542
  • 3,146
  • 6
  • 29
  • 50
173
votes
8 answers

Initialising mock objects - Mockito

There are many ways to initialize a mock object using MockIto. What is best way among these ? 1. public class SampleBaseTestCase { @Before public void initMocks() { MockitoAnnotations.initMocks(this); …
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
173
votes
3 answers

How to use ArgumentCaptor for stubbing?

In Mockito documentation and javadocs it says It is recommended to use ArgumentCaptor with verification but not with stubbing. but I don't understand how ArgumentCaptor can be used for stubbing. Can someone explain the above statement and show…
Can't Tell
  • 12,714
  • 9
  • 63
  • 91
168
votes
38 answers

Could not initialize plugin: interface org.mockito.plugins.MockMaker

I'm getting following exception once tests is started: Testcase: treeCtorArgumentTest(com.xythos.client.drive.cachedtree.CachedTreeTest): Caused an ERROR Could not initialize plugin: interface…
Paskas
  • 2,622
  • 2
  • 14
  • 10
168
votes
9 answers

Mockito: Mock private field initialization

How I can mock a field variable which is being initialized inline? class Test { private Person person = new Person(); ... public void testMethod() { person.someMethod(); ... } } Here I want to mock…
Arun
  • 2,360
  • 2
  • 17
  • 23
166
votes
9 answers

Mocking member variables of a class using Mockito

I am a newbie to development and to unit tests in particular . I guess my requirement is pretty simple, but I am keen to know others thoughts on this. Suppose I have two classes like so - public class First { Second second ; public…
Anand Hemmige
  • 3,593
  • 6
  • 21
  • 31
160
votes
7 answers

How to verify multiple method calls with different params

I have the following method that I wish to verify behaviour on. public void methodToTest(Exception e, ActionErrors errors) { ... errors.add("exception.message", ActionMessageFactory.createErrorMessage(e.toString())); …
Brad
  • 15,186
  • 11
  • 60
  • 74
159
votes
5 answers

Mockito: Stubbing Methods That Return Type With Bounded Wild-Cards

Consider this code: public class DummyClass { public List dummyMethod() { return new ArrayList(); } } public class DummyClassTest { public void testMockitoWithGenerics() { DummyClass dummyClass…
Shikhar Mishra
  • 1,965
  • 2
  • 13
  • 14
156
votes
13 answers

Testing Private method using mockito

public class A { public void method(boolean b){ if (b == true) method1(); else method2(); } private void method1() {} private void method2() {} } public class TestA { @Test …
Nageswaran
  • 7,481
  • 14
  • 55
  • 74
154
votes
2 answers

how to verify a method of a non-mock object is called?

It seems mockito only verifies whether a method of a mock object is called and the mock object always have something like doReturn().when(mock object) But can I create a mock object and define doReturn().when(mock object) and then verify a method of…
user389955
  • 9,605
  • 14
  • 56
  • 98
149
votes
2 answers

@RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this)

While writing a new jUnit4 test, I'm wondering whether to use @RunWith(MockitoJUnitRunner.class) or MockitoAnnotations.initMocks(this). I created a new test & the wizard automatically generated a test with the Runner. Javadocs for MockitoJUnitRunner…
OceanBlue
  • 9,142
  • 21
  • 62
  • 84
148
votes
21 answers

when I run mockito test occurs WrongTypeOfReturnValue Exception

Error detail: org.mockito.exceptions.misusing.WrongTypeOfReturnValue: Boolean cannot be returned by updateItemAttributesByJuId() updateItemAttributesByJuId() should return ResultRich This exception might occur in wrongly written multi-threaded…
confused windbell
  • 1,481
  • 2
  • 10
  • 5