@Test public void testCountNumberOfInteractions() throws Exception { userManager.findUser("user1"); // Verify the number of interactions with mock Mockito.verify(userService, Mockito.times(1)).findUserByName("user1"); // There was only one interaction with userService Mockito.verifyNoMoreInteractions(userService); }
@Test public void testFindUser() throws Exception { // Stub the value that will returned on call to userService.findUserByName User stubbedUser = new User("userAfterSave"); Mockito.when(userService.findUserByName("user1")).thenReturn(stubbedUser); // make the call User user = userManager.findUser("user1"); // Verify if findUserByName method was invoked on userService call Mockito.verify(userService).findUserByName("user1"); Assert.assertEquals("userAfterSave", user.getUserName()); }