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

How do I use Mockito to test that a Java 8 Stream had the expected values?

One of the interactions I want to test is that a class Foo is supposed to pass a Stream to FooListener.someChangesHappened. Is there a Mockito idiom to verify that a stream contained the expected objects?
Doradus
  • 938
  • 1
  • 9
  • 15
5
votes
2 answers

Mockito/Power Mockito : unable to get expected output when mocking method of LayoutParams in android

I have a method: public class MarginConverter { int top = 0; int bottom = 0; int right = 0; int left = 0; public MarginConverter(String val){ top = bottom = right = left = Integer.parseInt(val); } public…
Anuja Kothekar
  • 2,537
  • 2
  • 15
  • 28
5
votes
1 answer

Kotlin lazy block not executed when using Mockito and InjectMocks

I'm using Mockito to test my Kotlin code. It's a web app and I use spring to inject values into some fields. For example, my class snippet looks something like this: class MyComponent { @Inject private lateinit var request: HttpServletRequest …
Jumwah
  • 517
  • 5
  • 19
5
votes
1 answer

Injection an object though InjectMocks Spy

I need to run series of unit tests over a class, which has a @Autowired Logger implementation. The base idea of realization was: @Mock Logger logger; @InjectMocks TestedClass tested; but i want to save the logging output functionality. Does…
Draaksward
  • 759
  • 7
  • 32
5
votes
1 answer

Mockito verify(...) fails - "Actually, there were zero interactions with this mock." in more than one test run sequentially

I have a Wrapper class that causes that instead of equals method, the method equalsWithoutId is called on wrapped object. Implementation here: import org.apache.commons.lang3.Validate; public class IdAgnosticWrapper { private final IdAgnostic…
Michal Krasny
  • 5,434
  • 7
  • 36
  • 64
5
votes
0 answers

Testing a fragment that requires the parent activity to implement a listener

I'm trying to test a fragment using Robolectric. The fragment contains the code: public class MyDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Verify that…
clay_to_n
  • 583
  • 3
  • 15
5
votes
2 answers

Mockito - Unable to initialize Spy on HttpEntity

The code I'm testing works correctly, logs are right. Tests in error: ConnectorTest: Unable to initialize @Spy annotated field 'entity'. Why can't I use verify on the http entity? Is there a better way to test it? Should I spy on the logs…
Gabe
  • 5,997
  • 5
  • 46
  • 92
5
votes
1 answer

How to inject a bean in a ConstraintValidator with MockMVC?

I have a custom ConstraintValidator: @Component public class FooValidator implements ConstraintValidator { @Inject private FooRepository fooRepository; @Override public void initialize(FooAnnotation foo) { …
oscar
  • 1,636
  • 6
  • 31
  • 59
5
votes
2 answers

How to Mock an @Inject Interface

I have this Service Interface public interface ABC { public String getAbc(); } And I have this controller that uses this Service public class Do { @Inject ABC abc; public String doAbc() { String a = abc.getAbc(); } } In my…
Churk
  • 4,556
  • 5
  • 22
  • 37
5
votes
2 answers

pass remoteUser value in HttpServletRequest to mockmvc perform test

I have an api call as: @RequestMapping(value = "/course", method = RequestMethod.GET) ResponseEntity getCourse(HttpServletRequest request, HttpServletResponse response) throwsException { User user =…
Agnosco
  • 155
  • 2
  • 11
5
votes
0 answers

Spring MVC controller JSON date serialization during testing

I'm writing unit tests for Spring MVC 4.2.x REST controllers and have an issue with dates serialized by Jackson (2.6.x). While running the application the dates (java.util.Date) are serialized in the format yyyy-MM-dd by default (no extra…
yktoo
  • 2,696
  • 1
  • 23
  • 35
5
votes
1 answer

Run jUnit tests using @category spring and gradle

I need to run some integration tests and some unit tests and I am using spring, gradle and jUnit 4. I used jUnit categories @Category(UnitTestCategory.class) and @Category(IntegrationTestCategory.class) and i added test { useJUnit { …
MyUserQuestion
  • 295
  • 1
  • 9
5
votes
0 answers

Android Unit Test with Retrofit/RxJava/Roboletric and Mockito

I'm trying to write a Unit test with Robolectric and Mockito. I've a retrofit observable and I would like to test with a mock response. I've tried something like that link here but I got a java.lang.NullPointerException. Here a sample of my code: …
fGab
  • 59
  • 4
5
votes
1 answer

Verify that method is called in onNext of RxJava Subscriber

I have the following method that uses a Retrofit service interface to fetch some data from an API, then interacts with a view interface. @Override @VisibleForTesting public void fetchPhotos(@Nullable PhotosService service, @Nullable Scheduler…
fweigl
  • 21,278
  • 20
  • 114
  • 205
5
votes
1 answer

Android Mockito how to mock a resource

I am using robolectric as my test runner with Mockito. I would like to know in Android how do i mock a resource. I have a array resource but in my test i'd like to mock the value. i have an array that looks like this:
j2emanue
  • 60,549
  • 65
  • 286
  • 456
1 2 3
99
100