我有一个Mapper类,其中一种方法可以将一种类型的用户转换为另一种类型的用户:
/**
* Transform new {@link User} to legacy {@link com.xxx.xxx.bo.bean.User}.
* @param user User to transform.
* @return Transformed user.
* @throws MappingException If an error occurred when transforming.
*/
public com.xxx.xxx.bo.bean.User transform(User user)
throws MappingException {
try {
com.xxx.xxx.bo.bean.User legacyUser =
new com.xxx.xxx.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.xxx.xxx.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);
}
问题是这种方法太冗长了,我的问题是:
- 这是在映射器类中对转换方法进行单元测试的正确方法吗?还有其他更优雅的方式来实现这一目标吗?
- 我有一个同事说,由于这两个模型是 POJO,并且只包含 sets(( 和 gets((,因此验证每个成员是没有意义的,相反,她只会测试最终转换的用户是否为空。
谢谢。
1(如果你尝试对应用程序中的每个dto映射器进行单元测试,那将是矫枉过正。根据我的经验,只有最关键的DTO映射器才会以这种方式进行单元测试(User
通常是关键之一(。
2(我不会对输入用户使用模拟。一般规则是,如果你不需要在单元测试中模拟一个对象,那就不需要。这是一个POJO,没有外部依赖和复杂的逻辑。尝试在单元测试中合并用于创建 dto 的生成器模式。