MapStruct: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 1: | Line 1: | ||
{| | |||
| valign="top" | | |||
<source lang="java"> | |||
public class Car { | |||
private String make; | |||
private int numberOfSeats; | |||
private CarType type; | |||
//constructor, getters, setters etc. | |||
} | |||
</source> | |||
| valign="top" | | |||
<source lang="java"> | |||
public class CarDto { | |||
private String make; | |||
private int seatCount; | |||
private String type; | |||
//constructor, getters, setters etc. | |||
} | |||
</source> | |||
|- | |||
| valign="top" colspan="2" | | |||
<source lang="java"> | |||
@Mapper | |||
public interface CarMapper { | |||
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); | |||
@Mapping(source = "numberOfSeats", target = "seatCount") | |||
CarDto toDto(Car car); | |||
} | |||
</source> | |||
<source lang="java"> | |||
@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"); | |||
} | |||
</source> | |||
|} | |||
==References== | ==References== | ||
{| | {| |
Revision as of 09:33, 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");
}
|