Mockito: Trying to spy on method is calling the original method

Let me quote the official documentation: Important gotcha on spying real objects! Sometimes it’s impossible to use when(Object) for stubbing spies. Example: List list = new LinkedList(); List spy = spy(list); // Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty) when(spy.get(0)).thenReturn(“foo”); // You have to use doReturn() for stubbing … 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: Why doesn’t Mockito mock static methods?

Use Mockito to mock some methods but not others

To directly answer your question, yes, you can mock some methods without mocking others. This is called a partial mock. See the Mockito documentation on partial mocks for more information. For your example, you can do something like the following, in your test: Stock stock = mock(Stock.class); when(stock.getPrice()).thenReturn(100.00); // Mock implementation when(stock.getQuantity()).thenReturn(200); // Mock implementation … Read more

Can Mockito capture arguments of a method called multiple times?

I think it should be verify(mockBar, times(2)).doSomething(…) Sample from mockito javadoc: ArgumentCaptor<Person> peopleCaptor = ArgumentCaptor.forClass(Person.class); verify(mock, times(2)).doSomething(peopleCaptor.capture()); List<Person> capturedPeople = peopleCaptor.getAllValues(); assertEquals(“John”, capturedPeople.get(0).getName()); assertEquals(“Jane”, capturedPeople.get(1).getName());

Difference between @Mock and @InjectMocks

@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 (JUnit 4). With JUnit 5, you must use @ExtendWith(MockitoExtension.class). @RunWith(MockitoJUnitRunner.class) // JUnit 4 // … Read more

Making a mocked method return an argument that was passed to it

Since Mockito 1.9.5+ and Java 8+ You can use a lambda expression, like: when(myMock.myFunction(anyString())).thenAnswer(i -> i.getArguments()[0]); Where i is an instance of InvocationOnMock. For older versions You can create an Answer in Mockito. Let’s assume, we have an interface named MyInterface with a method myFunction. public interface MyInterface { public String myFunction(String abc); } Here … Read more

How to mock void methods with Mockito

Take a look at the Mockito API docs. As the linked document mentions (Point # 12) you can use any of the doThrow(),doAnswer(),doNothing(),doReturn() family of methods from Mockito framework to mock void methods. For example, Mockito.doThrow(new Exception()).when(instance).methodName(); or if you want to combine it with follow-up behavior, Mockito.doThrow(new Exception()).doNothing().when(instance).methodName(); Presuming that you are looking at … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)