MapStruct: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Line 54: Line 54:
| valign="top" |
| valign="top" |
* [https://www.baeldung.com/java-mapstruct-mapping-collections Mapping Collections with MapStruct]
* [https://www.baeldung.com/java-mapstruct-mapping-collections Mapping Collections with MapStruct]
* [https://plugins.jetbrains.com/plugin/10036-mapstruct-support IntelliJ IDEA MapStruct Support]
* [https://www.baeldung.com/mapstruct Quick Guide to MapStruct]
* [https://www.baeldung.com/mapstruct Quick Guide to MapStruct]
* [https://mapstruct.org/ MapStruct]
* [https://mapstruct.org/ MapStruct]

Revision as of 09:40, 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