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:
Thanks.
Writing a Unit Test for REST Controller First, we need to create Abstract class file used to create web application context by using MockMvc and define the mapToJson() and mapFromJson() methods to convert the Java object into JSON string and convert the JSON string into Java object.
Unit testing can be broadly classified into 2 categories. Quantitative Testing. Validate your Source and Target. a) Ensure that your connectors are configured properly. b) If you are using flat file make sure have enough read/write permission on the file share.
1) If you would try to unit test each single dto mapper in your app, that would be an overkill. In my experience only the most crucial DTO mappers are unit tested this way (and User
usually is one of the key ones).
2) I wouldn't use mocks for the Input user. The general rule is, if you do not need to mock an object in a unit test, simply don't. That is a POJO, has no external dependencies and complex logic. Try to incorporate the builder pattern for creating dtos in your unit tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With