【问题标题】:Mapstruct Abstract mapper cannot mock component in unit testMapstruct 抽象映射器无法在单元测试中模拟组件
【发布时间】:2021-06-11 08:14:41
【问题描述】:

使用 MapStruct,我创建了一个抽象类的映射器。我决定将映射器从接口转换为抽象,以便使用组件名称 AddressConverter,它本身正在使用名为 CountryService 的组件。

尽管映射工作正常,但在单元测试中它抱怨组件 AddressConverter 找不到合格的 bean。

我尝试将它添加到映射器的ContextConfiguration,但问题将链接到嵌套组件直到repository,我无法将它添加到ContextConfiguration,因为它是一个接口。

例外

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mind.microservice.mapper.converter.AddressConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)
    ... 42 more

映射器类。我尝试将AddressConverter 添加到@Mapper 注释的uses 属性中。但正如我上面提到的,异常转移到AddressConverter 的下一个组件。

@Mapper(componentModel = "spring", uses = {
        GenericMapper.class,
        Size.class
})
public abstract class StudentMapper{

    @Autowired
    private AddressConverter addressConverter;

    @Mappings({
            @Mapping(target = "address", source = "student", qualifiedByName = "formatAddress"),
    })
    public abstract StudentEntity map(Student student);

    @Named("formatAddress")
    public String formatAddress(Student student){
        return this.addressConverter.buildAddress(student);
    }
    
}

AddressCoverter

@Component
@AllArgsConstructor
public class AddressConverter {

    private final CountryService countryService;

    public String buildAddress(Student student){
        return this.countryService.countryFormatter(student.getCountry); 
    }
}

出现异常的测试类。正如我所提到的,我尝试将AddressConverter 添加到ContextConfiguration。我还尝试通过添加InjectMocks 来完全模拟它。

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {
        GenericMapper.class,
        Size.class
})
public class StudentMapperTest{

    @Autowired
    private StudentMapper MAPPER;
     
    //also @Autowired was used and also I removed it completely, still the same exception
    @InjectMocks 
    private AddressConverter addressConverter;

    @Test
    public void testStudentToStudentEntityMapping() {
        Student randomStudent = ObjectHandler.random(Student.class);

      //...the rest of the test but it doesn't even enter, so it doesn't affect the outcome.
   }
 
}

【问题讨论】:

    标签: java spring-boot junit dependency-injection mapstruct


    【解决方案1】:

    我相信你需要用@MockBean 改变@InjectMock

    【讨论】:

    • 非常感谢。
    猜你喜欢
    • 2017-11-03
    • 2019-07-13
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2017-12-29
    • 2023-01-31
    相关资源
    最近更新 更多