Mapstruct 抽象映射器无法在单元测试中模拟组件
Mapstruct Abstract mapper cannot mock component in unit test
使用 MapStruct,我创建了一个抽象的映射器 class。我决定将映射器从接口转换为抽象,以便使用组件名称 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
映射器class。我尝试将 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);
}
}
出现异常的测试class。正如我提到的,我尝试将 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.
}
}
我认为您需要将@InjectMock 更改为@MockBean
使用 MapStruct,我创建了一个抽象的映射器 class。我决定将映射器从接口转换为抽象,以便使用组件名称 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
映射器class。我尝试将 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);
}
}
出现异常的测试class。正如我提到的,我尝试将 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.
}
}
我认为您需要将@InjectMock 更改为@MockBean