我有一个Mapper类,其中有一个方法可以将一种类型的用户转换为另一种类型的用户:
/**
* Transform new {@link User} to legacy {@link com.whisbi.whisbiforstores.bo.bean.User}.
* @param user User to transform.
* @return Transformed user.
* @throws MappingException If an error occurred when transforming.
*/
public com.whisbi.whisbiforstores.bo.bean.User transform(User user)
throws MappingException {
try {
com.whisbi.whisbiforstores.bo.bean.User legacyUser =
new com.whisbi.whisbiforstores.bo.bean.User();
legacyUser.setIdUser(user.getIdUser());
legacyUser.setIdUserType(user.getIdUserType());
legacyUser.setName(user.getName());
legacyUser.setSurname(user.getSurname());
legacyUser.setEmail(user.getEmail());
legacyUser.setUserAccount(user.getUserAccount());
legacyUser.setPassword(user.getPassword());
legacyUser.setUserType(user.getUserType());
legacyUser.setIsOnlyAgent(user.isIsOnlyAgent());
legacyUser.setAgentId(user.getAgentId());
legacyUser.setIdVaAgent(user.getIdVaAgent());
legacyUser.setDesktopUrl(user.getDesktopUrl());
return legacyUser;
} catch (Exception e){
throw new MappingException("Error when mapping User to legacy User: "
+ e.getMessage());
}
}
我已经编写了一个测试来确保映射成功:
@Test
public void GivenUser_WhenTransformToLegacyUser_LegacyUserIsReturned()
throws Exception {
final UUID userId = UUID.randomUUID();
final UUID userTypeId = UUID.randomUUID();
final String name = "name";
final String surname = "surname";
final String email = "email";
final String userAccount = "user_account";
final String password = "password";
final String userType = "user_type";
final boolean isOnlyAgent = getRandomBoolean();
final UUID agentId = UUID.randomUUID();
final UUID vaAgentId = UUID.randomUUID();
final String desktopUrl = "desktop_url";
final User user = mock(User.class);
when(user.getIdUser()).thenReturn(userId);
when(user.getIdUserType()).thenReturn(userTypeId);
when(user.getName()).thenReturn(name);
when(user.getSurname()).thenReturn(surname);
when(user.getEmail()).thenReturn(email);
when(user.getUserAccount()).thenReturn(userAccount);
when(user.getPassword()).thenReturn(password);
when(user.getUserType()).thenReturn(userType);
when(user.isIsOnlyAgent()).thenReturn(isOnlyAgent);
when(user.getAgentId()).thenReturn(agentId);
when(user.getIdVaAgent()).thenReturn(vaAgentId);
when(user.getDesktopUrl()).thenReturn(desktopUrl);
com.whisbi.whisbiforstores.bo.bean.User legacyUser =
mMapper.transform(user);
assertEquals(legacyUser.getIdUser(), userId);
assertEquals(legacyUser.getIdUserType(), userTypeId);
assertEquals(legacyUser.getName(), name);
assertEquals(legacyUser.getSurname(), surname);
assertEquals(legacyUser.getEmail(), email);
assertEquals(legacyUser.getUserAccount(), userAccount);
assertEquals(legacyUser.getPassword(), password);
assertEquals(legacyUser.getUserType(), userType);
assertEquals(legacyUser.isIsOnlyAgent(), isOnlyAgent);
assertEquals(legacyUser.getAgentId(), agentId);
assertEquals(legacyUser.getIdVaAgent(), vaAgentId);
assertEquals(legacyUser.getDesktopUrl(), desktopUrl);
}
问题是这个方法太冗长了,我的问题是:
感谢。
答案 0 :(得分:2)
1)如果您尝试对应用中的每个dto映射器进行单元测试,那就太过分了。根据我的经验,只有最重要的DTO映射器才能以这种方式进行单元测试(User
通常是关键测试之一。)
2)我不会对输入用户使用模拟。一般规则是,如果您不需要在单元测试中模拟对象,那么就不要这样做。这是一个POJO,没有外部依赖和复杂的逻辑。尝试合并构建器模式以在单元测试中创建dtos。