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

Mocking Hibernate Session

I was trying to mock hibernate session. This is the code snippet I tried: @Before public void setUp() { campaignModel = DraftTestHelper.buildDraftModel(); if(sessionFactory != null) { System.out.println("Session Factory not null"); …
user3681970
  • 1,201
  • 4
  • 19
  • 37
5
votes
4 answers

Mock android Patterns with mockito

I want validate an email with some code provided by Android. Here is the code I want to mock : if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()) throw new InvalidPhoneException(phone); In my test file…
Xero
  • 3,951
  • 4
  • 41
  • 73
5
votes
2 answers

why Mock object has doreturn and thenreturn for mock?

Note: I understand that in spy we can differenciate among these two. I went all over internet but I still some doubts on doreturn/when and when/thenreturn in Mockito .Below is my doubt, 1)Are doreturn/when and when/thenreturn acts same for mock…
Avinash Jethy
  • 803
  • 2
  • 10
  • 25
5
votes
2 answers

Creating intent in test: "Method putExtra in android.content.Intent not mocked"

I'm trying to unit test a broadcast receiver which listens for "com.android.music.metachanged" intents using JUnit4 and Mockito. The broadcast receiver starts a service when it receives an intent. I want to assert that the service is started. I also…
ExplodingTiger
  • 327
  • 2
  • 6
  • 18
5
votes
1 answer

Is there any way to disable recording invocations with mockito

I have some tests, where I use Mockito mocks. I do not use verifications in the test at all. Now I would like to make some performance tests, and the mocks cause OutOfMemoryError. I could try to refactor the code to be able to "reset" the mocks.…
Gábor Lipták
  • 9,646
  • 2
  • 59
  • 113
5
votes
1 answer

Mockito counting stubbing as invocation

Trying to stub a class with 2 possible invocation/return paths using custom Matcher ... ran into an interest problem. Here is a test I wrote to illustrate ... This might be difficult to implement, but I would have expected the 1st ArgumentMatcher…
echen
  • 2,002
  • 1
  • 24
  • 38
5
votes
1 answer

How to unit test methods that interact with System (or Android) classes

How do you manage to write unit tests that interact with the system classes i.e. Android Framework classes? Imagine you have those classes: public class DeviceInfo { public final int screenWidth, screenHeight; public final String model; …
Addev
  • 31,819
  • 51
  • 183
  • 302
5
votes
2 answers

No converter found for return value of type: class java.util.LinkedHashMap

I wanted to get json response for exception in mockito unit testing. This is my Application configuration file. @Configuration @EnableWebMvc @ComponentScan(basePackages = "com.spring") public class AppConfig extends WebMvcConfigurerAdapter{ …
dev
  • 451
  • 4
  • 10
  • 23
5
votes
1 answer

How to inject mock in parent class with Mockito?

I'm using mockito to mock my services.. How can I inject mocks in parent class? Sample: public abstract class Parent(){ @Mock Message message; } public class MyTest() extends Parent{ @InjectMocks MyService myService //MyService…
Lucas
  • 1,251
  • 4
  • 16
  • 34
5
votes
1 answer

How to verify that specific class method was passed as parameter?

In class MyClass that I test I have: public void execute(){ service.call(ThisClass::method1); } And following: void method1(){do 1;} void method2(){do 2;} And in test: @Mock Service service; @Test public void testCallMethod1() { MyClass…
Anton
  • 1,409
  • 1
  • 19
  • 37
5
votes
1 answer

Mockito argument matcher with multiple arguments

I have a class with method: class URAction { public List getUrules(Cond cond, Cat cat) { ... } } I want to create its mock: @Mock URAction uraMock; @Test public void testSth() { Cond cond1; Cat cat1; …
user1539343
  • 1,569
  • 6
  • 28
  • 45
5
votes
2 answers

Missing method call for verify(mock), but there is one?

Introduction I'm attempting to make a test which verifies (using Mockito v1.9.5's verify) that a method with signature deinit() in an interface Bar is called after execution of a pass-through Foo.deinit(), and I'm hitting an error I really don't…
karobar
  • 1,250
  • 8
  • 30
  • 61
5
votes
1 answer

Mockito Replace Method

I have a Class: public class ProductComercialOrderDAO extends BaseDao implements ProductComercialOrderDAOLocal { private final static Logger log = UtilsBusiness.getLog4J(ProductComercialOrderDAO.class); public Session getSession()…
Ildelian
  • 1,278
  • 5
  • 15
  • 38
5
votes
1 answer

Mocking interface clone method

Mocking a clone() method on an interface appears to no longer be working with Mockito 2.1.0. The below code works fine with Mockito 1.10.19 but throws an IllegalAccessError with Mockito 2.1.0: public interface CloneableInterface extends Cloneable { …
jenglert
  • 1,589
  • 15
  • 23
5
votes
2 answers

Mock a method that returns a future to throw an exception

I'm using Java and Mockito to mock some methods for unit testing. I want to mock the producer in the below code so that I can test for the log messages that are sent when the exception is thrown. I tried mocking the future however I get the error…
annedroiid
  • 6,267
  • 11
  • 31
  • 54