Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何测试MapStruct映射器实现并模拟其依赖关系_Java_Spring_Spring Boot_Mockito_Mapstruct - Fatal编程技术网

Java 如何测试MapStruct映射器实现并模拟其依赖关系

Java 如何测试MapStruct映射器实现并模拟其依赖关系,java,spring,spring-boot,mockito,mapstruct,Java,Spring,Spring Boot,Mockito,Mapstruct,我正在使用MapStruct将对象从DTO映射到DTO。我的映射程序依赖于某些服务/存储库从数据库获取数据,例如从具有ID列表的DTo映射到具有其他POJO列表的POJO。 为此,我有一个映射接口和一个抽象类装饰器来实现这个接口。 我想测试映射器,但我需要模拟装饰器内的服务。我的问题是我如何才能做到这一点 现在我知道如果mapper没有那么多的依赖关系,会更好,但是我现在需要快速完成这个项目 看起来是这样的: 制图员 @Mapper(componentModel = "spring", inje

我正在使用MapStruct将对象从DTO映射到DTO。我的映射程序依赖于某些服务/存储库从数据库获取数据,例如从具有ID列表的DTo映射到具有其他POJO列表的POJO。 为此,我有一个映射接口和一个抽象类装饰器来实现这个接口。 我想测试映射器,但我需要模拟装饰器内的服务。我的问题是我如何才能做到这一点

现在我知道如果mapper没有那么多的依赖关系,会更好,但是我现在需要快速完成这个项目

看起来是这样的:

制图员

@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;
  }
}

好的,我在问了问题几分钟后找到了答案。真讽刺

如果有人需要,我会把它贴在这里

您只需要将setter添加到服务/存储库的decorator中,并测试设置了mock而不是依赖项的实现

像这样:

装饰师:

@塞特 公共抽象类AuthorMapperDecorator实现AuthorMapper{ @自动连线 @限定符代表 私有AuthorMapper委托; @自动连线 私人歌曲合作作者服务; @自动连线 私人歌曲服务; @凌驾 公共作者MapAuthorTo dto{ var author=delegate.mapdto; author.setBiographyUrlnull; author.setPhotoResourcenull; author.setCoauthorSongsnew HashSetsongCoauthorService.findbyauthoridto.getId; author.setSongsnew HashSetsongService.findbyauthoridto.getId; 返回作者; } @凌驾 公共作者映射通用创建到dto{ var author=delegate.mapdto; author.setSongsnew HashSet; author.setCoauthorSongsnew HashSet; author.setIdConstants.DEFAULT\u ID; 返回作者; } } 测试:

@ExtendWithMockitoExtension.class @SpringBootTestclasses={StkSongbookApplication.class,AuthorMapperImpl.class} 类AuthorMapperTest{ @嘲弄 私人歌曲服务; @嘲弄 私人歌曲合作作者服务; @自动连线 私有authormapperpimpl; 私有AuthorMapper映射器; @之前 无效设置{ impl.SetSongCoauthorService SongCoauthorService; impl.setSongServicesongService; mapper=impl; } @试验 void testmapto{ 作者=新作者; 作者:setId1L; author.setNamedummy名称; author.setSongsnew HashSet; author.setCoauthorSongsnew HashSet; AuthorDTO dto=mapper.mapauthor; assertEqualsauthor.getName,dto.getName; assertEqualsauthor.getId,dto.getId; } @试验 无效testMapToEntity{ AuthorDTO author=AuthorDTO.builder.id1L.namemdummy name.build; 歌曲1=新歌; 宋1.setId1L; 歌曲1.SetTitletle歌曲1; 新歌[]{song1}的givensongService.findByAuthorId1L.willReturnList; Author mapped=mapper.mapper; assertEqualsauthor.getId,mapped.getId; assertEqualsauthor.getName,mapped.getName; assertEquals1,mapped.getSongs.size; } }
好的,我在问了问题几分钟后找到了答案。真讽刺

如果有人需要,我会把它贴在这里

您只需要将setter添加到服务/存储库的decorator中,并测试设置了mock而不是依赖项的实现

像这样:

装饰师:

@塞特 公共抽象类AuthorMapperDecorator实现AuthorMapper{ @自动连线 @限定符代表 私有AuthorMapper委托; @自动连线 私人歌曲合作作者服务; @自动连线 私人歌曲服务; @凌驾 公共作者MapAuthorTo dto{ var author=delegate.mapdto; author.setBiographyUrlnull; author.setPhotoResourcenull; author.setCoauthorSongsnew HashSetsongCoauthorService.findbyauthoridto.getId; author.setSongsnew HashSetsongService.findbyauthoridto.getId; 返回作者; } @凌驾 公共作者映射通用创建到dto{ var author=delegate.mapdto; author.setSongsnew HashSet; author.setCoauthorSongsnew HashSet; author.setIdConstants.DEFAULT\u ID; 返回作者; } } 测试:

@ExtendWithMockitoExtension.class @SpringBootTestclasses={StkSongbookApplication.class,AuthorMapperImpl.class} 类AuthorMapperTest{ @嘲弄 私人歌曲服务; @嘲弄 私人歌曲合作作者服务; @自动连线 私有authormapperpimpl; 私有AuthorMapper映射器; @之前 无效设置{ impl.SetSongCoauthorService SongCoauthorService; impl.setSongServicesongService; mapper=impl; } @试验 void testmapto{ 作者=新作者; 作者:setId1L; author.setNamedummy名称; author.setSongsnew HashSet; author.setCoauthorSongsnew HashSet; AuthorDTO dto=mapper.mapauthor; assertEqualsauthor.getName,dto.getName; assertEqualsauthor.getId,dto.getId; } @试验 无效测试 适应性{ AuthorDTO author=AuthorDTO.builder.id1L.namemdummy name.build; 歌曲1=新歌; 宋1.setId1L; 歌曲1.SetTitletle歌曲1; 新歌[]{song1}的givensongService.findByAuthorId1L.willReturnList; Author mapped=mapper.mapper; assertEqualsauthor.getId,mapped.getId; assertEqualsauthor.getName,mapped.getName; assertEquals1,mapped.getSongs.size; } }
@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 );
    }
}