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

Mockito. Verify method arguments

I've googled about this, but didn't find anything relevant. I've got something like this: Object obj = getObject(); Mockeable mock= Mockito.mock(Mockeable.class); Mockito.when(mock.mymethod(obj )).thenReturn(null); Testeable testableObj = new…
manolowar
  • 6,772
  • 5
  • 23
  • 18
355
votes
4 answers

Can Mockito stub a method without regard to the argument?

I'm trying to test some legacy code, using Mockito. I want to stub a FooDao that is used in production as follows: foo = fooDao.getBar(new Bazoo()); I can write: when(fooDao.getBar(new Bazoo())).thenReturn(myFoo); But the obvious problem is that…
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
331
votes
12 answers

Verify object attribute value with mockito

I have a method call which I want to mock with mockito. To start with I have created and injected an instance of an object on which the method will be called. My aim is to verify one of the object in method call. Is there a way that mockito allows…
Priyank
  • 14,231
  • 18
  • 78
  • 107
314
votes
5 answers

Mockito - difference between doReturn() and when()

I am currently in the process of using Mockito to mock my service layer objects in a Spring MVC application in which I want to test my Controller methods. However, as I have been reading on the specifics of Mockito, I have found that the methods…
blackpanther
  • 10,998
  • 11
  • 48
  • 78
305
votes
5 answers

throw checked Exceptions from mocks with Mockito

I'm trying to have one of my mocked objects throw a checked Exception when a particular method is called. I'm trying the following. @Test(expectedExceptions = SomeException.class) public void throwCheckedException() { List list =…
Arthur Maltson
  • 5,760
  • 4
  • 30
  • 33
304
votes
23 answers

Injecting Mockito mocks into a Spring bean

I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the @Autowired annotation on private member fields. I have considered using…
teabot
  • 15,358
  • 11
  • 64
  • 79
297
votes
7 answers

Why doesn't Mockito mock static methods?

I read a few threads here about static methods, and I think I understand the problems misuse/excessive use of static methods can cause. But I didn't really get to the bottom of why it is hard to mock static methods. I know other mocking frameworks,…
Abidi
  • 7,846
  • 14
  • 43
  • 65
295
votes
5 answers

How to tell a Mockito mock object to return something different the next time it is called?

So, I'm creating a mock object as a static variable on the class level like so... In one test, I want Foo.someMethod() to return a certain value, while in another test, I want it to return a different value. The problem I'm having is that it seems…
DigitalZebra
  • 39,494
  • 39
  • 114
  • 146
289
votes
8 answers

Mockito matcher and array of primitives

With Mockito, I want to verify() a method call with byte[] in its argument list, but I didn't find how to write this. myMethod( byte[] ) I just want something like anyByteArray(), how to do that with Mockito ?
tbruyelle
  • 12,895
  • 9
  • 60
  • 74
279
votes
6 answers

Mockito verify order / sequence of method calls

Is there a way to verify if a methodOne is called before methodTwo in Mockito? public class ServiceClassA { public void methodOne(){} } public class ServiceClassB { public void methodTwo(){} } public class TestClass { public void…
froi
  • 7,268
  • 5
  • 40
  • 78
272
votes
5 answers

Mockito: Inject real objects into private @Autowired fields

I'm using Mockito's @Mock and @InjectMocks annotations to inject dependencies into private fields which are annotated with Spring's @Autowired: @RunWith(MockitoJUnitRunner.class) public class DemoTest { @Mock private SomeService service; …
user2286693
  • 3,007
  • 2
  • 17
  • 17
269
votes
10 answers

Mocking python function based on input arguments

We have been using Mock for python for a while. Now, we have a situation in which we want to mock a function def foo(self, my_param): #do something here, assign something to my_result return my_result Normally, the way to mock this would…
Juan Antonio Gomez Moriano
  • 13,103
  • 10
  • 47
  • 65
258
votes
9 answers

How do I mock an autowired @Value field in Spring with Mockito?

I'm using Spring 3.1.4.RELEASE and Mockito 1.9.5. In my Spring class I have: @Value("#{myProps['default.url']}") private String defaultUrl; @Value("#{myProps['default.password']}") private String defaultrPassword; // ... From my JUnit test,…
Dave
  • 15,639
  • 133
  • 442
  • 830
254
votes
12 answers

Using Mockito to test abstract classes

I'd like to test an abstract class. Sure, I can manually write a mock that inherits from the class. Can I do this using a mocking framework (I'm using Mockito) instead of hand-crafting my mock? How?
ripper234
  • 222,824
  • 274
  • 634
  • 905
253
votes
4 answers

Difference between @Mock, @MockBean and Mockito.mock()

When creating tests and mocking dependencies, what is the difference between these three approaches? @MockBean: @MockBean MyService myservice; @Mock: @Mock MyService myservice; Mockito.mock() MyService myservice = Mockito.mock(MyService.class);
Doug
  • 5,661
  • 2
  • 26
  • 27