Skip to main content
Filter by
Sorted by
Tagged with
1237 votes
12 answers
1.3m views

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 ...
ibrahimyilmaz's user avatar
962 votes
10 answers
602k views

Consider a method signature like: public String myFunction(String abc); Can Mockito help return the same string that the method received?
Abhijeet Kashnia's user avatar
654 votes
5 answers
757k views

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 ...
Victor Grazi's user avatar
  • 16.6k
611 votes
6 answers
429k views

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....
Eric Wilson's user avatar
  • 59.7k
529 votes
14 answers
536k views

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 ...
Emma's user avatar
  • 6,652
510 votes
9 answers
1.0m views

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 =...
mre's user avatar
  • 44.4k
415 votes
12 answers
333k views

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 ...
Tim Clemons's user avatar
  • 6,541
397 votes
29 answers
646k views

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 ...
buttowski's user avatar
  • 4,937
368 votes
4 answers
328k views

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()))....
Eric Wilson's user avatar
  • 59.7k
348 votes
12 answers
300k views

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 ...
Priyank's user avatar
  • 14.4k
320 votes
5 answers
361k views

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 ...
DigitalZebra's user avatar
  • 41.9k
305 votes
6 answers
186k views

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(){}...
froi's user avatar
  • 7,908
303 votes
7 answers
263k views

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 ...
Abidi's user avatar
  • 8,016
289 votes
5 answers
214k views

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 ...
user2286693's user avatar
  • 3,207
284 votes
4 answers
188k views

When creating tests and mocking dependencies, what is the difference between these three approaches? @MockBean: @MockBean MyService myservice; @Mock: @Mock MyService myservice; Mockito.mock() ...
Doug's user avatar
  • 6,535
265 votes
9 answers
299k views

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....
Royal Rose's user avatar
  • 2,761
257 votes
16 answers
510k views

My Code is as below, @RunWith(MockitoJUnitRunner.class) public class MyClass { private static final String code ="Test"; @Mock private MyClassDAO dao; @InjectMocks private ...
VHS's user avatar
  • 2,579
233 votes
5 answers
269k views

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.
Daniel Käfer's user avatar
229 votes
6 answers
171k views

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....
jb.'s user avatar
  • 10.4k
213 votes
6 answers
70k views

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 ...
Russell's user avatar
  • 2,131
210 votes
4 answers
224k views

Mockito offers: when(mock.process(Matchers.any(List.class))); How to avoid warning if process takes a List<Bar> instead?
Philippe Blayo's user avatar
207 votes
9 answers
228k views

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 ...
Abhinav's user avatar
  • 2,141
199 votes
1 answer
324k views

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 ...
nbpeth's user avatar
  • 3,158
197 votes
4 answers
259k views

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.
Rajkumar Thambu's user avatar
192 votes
16 answers
709k views

I'm using mockito in a junit test. How do you make an exception happen and then assert that it has (generic pseudo-code)
stackoverflow's user avatar

1
2 3 4 5
279