13,916 questions
1237
votes
12
answers
1.3m
views
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 ...
962
votes
10
answers
602k
views
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?
654
votes
5
answers
757k
views
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 ...
611
votes
6
answers
429k
views
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<Foo> firstFooCaptor = ArgumentCaptor.forClass(Foo....
529
votes
14
answers
536k
views
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 ...
510
votes
9
answers
1.0m
views
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 =...
415
votes
12
answers
333k
views
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<T> which I need to pass into a method that expects a Foo<Bar>. I can do the following ...
397
votes
29
answers
646k
views
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 ...
368
votes
4
answers
328k
views
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()))....
348
votes
12
answers
300k
views
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 ...
320
votes
5
answers
361k
views
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 ...
305
votes
6
answers
186k
views
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(){}...
303
votes
7
answers
263k
views
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 ...
289
votes
5
answers
214k
views
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 ...
284
votes
4
answers
188k
views
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()
...
265
votes
9
answers
299k
views
Unfinished Stubbing Detected in Mockito
I am getting following exception while running the tests. I am using Mockito for mocking. The hints mentioned by Mockito library are not helping.
org.mockito.exceptions.misusing....
257
votes
16
answers
510k
views
How to resolve Unneccessary Stubbing exception
My Code is as below,
@RunWith(MockitoJUnitRunner.class)
public class MyClass {
private static final String code ="Test";
@Mock
private MyClassDAO dao;
@InjectMocks
private ...
233
votes
5
answers
269k
views
How to use Mockito with JUnit 5?
How can I use injection with Mockito and JUnit 5?
In JUnit 4, I can just use the @RunWith(MockitoJUnitRunner.class) annotation.
In JUnit 5, there is no @RunWith Annotation.
229
votes
6
answers
171k
views
Simulate first call fails, second call succeeds
I want to use Mockito to test the (simplified) code below. I don't know how to tell Mockito to fail the first time, then succeed the second time.
for(int i = 1; i < 3; i++) {
String ret = myMock....
213
votes
6
answers
70k
views
When to use Mockito.verify()?
I write jUnit test cases for 3 purposes:
To ensure that my code satisfies all of the required functionality, under all (or most of) the input combinations/values.
To ensure that I can change the ...
210
votes
4
answers
224k
views
Mockito: List Matchers with generics
Mockito offers:
when(mock.process(Matchers.any(List.class)));
How to avoid warning if process takes a List<Bar> instead?
207
votes
9
answers
228k
views
Mockito - @Spy vs @Mock [duplicate]
I understand a spy calls the real methods on an object, while a mock calls methods on the double object. Also spies are to be avoided unless there is a code smell.
However, how do spies work and when ...
199
votes
1
answer
324k
views
Java verify void method calls n times with Mockito
I'm trying to verify that a (void) method is being called inside of a DAO - I'm using a commit point that sends a list of results up to that point, resets the list and continues.
Say I have 4 things ...
197
votes
4
answers
259k
views
Mockito : doAnswer Vs thenReturn
I am using Mockito for service later unit testing. I am confused when to use doAnswer vs thenReturn.
Can anyone help me in detail? So far, I have tried it with thenReturn.
192
votes
16
answers
709k
views
Mockito How to mock and assert a thrown exception?
I'm using mockito in a junit test. How do you make an exception happen and then assert that it has (generic pseudo-code)