44import org .junit .Before ;
55import org .junit .Test ;
66import org .modelmapper .ModelMapper ;
7+ import org .modelmapper .TypeMap ;
78import org .modelmapper .TypeToken ;
89
910import java .util .ArrayList ;
1213import static org .hamcrest .Matchers .equalTo ;
1314import static org .hamcrest .Matchers .hasItems ;
1415import static org .hamcrest .Matchers .hasProperty ;
15- import static org .hamcrest .collection .IsCollectionWithSize .hasSize ;
16- import static org .junit .Assert .assertNotNull ;
1716import static org .junit .Assert .assertThat ;
1817
1918
2019/**
2120 * This class has test methods of mapping Integer to Character list,
22- * mapping user list to DTO list using MapperUtil generic methods and Converter
21+ * mapping users list to DTO list using MapperUtil custom type method and property mapping using converter class
2322 *
2423 * @author Sasa Milenkovic
2524 */
@@ -32,7 +31,12 @@ public class UsersListMappingUnitTest {
3231 public void init () {
3332
3433 modelMapper = new ModelMapper ();
35- modelMapper .addMappings (new UserPropertyMap ());
34+
35+ TypeMap <UserList , UserListDTO > typeMap = modelMapper .createTypeMap (UserList .class , UserListDTO .class );
36+
37+ typeMap .addMappings (mapper -> mapper .using (new UsersListConverter ())
38+ .map (UserList ::getUsers , UserListDTO ::setUsernames ));
39+
3640 users = new ArrayList ();
3741 users .
add (
new User (
"b100" ,
"user1" ,
"[email protected] " ,
"111-222" ,
"USER" ));
3842 users .
add (
new User (
"b101" ,
"user2" ,
"[email protected] " ,
"111-333" ,
"USER" ));
@@ -41,7 +45,7 @@ public void init() {
4145 }
4246
4347 @ Test
44- public void whenMapIntegerToCharList () {
48+ public void whenInteger_thenMapToCharacter () {
4549
4650 List <Integer > integers = new ArrayList <Integer >();
4751
@@ -57,9 +61,9 @@ public void whenMapIntegerToCharList() {
5761 }
5862
5963 @ Test
60- public void givenUsersList_whenUseGenericType_thenMapToDto () {
64+ public void givenUsersList_whenUseGenericType_thenMapToUserDTO () {
6165
62- // Mapping lists using custom type methods
66+ // Mapping lists using custom (generic) type mapping
6367
6468 List <UserDTO > userDtoList = MapperUtil .mapList (users , UserDTO .class );
6569
@@ -68,16 +72,20 @@ public void givenUsersList_whenUseGenericType_thenMapToDto() {
6872 .
and (
hasProperty (
"email" ,
equalTo (
"[email protected] " )))
6973 .and (hasProperty ("username" , equalTo ("user1" )))));
7074
71- // Mapping lists using PropertyMap and Converter
75+
76+ }
77+
78+ @ Test
79+ public void givenUsersList_whenUseConverter_thenMapToUsernames () {
80+
81+ // Mapping lists using property mapping and converter
7282
7383 UserList userList = new UserList ();
7484 userList .setUsers (users );
7585 UserListDTO dtos = new UserListDTO ();
7686 modelMapper .map (userList , dtos );
7787
78- assertNotNull (dtos );
79- assertThat (dtos , Matchers .hasProperty ("usernames" ));
80- assertThat (dtos .getUsernames (), hasSize (3 ));
88+ assertThat (dtos .getUsernames (), hasItems ("user1" , "user2" , "user3" ));
8189
8290 }
8391
0 commit comments