使用mockito进行弹簧服务单元测试

时间:2012-01-23 22:55:16

标签: java spring junit mockito

直到现在,SO的答案对我的问题完全满意。我正在学习Junit和Mockito的单元测试,我想测试我的服务类,这是我的Spring网络应用程序的一部分。我阅读了很多教程和文章,但仍然有问题为我的服务层编写适当的单元测试。我想知道我的问题的答案,但首先我粘贴一些代码:

服务类

public class AccountServiceImpl implements AccountService {

@Autowired
AccountDao accountDao, RoleDao roleDao, PasswordEncoder passwordEncoder, SaltSource saltSource;

@PersistenceContext
EntityManager entityManager;

public Boolean registerNewAccount(Account newAccount) {
    entityManager.persist(newAccount);
    newAccount.setPassword(passwordEncoder.encodePassword(newAccount.getPassword(), saltSource.getSalt(newAccount)));
    setRoleToAccount("ROLE_REGISTERED", newAccount);

    return checkIfUsernameExists(newAccount.getUsername());    
}

public void setRoleToAccount(String roleName, Account account) {
    List<Role> roles = new ArrayList<Role>();
    try {
        roles.add(roleDao.findRole(roleName));
    } catch(RoleNotFoundException rnf) {
        logger.error(rnf.getMessage());
    }
    account.setRoles(roles);
}

public Boolean checkIfUsernameExists(String username) {
    try {
        loadUserByUsername(username);
    } catch(UsernameNotFoundException unf) {
        return false;
    }
    return true;
}

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {  
    try {
        Account loadedAccount = accountDao.findUsername(username);
        return loadedAccount;   
    } catch (UserNotFoundException e) {
        throw new UsernameNotFoundException("User: " + username + "not found!");
    }
}
}

我未完成的考试成绩

@RunWith(MockitoJUnitRunner.class)
public class AccountServiceImplTest {

private AccountServiceImpl accountServiceImpl;
@Mock private Account newAccount;
@Mock private PasswordEncoder passwordEncoder;
@Mock private SaltSource saltSource;
@Mock private EntityManager entityManager;
@Mock private AccountDao accountDao;
@Mock private RoleDao roleDao;

@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    accountServiceImpl = new AccountServiceImpl();
    ReflectionTestUtils.setField(accountServiceImpl, "entityManager", entityManager);
    ReflectionTestUtils.setField(accountServiceImpl, "passwordEncoder", passwordEncoder);
    ReflectionTestUtils.setField(accountServiceImpl, "saltSource", saltSource);
    ReflectionTestUtils.setField(accountServiceImpl, "accountDao", accountDao);
    ReflectionTestUtils.setField(accountServiceImpl, "roleDao", roleDao);
}

@Test
public void testRegisterNewAccount() {
    Boolean isAccountCreatedSuccessfully = accountServiceImpl.registerNewAccount(newAccount);

    verify(entityManager).persist(newAccount);
    verify(newAccount).setPassword(passwordEncoder.encodePassword(newAccount.getPassword(), saltSource.getSalt(newAccount)));
    assertTrue(isAccountCreatedSuccessfully);
}

@Test
public void testShouldSetRoleToAccount() throws RoleNotFoundException{
    Role role = new Role(); //Maybe I can use mock here?
    role.setName("ROLE_REGISTERED");
    when(roleDao.findRole("ROLE_REGISTERED")).thenReturn(role);
    accountServiceImpl.setRoleToAccount("ROLE_REGISTERED", newAccount);
    assertTrue(newAccount.getRoles().contains(role)); 
}

}

