Skip to content

Commit 2961038

Browse files
Merge pull request #18625 from maenolis/BAEL-9097
Testing with Mapstruct Mappers
2 parents 9659791 + a8f2cfb commit 2961038

File tree

11 files changed

+282
-0
lines changed

11 files changed

+282
-0
lines changed

mapstruct-2/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525
<version>${lombok.version}</version>
2626
<scope>provided</scope>
2727
</dependency>
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-context</artifactId>
31+
<version>${springframework.version}</version>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework</groupId>
36+
<artifactId>spring-test</artifactId>
37+
<version>${springframework.version}</version>
38+
<scope>test</scope>
39+
</dependency>
2840
</dependencies>
2941
<build>
3042
<finalName>mapstruct-2</finalName>
@@ -60,6 +72,7 @@
6072
<properties>
6173
<org.mapstruct.version>1.6.3</org.mapstruct.version>
6274
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
75+
<springframework.version>6.2.1</springframework.version>
6376
</properties>
6477

6578
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.dto;
2+
3+
public class MediaDto {
4+
5+
private Long id;
6+
7+
private String title;
8+
9+
public MediaDto(Long id, String title) {
10+
this.id = id;
11+
this.title = title;
12+
}
13+
14+
public MediaDto() {
15+
}
16+
17+
public Long getId() {
18+
return id;
19+
}
20+
21+
public void setId(Long id) {
22+
this.id = id;
23+
}
24+
25+
public String getTitle() {
26+
return title;
27+
}
28+
29+
public void setTitle(String title) {
30+
this.title = title;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "MediaDto{" + "id=" + id + ", title='" + title + '\'' + '}';
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.entity;
2+
3+
public class Media {
4+
5+
private Long id;
6+
7+
private String title;
8+
9+
public Media(Long id, String title) {
10+
this.id = id;
11+
this.title = title;
12+
}
13+
14+
public Media() {
15+
}
16+
17+
public Long getId() {
18+
return id;
19+
}
20+
21+
public void setId(Long id) {
22+
this.id = id;
23+
}
24+
25+
public String getTitle() {
26+
return title;
27+
}
28+
29+
public void setTitle(String title) {
30+
this.title = title;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "Media{" + "id=" + id + ", title='" + title + '\'' + '}';
36+
}
37+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.mapper;
2+
3+
import org.mapstruct.Mapper;
4+
5+
import com.baeldung.dto.MediaDto;
6+
import com.baeldung.entity.Media;
7+
8+
@Mapper
9+
public interface MediaMapper {
10+
11+
MediaDto toDto(Media media);
12+
13+
Media toEntity(MediaDto mediaDto);
14+
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.service;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import com.baeldung.dto.MediaDto;
7+
import com.baeldung.entity.Media;
8+
import com.baeldung.mapper.MediaMapper;
9+
10+
public class MediaService {
11+
12+
final Logger logger = LoggerFactory.getLogger(MediaService.class);
13+
14+
private final MediaMapper mediaMapper;
15+
16+
public MediaService(MediaMapper mediaMapper) {
17+
this.mediaMapper = mediaMapper;
18+
}
19+
20+
public Media persistMedia(MediaDto mediaDto) {
21+
Media media = mediaMapper.toEntity(mediaDto);
22+
logger.info("Persist media: {}", media);
23+
return media;
24+
}
25+
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.service;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
import org.mapstruct.factory.Mappers;
7+
8+
import com.baeldung.dto.MediaDto;
9+
import com.baeldung.entity.Media;
10+
import com.baeldung.mapper.MediaMapper;
11+
12+
public class MediaServiceGeneratedMapperUnitTest {
13+
14+
@Test
15+
public void whenGeneratedMapperIsUsed_thenActualValuesAreMapped() {
16+
MediaService mediaService = new MediaService(Mappers.getMapper(MediaMapper.class));
17+
MediaDto mediaDto = new MediaDto(1L, "title 1");
18+
Media persisted = mediaService.persistMedia(mediaDto);
19+
assertEquals(mediaDto.getId(), persisted.getId());
20+
assertEquals(mediaDto.getTitle(), persisted.getTitle());
21+
}
22+
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.service;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.when;
8+
9+
import org.junit.Test;
10+
11+
import com.baeldung.dto.MediaDto;
12+
import com.baeldung.entity.Media;
13+
import com.baeldung.mapper.MediaMapper;
14+
15+
public class MediaServiceMockedMapperUnitTest {
16+
17+
@Test
18+
public void whenMockedMapperIsUsed_thenMockedValuesAreMapped() {
19+
MediaMapper mockMediaMapper = mock(MediaMapper.class);
20+
Media mockedMedia = new Media(5L, "Title 5");
21+
when(mockMediaMapper.toEntity(any())).thenReturn(mockedMedia);
22+
23+
MediaService mediaService = new MediaService(mockMediaMapper);
24+
MediaDto mediaDto = new MediaDto(1L, "title 1");
25+
Media persisted = mediaService.persistMedia(mediaDto);
26+
27+
verify(mockMediaMapper).toEntity(mediaDto);
28+
assertEquals(mockedMedia.getId(), persisted.getId());
29+
assertEquals(mockedMedia.getTitle(), persisted.getTitle());
30+
}
31+
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.spring;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
import com.baeldung.mapper.MediaMapper;
7+
import com.baeldung.service.MediaService;
8+
9+
@Configuration
10+
public class Config {
11+
12+
@Bean
13+
public MediaService mediaService(MediaMapper mediaMapper) {
14+
return new MediaService(mediaMapper);
15+
}
16+
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.spring;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
11+
import com.baeldung.dto.MediaDto;
12+
import com.baeldung.entity.Media;
13+
import com.baeldung.service.MediaService;
14+
15+
@RunWith(SpringJUnit4ClassRunner.class)
16+
@ContextConfiguration(classes = { Config.class, MediaSpringMapperImpl.class })
17+
public class MediaServiceSpringGeneratedMapperUnitTest {
18+
19+
@Autowired
20+
MediaService mediaService;
21+
22+
@Test
23+
public void whenGeneratedSpringMapperIsUsed_thenActualValuesAreMapped() {
24+
MediaDto mediaDto = new MediaDto(1L, "title 1");
25+
Media persisted = mediaService.persistMedia(mediaDto);
26+
assertEquals(mediaDto.getId(), persisted.getId());
27+
assertEquals(mediaDto.getTitle(), persisted.getTitle());
28+
}
29+
30+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.spring;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.mockito.Mockito.verify;
5+
import static org.mockito.Mockito.when;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.mockito.ArgumentMatchers;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
15+
import com.baeldung.dto.MediaDto;
16+
import com.baeldung.entity.Media;
17+
import com.baeldung.service.MediaService;
18+
19+
@RunWith(SpringJUnit4ClassRunner.class)
20+
@ContextConfiguration(classes = Config.class)
21+
public class MediaServiceSpringMockedMapperUnitTest {
22+
23+
@Autowired
24+
MediaService mediaService;
25+
26+
@MockitoBean
27+
MediaSpringMapper mockMediaMapper;
28+
29+
@Test
30+
public void whenMockedSpringMapperIsUsed_thenMockedValuesAreMapped() {
31+
Media mockedMedia = new Media(12L, "title 12");
32+
when(mockMediaMapper.toEntity(ArgumentMatchers.any())).thenReturn(mockedMedia);
33+
34+
MediaDto mediaDto = new MediaDto(1L, "title 1");
35+
Media persisted = mediaService.persistMedia(mediaDto);
36+
37+
verify(mockMediaMapper).toEntity(mediaDto);
38+
assertEquals(mockedMedia.getId(), persisted.getId());
39+
assertEquals(mockedMedia.getTitle(), persisted.getTitle());
40+
}
41+
42+
}

0 commit comments

Comments
 (0)