这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions mapstruct-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mapstruct-2</finalName>
Expand Down Expand Up @@ -60,6 +72,7 @@
<properties>
<org.mapstruct.version>1.6.3</org.mapstruct.version>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
<springframework.version>6.2.1</springframework.version>
</properties>

</project>
37 changes: 37 additions & 0 deletions mapstruct-2/src/main/java/com/baeldung/dto/MediaDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.baeldung.dto;

public class MediaDto {

private Long id;

private String title;

public MediaDto(Long id, String title) {
this.id = id;
this.title = title;
}

public MediaDto() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Override
public String toString() {
return "MediaDto{" + "id=" + id + ", title='" + title + '\'' + '}';
}
}
37 changes: 37 additions & 0 deletions mapstruct-2/src/main/java/com/baeldung/entity/Media.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.baeldung.entity;

public class Media {

private Long id;

private String title;

public Media(Long id, String title) {
this.id = id;
this.title = title;
}

public Media() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Override
public String toString() {
return "Media{" + "id=" + id + ", title='" + title + '\'' + '}';
}
}
15 changes: 15 additions & 0 deletions mapstruct-2/src/main/java/com/baeldung/mapper/MediaMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.mapper;

import org.mapstruct.Mapper;

import com.baeldung.dto.MediaDto;
import com.baeldung.entity.Media;

@Mapper
public interface MediaMapper {

MediaDto toDto(Media media);

Media toEntity(MediaDto mediaDto);

}
26 changes: 26 additions & 0 deletions mapstruct-2/src/main/java/com/baeldung/service/MediaService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baeldung.dto.MediaDto;
import com.baeldung.entity.Media;
import com.baeldung.mapper.MediaMapper;

public class MediaService {

final Logger logger = LoggerFactory.getLogger(MediaService.class);

private final MediaMapper mediaMapper;

public MediaService(MediaMapper mediaMapper) {
this.mediaMapper = mediaMapper;
}

public Media persistMedia(MediaDto mediaDto) {
Media media = mediaMapper.toEntity(mediaDto);
logger.info("Persist media: {}", media);
return media;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.service;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.mapstruct.factory.Mappers;

import com.baeldung.dto.MediaDto;
import com.baeldung.entity.Media;
import com.baeldung.mapper.MediaMapper;

public class MediaServiceGeneratedMapperUnitTest {

@Test
public void whenGeneratedMapperIsUsed_thenActualValuesAreMapped() {
MediaService mediaService = new MediaService(Mappers.getMapper(MediaMapper.class));
MediaDto mediaDto = new MediaDto(1L, "title 1");
Media persisted = mediaService.persistMedia(mediaDto);
assertEquals(mediaDto.getId(), persisted.getId());
assertEquals(mediaDto.getTitle(), persisted.getTitle());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.baeldung.service;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Test;

import com.baeldung.dto.MediaDto;
import com.baeldung.entity.Media;
import com.baeldung.mapper.MediaMapper;

public class MediaServiceMockedMapperUnitTest {

@Test
public void whenMockedMapperIsUsed_thenMockedValuesAreMapped() {
MediaMapper mockMediaMapper = mock(MediaMapper.class);
Media mockedMedia = new Media(5L, "Title 5");
when(mockMediaMapper.toEntity(any())).thenReturn(mockedMedia);

MediaService mediaService = new MediaService(mockMediaMapper);
MediaDto mediaDto = new MediaDto(1L, "title 1");
Media persisted = mediaService.persistMedia(mediaDto);

verify(mockMediaMapper).toEntity(mediaDto);
assertEquals(mockedMedia.getId(), persisted.getId());
assertEquals(mockedMedia.getTitle(), persisted.getTitle());
}

}
17 changes: 17 additions & 0 deletions mapstruct-2/src/test/java/com/baeldung/spring/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.baeldung.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baeldung.mapper.MediaMapper;
import com.baeldung.service.MediaService;

@Configuration
public class Config {

@Bean
public MediaService mediaService(MediaMapper mediaMapper) {
return new MediaService(mediaMapper);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baeldung.spring;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baeldung.dto.MediaDto;
import com.baeldung.entity.Media;
import com.baeldung.service.MediaService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { Config.class, MediaSpringMapperImpl.class })
public class MediaServiceSpringGeneratedMapperUnitTest {

@Autowired
MediaService mediaService;

@Test
public void whenGeneratedSpringMapperIsUsed_thenActualValuesAreMapped() {
MediaDto mediaDto = new MediaDto(1L, "title 1");
Media persisted = mediaService.persistMedia(mediaDto);
assertEquals(mediaDto.getId(), persisted.getId());
assertEquals(mediaDto.getTitle(), persisted.getTitle());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baeldung.spring;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baeldung.dto.MediaDto;
import com.baeldung.entity.Media;
import com.baeldung.service.MediaService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class MediaServiceSpringMockedMapperUnitTest {

@Autowired
MediaService mediaService;

@MockitoBean
MediaSpringMapper mockMediaMapper;

@Test
public void whenMockedSpringMapperIsUsed_thenMockedValuesAreMapped() {
Media mockedMedia = new Media(12L, "title 12");
when(mockMediaMapper.toEntity(ArgumentMatchers.any())).thenReturn(mockedMedia);

MediaDto mediaDto = new MediaDto(1L, "title 1");
Media persisted = mediaService.persistMedia(mediaDto);

verify(mockMediaMapper).toEntity(mediaDto);
assertEquals(mockedMedia.getId(), persisted.getId());
assertEquals(mockedMedia.getTitle(), persisted.getTitle());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baeldung.spring;

import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;

import com.baeldung.mapper.MediaMapper;

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface MediaSpringMapper extends MediaMapper {
}