I have a Mapper class with one method which transforms one type of User to another type of User:
/**
* 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());
}
}
I have written a test to ensure that the mapping is made successfully:
@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);
}
The problem is that this method is too verbose, my questions are:
- Is this the proper way to unit test a transformation method in a Mapper class? Is there any other more elegant way to achive this?
- I have a coleague who says that as this two models are POJOs and only contains sets() and gets(), it has no sense to verify each individual member, instead she would only test if the final transformed User is not null.
Thanks.