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

define mockito when with multiple any arguments

I am trying to define when mockito method with multiple any arguments: TestBedDaoClient testBedDaoClient = mock(TestBedDaoClient.class); when(testBedDaoClient.addTestBed(anyString(), anyString(),…
Shashi Ranjan
  • 1,491
  • 6
  • 27
  • 52
5
votes
2 answers

Unit testing while using Dagger 2 (Robolectric and Mockito)

I'm trying to write some tests for fragments which have fields annotated with @Inject. For example, a chunk of my app looks like this: Module: @Module public class PdfFactoryModule { @Provides @Singleton PdfFactory providePdfFactory() { …
Haruspik
  • 412
  • 5
  • 17
5
votes
3 answers

A simple kotlin class with mockito test caused MissingMethodInvocationException

I start to learn Kotlin and Mockito, so I code a simple module to test it. AccountData_K.kt: open class AccountData_K { var isLogin: Boolean = false var userName: String? = null fun changeLogin() : Boolean { return !isLogin …
David_Jiang
  • 73
  • 1
  • 5
5
votes
2 answers

Mocking/stubbing private variables of a class without getter and setter methods

I recently started working on TDD for exisitng project and faced couple of issues, one of those is mentioned below I have a private variable to be mocked in test class and the variable looks like below private Class cls = XYZ.class; later this…
Praveen Kumar Mekala
  • 628
  • 1
  • 10
  • 26
5
votes
1 answer

How to verify a non-mocked method was called?

I want to test that my method calls another method in the same class that I cannot mock. Example: public void methodToTest(){ //other stuff to test that can be mocked someClassICanMock.doSomething(); //method within same class that cannot be…
java123999
  • 6,974
  • 36
  • 77
  • 121
5
votes
1 answer

Mockito calls real method from superclass when stubbing

I am getting problem with Mockito trying to call real method while stubbing. I assume this might be related to that method is inherited. This is component external to my system and can't do really much about it. But let's get to the…
Piotr
  • 671
  • 6
  • 17
5
votes
2 answers

How do we unit test this lambda expression?

We have a REST end point (JAX-RS) that is invoked from the browser. We are passing around OutputStream so that we could have the browser display the result of the call. Here is the method. @Path("/mypath/{userId}") @POST public Response…
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
5
votes
4 answers

Mock inherited method in Mockito Java

My class structure is as follows: public class MyParentClass { void doSomethingParent() { System.out.println("something in parent"); } } public class MyClass extends MyParentClass { protected String createDummyRequest(Holder…
Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62
5
votes
1 answer

PowerMockito is calling the method when I use doReturn(..).when(....)

I'm new to PowerMockito and there's a behavior it's displaying which I don't understand. The following code explains my issue: public class ClassOfInterest { private Object methodIWantToMock(String x) { String y = x.trim(); //Do some…
Ahmad
  • 12,886
  • 30
  • 93
  • 146
5
votes
2 answers

How to mock sharedpreferences for android instrumentation tests?

I have a preference util class to store and retrieve the data in shared preferences in a single place. Prefutils.java: public class PrefUtils { private static final String PREF_ORGANIZATION = "organization"; private static SharedPreferences…
5
votes
1 answer

Kotlin Anonymous Function Parameter Unit Testing

As per Kotlin Unit Testing for Function Parameter and Object, we could test the function variable funcParam, as it is an object function variable. However if code is written using anonymous/inlining function parameter (which is a very nice Kotlin…
Elye
  • 53,639
  • 54
  • 212
  • 474
5
votes
1 answer

How to stub overloaded method in Mockito using Groovy?

Groovy appears to be messing up my stubbing. The following test passes: MockitoStubTest2.java: public class MockitoStubTest2 { @Test public void testStubbing() { MyInterface myInterface = mock(MyInterface.class); …
tytk
  • 2,082
  • 3
  • 27
  • 39
5
votes
2 answers

Run JUnit unit tests in Spring Boot application without providing datasource

I am attempting to build a Spring Boot application, trying to stick to test driven development. My issue is that I have Spring Boot JPA included in my project but don't actually have a data source set up yet. Before adding the dependency, I was able…
c.dunlap
  • 600
  • 1
  • 11
  • 28
5
votes
1 answer

Replacing ManagedExecutorService with ExecutorService in test

I have some service which uses an Java EE ManagedExecutorService (in Wildfly 9) public class FooService{ @Resource ManagedExecutorService executorService; } For a test with Mockito, I would like to use a "normal"…
user140547
  • 7,750
  • 3
  • 28
  • 80
5
votes
1 answer

Verify call to static method

I want to verify that a public static void method has been called. @RunWith(PowerMockRunner.class) @PrepareForTest({ConsoleLog.class}) public class AdContentDataUnitTest { @Before public void setUp() throws Exception { …
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
1 2 3
99
100