コード例 #1
0
  @Test
  public void testZeroInteractionsWithMock() throws Exception {

    User user = new User("user1", new Date());

    // call method where no call to userService will be made
    userManager.getUserLastLogin(user);
    Mockito.verifyZeroInteractions(userService);

    // Another way to check zero interactions
    userManager.getUserLastLogin(user);
    Mockito.verify(userService, Mockito.never());
  }
コード例 #2
0
  @Test
  public void testSaveUser() throws Exception {
    User user = new User("u1");
    userManager.saveUser(user);

    // Verify if saveUser was invoked on userService with given 'user' object.
    Mockito.verify(userService).saveUser(user);

    // Verify with Argument Matcher
    Mockito.verify(userService).saveUser(Mockito.<User>any());
  }
コード例 #3
0
  @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);
  }
コード例 #4
0
  @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());
  }
コード例 #5
0
 @Before
 public void setUp() throws Exception {
   userService = Mockito.mock(UserService.class);
   userManager = new UserManager();
   userManager.setUserService(userService);
 }