BAEL-6216 Use Mapper in Another Mapper with Mapstruct and Java (#13802)

This commit is contained in:
mdabrowski-eu 2023-04-10 07:59:12 +02:00 committed by GitHub
parent 4272773056
commit 8b688e4e40
5 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.dto;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class ArticleDTO {
private int id;
private String name;
private PersonDTO author;
}

View File

@ -0,0 +1,12 @@
package com.baeldung.entity;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Article {
private int id;
private String name;
private Person author;
}

View File

@ -0,0 +1,20 @@
package com.baeldung.mapper;
import com.baeldung.dto.ArticleDTO;
import com.baeldung.dto.PersonDTO;
import com.baeldung.entity.Article;
import com.baeldung.entity.Person;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface ArticleMapper {
ArticleMapper INSTANCE = Mappers.getMapper(ArticleMapper.class);
ArticleDTO articleToArticleDto(Article article);
default PersonDTO personToPersonDto(Person person) {
return Mappers.getMapper(PersonMapper.class).personToPersonDTO(person);
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.mapper;
import com.baeldung.dto.ArticleDTO;
import com.baeldung.dto.PersonDTO;
import com.baeldung.entity.Article;
import com.baeldung.entity.Person;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper(uses = PersonMapper.class)
public interface ArticleUsingPersonMapper {
ArticleUsingPersonMapper INSTANCE = Mappers.getMapper(ArticleUsingPersonMapper.class);
ArticleDTO articleToArticleDto(Article article);
}

View File

@ -0,0 +1,49 @@
package com.baeldung.mapper;
import com.baeldung.dto.ArticleDTO;
import com.baeldung.dto.CarDTO;
import com.baeldung.entity.Article;
import com.baeldung.entity.Car;
import com.baeldung.entity.Person;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ArticleMapperUnitTest {
@Test
public void givenArticle_whenMaps_thenProducesCorrectDto() {
Article entity = new Article();
entity.setId(1);
entity.setName("Mapstruct Mapping");
Person author = new Person();
author.setId("1");
author.setName("John");
entity.setAuthor(author);
ArticleDTO articleDTO = ArticleMapper.INSTANCE.articleToArticleDto(entity);
assertEquals(articleDTO.getId(), entity.getId());
assertEquals(articleDTO.getName(), entity.getName());
assertEquals(articleDTO.getAuthor().getName(), entity.getAuthor().getName());
}
@Test
public void givenArticle_whenMapsWithUses_thenProducesCorrectDto() {
Article entity = new Article();
entity.setId(1);
entity.setName("Mapstruct Mapping");
Person author = new Person();
author.setId("1");
author.setName("John");
entity.setAuthor(author);
ArticleDTO articleDTO = ArticleUsingPersonMapper.INSTANCE.articleToArticleDto(entity);
assertEquals(articleDTO.getId(), entity.getId());
assertEquals(articleDTO.getName(), entity.getName());
assertEquals(articleDTO.getAuthor().getName(), entity.getAuthor().getName());
}
}