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
1181
votes
11 answers

How to mock void methods with Mockito

How to mock methods with void return type? I implemented an observer pattern but I can't mock it with Mockito because I don't know how. And I tried to find an example on the Internet but didn't succeed. My class looks like this: public class World…
ibrahimyilmaz
  • 18,331
  • 13
  • 61
  • 80
906
votes
7 answers

How to verify that a specific method was not called using Mockito?

How to verify that a method is not called on an object's dependency? For example: public interface Dependency { void someMethod(); } public class Foo { public bar(final Dependency d) { ... } } With the Foo test: public class…
beluchin
  • 12,047
  • 4
  • 24
  • 35
900
votes
10 answers

Making a mocked method return an argument that was passed to it

Consider a method signature like: public String myFunction(String abc); Can Mockito help return the same string that the method received?
Abhijeet Kashnia
  • 12,290
  • 8
  • 38
  • 50
730
votes
13 answers

Difference between @Mock and @InjectMocks

What is the difference between @Mock and @InjectMocks in Mockito framework?
user2249972
  • 7,321
  • 3
  • 13
  • 5
601
votes
5 answers

Use Mockito to mock some methods but not others

Is there any way, using Mockito, to mock some methods in a class, but not others? For example, in this (admittedly contrived) Stock class I want to mock the getPrice() and getQuantity() return values (as shown in the test snippet below) but I want…
Victor Grazi
  • 15,563
  • 14
  • 61
  • 94
593
votes
4 answers

Mockito test a void method throws an exception

I have a method with a void return type. It can also throw a number of exceptions so I'd like to test those exceptions being thrown. All attempts have failed with the same reason: The method when(T) in the type Stubber is not applicable for the…
edwardmlyte
  • 15,937
  • 23
  • 58
  • 83
580
votes
6 answers

Can Mockito capture arguments of a method called multiple times?

I have a method that gets called twice, and I want to capture the argument of the second method call. Here's what I've tried: ArgumentCaptor firstFooCaptor = ArgumentCaptor.forClass(Foo.class); ArgumentCaptor secondFooCaptor =…
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
497
votes
22 answers

Mocking static methods with Mockito

I've written a factory to produce java.sql.Connection objects: public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory { @Override public Connection getConnection() { try { return…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
489
votes
10 answers

Mockito: Trying to spy on method is calling the original method

I'm using Mockito 1.9.0. I want mock the behaviour for a single method of a class in a JUnit test, so I have final MyClass myClassSpy = Mockito.spy(myInstance); Mockito.when(myClassSpy.method1()).thenReturn(myResults); The problem is, in the…
Dave
  • 15,639
  • 133
  • 442
  • 830
477
votes
8 answers

Mockito : how to verify method was called on an object created within a method?

I am new to Mockito. Given the class below, how can I use Mockito to verify that someMethod was invoked exactly once after foo was invoked? public class Foo { public void foo(){ Bar bar = new Bar(); bar.someMethod(); } } I…
mre
  • 43,520
  • 33
  • 120
  • 170
475
votes
14 answers

Using Mockito with multiple calls to the same method with the same arguments

Is there a way to have a stubbed method return different objects on subsequent invocations? I'd like to do this to test nondeterminate responses from an ExecutorCompletionService. i.e. to test that irrespective of the return order of the methods,…
Emma
  • 6,112
  • 2
  • 18
  • 11
421
votes
2 answers

How to verify a method is called two times with mockito verify()

I want to verify if a method is called at least once through mockito verify. I used verify and it complains like this: org.mockito.exceptions.verification.TooManyActualInvocations: Wanted 1 time: But was 2 times. Undesired invocation:
Ahmad Beg
  • 4,285
  • 3
  • 14
  • 5
383
votes
12 answers

Using Mockito to mock classes with generic parameters

Is there a clean method of mocking a class with generic parameters? Say I have to mock a class Foo which I need to pass into a method that expects a Foo. I can do the following easily enough: Foo mockFoo =…
Tim Clemons
  • 6,231
  • 4
  • 25
  • 23
378
votes
8 answers

How to capture a list of specific type with mockito

Is there a way to capture a list of specific type using mockitos ArgumentCaptore. This doesn't work: ArgumentCaptor> argument = ArgumentCaptor.forClass(ArrayList.class);
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
371
votes
28 answers

How to mock a final class with mockito

I have a final class, something like this: public final class RainOnTrees{ public void startRain(){ // some code here } } I am using this class in some other class like this: public class Seasons{ RainOnTrees rain = new…
buttowski
  • 4,657
  • 8
  • 27
  • 33
1
2 3
99 100