Part 1 – What is Mockito?
In this tutorial, we will understand below topics –
– What is a Mock Object?
– When should I mock?
– Mockito Framework
– Enable Mockito Annotations
– How to Mock Object with @Mock & @InjectMock annotations in Spring application?
https://www.onlyfullstack.com/what-is-mock-object-what-is-mockito/
1. when/then
2. when/thenThrow
3. when/thenAnswer
https://www.onlyfullstack.com/how-to-mock-methods-with-mockito/
– Mocking Void Methods
– Three ways to mock the void method:
1. doNothing/when
2. doAnswer/when
3. doThrow/when
https://www.onlyfullstack.com/how-to-mock-void-methods-with-mockito/
Mockito Verify methods are used to check that certain behaviour happened. We can use Mockito verify methods at the end of the testing method code to make sure that specified methods are called.
Let’s look at some of the Mockito verify method variations and examples.
Simple verify method:
@Test public void saveCustomer_withValidCustomer_when_thenReturn() { Customer customer = new Customer(6, "QQQ", "Mumbai"); when(repository.saveCusomer(any())).thenReturn(true); Boolean save = customerService.saveCustomer(customer); assertThat(true, is(save)); verify(repository, times(1)).saveCusomer(eq(customer)); }
Below are the variations of verify method which we can use when we want any type of parameters or a specific type of parameters or exact parameter.
verify(repository).saveCusomer(any()); verify(repository).saveCusomer(any(Customer.class)); verify(repository).saveCusomer(ArgumentMatchers.any(Customer.class)); verify(repository).saveCusomer(eq(customer));
Verify with the number of times
verify(repository, times(1)). saveCusomer (); //same as normal verify method verify(repository, atLeastOnce()).saveCusomer (); // must be called at least once verify(repository, atMost(2)). saveCusomer(); // must be called at most 2 times verify(repository, atLeast(1)). saveCusomer(); // must be called at least once verify(repository, never()).getCusomer(); // must never be called
Mockito Verify Order of Invocation
We can use InOrder to verify the order of invocation. We can skip any method to verify, but the methods being verified must be invoked in the same order.
// A. Single mock whose methods must be invoked in a particular order List singleMock = mock(List.class); //using a single mock singleMock.add("was added first"); singleMock.add("was added second"); //create an inOrder verifier for a single mock InOrder inOrder = inOrder(singleMock); //following will make sure that add is first called with "was added first,
//then with "was added second" inOrder.verify(singleMock).add("was added first"); inOrder.verify(singleMock).add("was added second"); // B. Multiple mocks that must be used in a particular order List firstMock = mock(List.class); List secondMock = mock(List.class); //using mocks firstMock.add("was called first"); secondMock.add("was called second"); //create inOrder object passing any mocks that need to be verified in order InOrder inOrder = inOrder(firstMock, secondMock); //following will make sure that firstMock was called before secondMock inOrder.verify(firstMock).add("was called first"); inOrder.verify(secondMock).add("was called second"); // Oh, and A + B can be mixed together at will
Source Code
Download the source code of JUnit tutorial from below git repository :
unit-testing-and-integration-testing-with-spring-boot
Let’s go to our next tutorial where we will discuss below points :
1. Object declaration
2. When the methods are not mocked
3. When the method is mocked
https://www.onlyfullstack.com/mockito-mock-vs-spy/
Mockito Tutorial
https://www.onlyfullstack.com/mockito-tutorial/