Пример #1
0
  @Test
  public void testAddAndRemoveUser() throws Exception {
    // given
    User user = new User();

    // call populate method in super class to populate test data
    // from a properties file matching this class name
    user = (User) populate(user);

    Role role = new Role(Constants.USER_ROLE);
    user.addRole(role);

    final User user1 = user;
    given(userDao.saveUser(user1)).willReturn(user1);

    // when
    user = userManager.saveUser(user);

    // then
    assertTrue(user.getUsername().equals("john"));
    assertTrue(user.getRoles().size() == 1);

    // given
    willDoNothing().given(userDao).remove(5L);
    userManager.removeUser("5");

    given(userDao.get(5L)).willReturn(null);

    // when
    user = userManager.getUser("5");

    // then
    assertNull(user);
    verify(userDao).remove(5L);
  }
  @Test
  public void testAddAndRemoveUser() throws Exception {
    User user = new User();

    // call populate method in super class to populate test data
    // from a properties file matching this class name
    user = (User) populate(user);

    // set expected behavior on role dao
    context.checking(
        new Expectations() {
          {
            one(roleDao).getRoleByName(with(equal("ROLE_USER")));
            will(returnValue(new Role("ROLE_USER")));
          }
        });

    Role role = roleManager.getRole(Constants.USER_ROLE);
    user.addRole(role);

    // set expected behavior on user dao
    final User user1 = user;
    context.checking(
        new Expectations() {
          {
            one(userDao).saveUser(with(same(user1)));
            will(returnValue(user1));
          }
        });

    user = userManager.saveUser(user);
    assertTrue(user.getUsername().equals("john"));
    assertTrue(user.getRoles().size() == 1);

    context.checking(
        new Expectations() {
          {
            one(userDao).remove(with(equal(5L)));
          }
        });

    userManager.removeUser("5");

    context.checking(
        new Expectations() {
          {
            one(userDao).get(with(equal(5L)));
            will(returnValue(null));
          }
        });

    user = userManager.getUser("5");
    assertNull(user);
  }