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.class); ArgumentCaptor<Foo> secondFooCaptor = ArgumentCaptor.forClass(Foo.class); verify(mockBar).doSomething(firstFooCaptor.capture()); verify(mockBar).doSomething(secondFooCaptor.capture()); // then do some assertions on secondFooCaptor.getValue() But I get a TooManyActualInvocations Exception, as Mockito thinks that doSomething should only be … Read more

Difference between @Mock and @InjectMocks

What is the difference between @Mock and @InjectMocks in Mockito framework? 12 s 12 @Mock creates a mock. @InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock (or @Spy) annotations into this instance. Note you must use @RunWith(MockitoJUnitRunner.class) or Mockito.initMocks(this) to initialize these mocks and inject them … Read more

How to verify that a specific method was not called using Mockito?

How to verify that a method is not called on an object’s dependency? For example: public interface Dependency { void someMethod(); } public class Foo { public bar(final Dependency d) { … } } With the Foo test: public class FooTest { @Test public void dependencyIsNotCalled() { final Foo foo = new Foo(…); final Dependency … Read more

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? 10 s 10 You can create an in Mockito. Let’s assume, we have an interface named Application with a method myFunction. public interface Application { public String myFunction(String abc); } Here is the test method … Read more

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 succeed. My class looks like this: public class World { List<Listener> listeners; void addListener(Listener item) { listeners.add(item); } … Read more

Mocking static methods with Mockito

Use PowerMockito on top of Mockito. Example code: @RunWith(PowerMockRunner.class) @PrepareForTest(DriverManager.class) public class Mocker { @Test public void shouldVerifyParameters() throws Exception { //given PowerMockito.mockStatic(DriverManager.class); BDDMockito.given(DriverManager.getConnection(…)).willReturn(…); //when sut.execute(); // System Under Test (sut) //then PowerMockito.verifyStatic(); DriverManager.getConnection(…); } More information:

throw checked Exceptions from mocks with Mockito

Check the Java API for List.The get(int index) method is declared to throw only the IndexOutOfBoundException which extends RuntimeException.You are trying to tell Mockito to throw an exception SomeException() that is not valid to be thrown by that particular method call. To clarify further.The List interface does not provide for a checked Exception to be thrown from the get(int index) method and that is why Mockito is failing.When … Read more