问题

  1. 在我的服务类中使用方法的方法中进行单元测试的最佳方法是什么?我可以像上面一样单独测试吗? [我将我的代码分成几个方法来清理代码]
  2. testRegisterNewAccount()对我的服务方法有好的单元测试吗?测试是绿色的,但我不确定。
  3. 我的testShouldSetRoleToAccount失败了。我做错了什么?
  4. 如何测试checkIfUsernameExists?
  5. 也许有人会帮我这个,因为我花了几天时间并没有取得进展:(


    更新

    完成测试类

    @RunWith(MockitoJUnitRunner.class)
    public class AccountServiceImplTest extends BaseTest {
    
    private AccountServiceImpl accountServiceImpl;
    private Role role;
    private Account account;
    @Mock private Account newAccount;
    @Mock private PasswordEncoder passwordEncoder;
    @Mock private SaltSource saltSource;
    @Mock private EntityManager entityManager;
    @Mock private AccountDao accountDao;
    @Mock private RoleDao roleDao;
    
    @Before
    public void init() {
        accountServiceImpl = new AccountServiceImpl();
        role = new Role();
        account = new Account();
        ReflectionTestUtils.setField(accountServiceImpl, "entityManager", entityManager);
        ReflectionTestUtils.setField(accountServiceImpl, "passwordEncoder", passwordEncoder);
        ReflectionTestUtils.setField(accountServiceImpl, "saltSource", saltSource);
        ReflectionTestUtils.setField(accountServiceImpl, "accountDao", accountDao);
        ReflectionTestUtils.setField(accountServiceImpl, "roleDao", roleDao);
    }
    
    @Test
    public void testShouldRegisterNewAccount() {
        Boolean isAccountCreatedSuccessfully = accountServiceImpl.registerNewAccount(newAccount);
    
        verify(entityManager).persist(newAccount);
        verify(newAccount).setPassword(passwordEncoder.encodePassword(newAccount.getPassword(), saltSource.getSalt(newAccount)));
        assertTrue(isAccountCreatedSuccessfully);
    }
    
    @Test(expected = IllegalArgumentException.class)
    public void testShouldNotRegisterNewAccount() {
        doThrow(new IllegalArgumentException()).when(entityManager).persist(account);
        accountServiceImpl.registerNewAccount(account);
    }
    
    @Test
    public void testShouldSetRoleToAccount() throws RoleNotFoundException {
        when(roleDao.findRole(anyString())).thenReturn(role);
        accountServiceImpl.setRoleToAccount("ROLE_REGISTERED", account);
        assertTrue(account.getRoles().contains(role)); 
    }
    
    @Test
    public void testShouldNotSetRoleToAccount() throws RoleNotFoundException {
        when(roleDao.findRole(anyString())).thenThrow(new RoleNotFoundException());
        accountServiceImpl.setRoleToAccount("ROLE_RANDOM", account);
        assertFalse(account.getRoles().contains(role));
    }
    
    @Test
    public void testCheckIfUsernameExistsIsTrue() throws UserNotFoundException {
        when(accountDao.findUsername(anyString())).thenReturn(account);
        Boolean userExists = accountServiceImpl.checkIfUsernameExists(anyString());
        assertTrue(userExists);
    }
    
    @Test
    public void testCheckIfUsernameExistsIsFalse() throws UserNotFoundException {
        when(accountDao.findUsername(anyString())).thenThrow(new UserNotFoundException());
        Boolean userExists = accountServiceImpl.checkIfUsernameExists(anyString());
        assertFalse(userExists);
    }
    
    @Test 
    public void testShouldLoadUserByUsername() throws UserNotFoundException {
        when(accountDao.findUsername(anyString())).thenReturn(account);
        Account foundAccount = (Account) accountServiceImpl.loadUserByUsername(anyString());
        assertEquals(account, foundAccount);
    }
    
    @Test(expected = UsernameNotFoundException.class)
    public void testShouldNotLoadUserByUsername() throws UserNotFoundException {
        when(accountDao.findUsername(anyString())).thenThrow(new UsernameNotFoundException(null));
        accountServiceImpl.loadUserByUsername(anyString());
    }
    
    }
    

1 个答案:

答案 0 :(得分:5)

问题1 - 你在这里有几个选择。

选项1 - 根据该行为所需的内容,为每个公共方法的每个行为编写单独的测试。这使每个测试保持清洁和分离,但它确实意味着辅助方法中的逻辑(例如checkIfUsernameExists)将被执行两次。从某种意义上说,这是不必要的重复,但这个选项的一个优点是,如果你改变实现,而不是所需的行为,你仍然会根据行为进行良好的测试。

选项2 - 使用Mockito间谍。这有点像模拟,除了你从一个真实对象创建它,它的默认行为是所有方法像往常一样运行。然后,您可以存根并验证辅助方法,以便测试调用它们的方法。

问题2 - 这似乎是registerNewAccount的“成功”案例的一个很好的测试。请考虑一下导致registerNewAccount失败并返回false的情况;并测试这个案例。

问题3 - 我没看好这个;但尝试使用调试器运行,并找出您的对象与您的期望不同的点。如果你无法解决问题,请再次发布,我会再看看。

问题4 - 要测试否定的情况,请存储AccountDao的模拟以抛出所需的异常。否则,请参阅我对问题1的回答。