MapStruct: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
Line 10: Line 10:
}
}
</source>
</source>
| valign="top" |
| valign="top" |
<source lang="java">
<source lang="java">
Line 21: Line 20:
}
}
</source>
</source>
|-
|-
| valign="top" colspan="2" |
| valign="top" colspan="2" |
Line 33: Line 31:
}
}
</source>
</source>
 
|-
| valign="top" colspan="2" |
<source lang="java">
<source lang="java">
@Test
@Test
Line 47: Line 46:
}
}
</source>
</source>
|}
|}



Revision as of 09:45, 21 September 2021

public class Car {
    private String make;
    private int numberOfSeats;
    private CarType type;
 
    //constructor, getters, setters etc.
}
public class CarDto {
    private String make;
    private int seatCount;
    private String type;
 
    //constructor, getters, setters etc.
}
@Mapper
public interface CarMapper {
    CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);
 
    @Mapping(source = "numberOfSeats", target = "seatCount")
    CarDto toDto(Car car);
}
@Test
public void shouldMapCarToDto() {
    Car car = new Car("Morris", 5, CarType.SEDAN);
    CarDto dto = CarMapper.INSTANCE.toDto(car);
 
    //then
    assertThat(dto).isNotNull();
    assertThat(dto.getMake()).isEqualTo("Morris");
    assertThat(dto.getSeatCount()).isEqualTo(5);
    assertThat(dto.getType()).isEqualTo("SEDAN");
}

References