简体   繁体   English

Mapstruct 抽象映射器无法在单元测试中模拟组件

[英]Mapstruct Abstract mapper cannot mock component in unit test

With MapStruct, I created a mapper that is an abstract class.使用 MapStruct,我创建了一个抽象的 class 映射器。 I deciced to transform the mapper from interface to abstract, in order to use a component names AddressConverter that itself is using a component named CountryService .我决定将映射器从接口转换为抽象,以便使用一个名为CountryService的组件,该组件本身正在使用一个名为AddressConverter的组件。

Even though the mapping works fine, on unit test it complains about the component AddressConverter that cannot find a qualifying bean.尽管映射工作正常,但在单元测试中它抱怨组件AddressConverter找不到合格的 bean。

I tried adding it to ContextConfiguration of the mapper, but the issue will chain to the nested component up until the repository which I cannot add it to ContextConfiguration since it's an interface.我尝试将它添加到映射器的ContextConfiguration中,但问题将链接到嵌套组件,直到我无法将它添加到ContextConfigurationrepository ,因为它是一个接口。

The exception例外

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

Mapper class.映射器 class。 I tried adding AddressConverter to the uses attribute on @Mapper annotation.我尝试将 AddressConverter 添加到AddressConverter注释的uses属性@Mapper But the exception moved to the next component of AddressConverter as I mentioned above.但正如我上面提到的,异常转移到了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); 
    }
}

The test class that the exception appears.出现异常的测试class。 As I mentioned, I tried adding AddressConverter to ContextConfiguration .正如我所提到的,我尝试将AddressConverter添加到ContextConfiguration I also tried mocking it completely by adding InjectMocks .我还通过添加 InjectMocks 完全尝试了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.
   }
 
}

I belive you need to change @InjectMock with @MockBean我相信你需要用@MockBean 改变@InjectMock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM