mapstruct对象的Junit测试

mapstruct对象的Junit测试,junit,junit5,mapstruct,Junit,Junit5,Mapstruct,我创建了一个单元测试,其中我有一个PersonMapper,它使用PetMapper和WorkMapper,最后,在我的代码中,我使用mapstrct生成的类,而不是我创建的接口 下面的代码代表我的单元测试: @SpringBootTest(类={PersonMapperImpl.class,PetMapperImpl.class,WorkMapperImpl.class}) 公共类人事顾问{ @自动连线的私人PersonMapperImpl PersonMapperImpl; @试验 ...

我创建了一个单元测试,其中我有一个
PersonMapper
,它使用
PetMapper
WorkMapper
,最后,在我的代码中,我使用mapstrct生成的类,而不是我创建的接口

下面的代码代表我的单元测试:

@SpringBootTest(类={PersonMapperImpl.class,PetMapperImpl.class,WorkMapperImpl.class})
公共类人事顾问{
@自动连线的私人PersonMapperImpl PersonMapperImpl;
@试验
...
}

我的问题是,我希望使用接口,但在这个单元测试中,我直接将mapstruct生成的类放在了一起。这是测试mapstruct对象的好方法吗?

在使用mapstruct和Spring时,映射程序应该具有
@Mapper(componentModel=“Spring”)
。这将创建用
@Component
注释的实现类。为了能够在测试中使用接口,您需要对映射程序包进行组件扫描。

在每个映射程序类中不使用
componentModel
,而是将此属性添加到POM文件中的MapStruct插件中:


-Amapstruct.defaultComponentModel=弹簧
然后,我只需删除属性
,如下所示,一切正常

@SpringBootTest
public class PersonMapperTest {

    @Autowired private PersonMapper personMapper;

    @Test
    ...
}

为什么要使用实现而不是接口?因为如果我使用接口,spring会返回以下异常:
org.springframework.beans.factory.UnsatisfiedDependencyException
Ah。。我也很怀疑。。签出:我认为您需要@componentscan注释。仍然无法工作:(