我正在使用MapStruct将对象从DTO映射到DTO。我的映射器依赖于一些服务/存储库来从数据库中获取数据,例如,从具有IDs列表的DTo映射到具有其他POJO列表的POJO。为此,我有一个Mapper inteface和一个抽象类Decorator来实现这个接口。我想测试映射器,但我需要模拟装饰器中的服务。我的问题是我怎样才能做到这一点?
现在我知道如果mapper没有那么多依赖项(SOLID),情况会更好,但我现在需要尽快完成项目。
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.CONSTRUCTOR)
@DecoratedWith(AuthorMapperDecorator.class)
public interface AuthorMapper {
AuthorDTO map(Author entity);
@Mapping(target = "songs", ignore = true)
Author map(AuthorDTO dto);
@Mapping(target = "coauthorSongs", ignore = true)
@Mapping(target = "songs", ignore = true)
@Mapping(target = "id", ignore = true)
@Mapping(target = "name", source = "name")
Author map(UniversalCreateDTO dto);
}
public abstract class AuthorMapperDecorator implements AuthorMapper {
@Autowired
@Qualifier("delegate")
private AuthorMapper delegate;
@Autowired
private SongCoauthorService songCoauthorService;
@Autowired
private SongService songService;
@Override
public Author map(AuthorDTO dto) {
var author = delegate.map(dto);
author.setBiographyUrl(null);
author.setPhotoResource(null);
author.setCoauthorSongs(new HashSet<>(songCoauthorService.findByAuthorId(dto.getId())));
author.setSongs(new HashSet<>(songService.findByAuthorId(dto.getId())));
return author;
}
@Override
public Author map(UniversalCreateDTO dto) {
var author = delegate.map(dto);
author.setSongs(new HashSet<>());
author.setCoauthorSongs(new HashSet<>());
author.setId(Constants.DEFAULT_ID);
return author;
}
}
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-05-13T22:50:36+0200",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 13.0.2 (Oracle Corporation)"
)
@Component
@Qualifier("delegate")
public class AuthorMapperImpl_ implements AuthorMapper {
@Override
public AuthorDTO map(Author entity) {
if ( entity == null ) {
return null;
}
Builder authorDTO = AuthorDTO.builder();
authorDTO.id( entity.getId() );
authorDTO.name( entity.getName() );
return authorDTO.build();
}
@Override
public Author map(AuthorDTO dto) {
if ( dto == null ) {
return null;
}
Author author = new Author();
author.setId( dto.getId() );
author.setName( dto.getName() );
return author;
}
@Override
public Author map(UniversalCreateDTO dto) {
if ( dto == null ) {
return null;
}
Author author = new Author();
author.setName( dto.getName() );
return author;
}
}
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-05-13T22:50:36+0200",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 13.0.2 (Oracle Corporation)"
)
@Component
@Primary
public class AuthorMapperImpl extends AuthorMapperDecorator implements AuthorMapper {
private final AuthorMapper delegate;
@Autowired
public AuthorMapperImpl(@Qualifier("delegate") AuthorMapper delegate) {
this.delegate = delegate;
}
@Override
public AuthorDTO map(Author entity) {
return delegate.map( entity );
}
}
好吧,我在问了这个问题几分钟后找到了答案。太讽刺了。
如果有人需要,我会把它贴在这里。
您只需要将setter添加到服务/存储库的decorator中,并测试将mocks设置为依赖项的实现。
@Setter
public abstract class AuthorMapperDecorator implements AuthorMapper {
@Autowired
@Qualifier("delegate")
private AuthorMapper delegate;
@Autowired
private SongCoauthorService songCoauthorService;
@Autowired
private SongService songService;
@Override
public Author map(AuthorDTO dto) {
var author = delegate.map(dto);
author.setBiographyUrl(null);
author.setPhotoResource(null);
author.setCoauthorSongs(new HashSet<>(songCoauthorService.findByAuthorId(dto.getId())));
author.setSongs(new HashSet<>(songService.findByAuthorId(dto.getId())));
return author;
}
@Override
public Author map(UniversalCreateDTO dto) {
var author = delegate.map(dto);
author.setSongs(new HashSet<>());
author.setCoauthorSongs(new HashSet<>());
author.setId(Constants.DEFAULT_ID);
return author;
}
}
测试:
@ExtendWith(MockitoExtension.class)
@SpringBootTest(classes = { StkSongbookApplication.class, AuthorMapperImpl.class })
class AuthorMapperTest {
@Mock
private SongService songService;
@Mock
private SongCoauthorService songCoauthorService;
@Autowired
private AuthorMapperImpl impl;
private AuthorMapper mapper;
@BeforeEach
void setUp() {
impl.setSongCoauthorService(songCoauthorService);
impl.setSongService(songService);
mapper = impl;
}
@Test
void testMapToDTO() {
Author author = new Author();
author.setId(1L);
author.setName("dummy name");
author.setSongs(new HashSet<>());
author.setCoauthorSongs(new HashSet<>());
AuthorDTO dto = mapper.map(author);
assertEquals(author.getName(), dto.getName());
assertEquals(author.getId(), dto.getId());
}
@Test
void testMapToEntity() {
AuthorDTO author = AuthorDTO.builder().id(1L).name("dummy name").build();
Song song1 = new Song();
song1.setId(1L);
song1.setTitle("title song1");
given(songService.findByAuthorId(1L)).willReturn(List.of(new Song[]{song1}));
Author mapped = mapper.map(author);
assertEquals(author.getId(), mapped.getId());
assertEquals(author.getName(), mapped.getName());
assertEquals(1, mapped.getSongs().size());
}
}
我尝试使用java Spring映射对象DTO到对象正常 我尝试调用接口映射器,因为服务,但我有NullPointerExctive,似乎接口没有注入服务,我使用自动配置,我退出了这个 服务 制图员 错误 我尝试调试,我看到iUserMapper是空的,我不知道如何调用,因为服务 谢谢!
null 我试图用另一个话题来做这件事,但对我来说不起作用。 这是我的mapstruct接口: 这是我的服务班: @SpringBootTest(classes={servicerisqueimpl.class,risqueboconvertisseurimpl.class,risqueboconvertisseur.class})公共类ServiceRisqueImplTest{ junit返回
我有一个简单的Java单模块Gradle项目,其中我使用Mapstruct进行Java映射。我的如下所示: 我的源文件夹包含以下Java源代码:
使用MapStruct,我创建了一个映射器,它是一个抽象类。我决定将映射器从接口转换为抽象,以便使用组件名称,该组件本身使用名为的组件。 尽管映射工作正常,但在单元测试中,它会抱怨组件找不到合格bean。 映射器类。我尝试在注释的属性中添加。但是异常移到了的下一个组件。 出现异常的测试类。正如我提到的,我尝试将添加到。我还尝试通过添加来完全模仿它。
是否可能在MapStruct中使用不同的映射器?我有这个映射器 是否可以将此实现更改为MapStruct?