Spring boot 如何有效地测试mapstruct spring映射器?
我创建了一些用spring构造函数注入注释的mapstruct映射器。我知道在测试中,我可以使用Spring boot 如何有效地测试mapstruct spring映射器?,spring-boot,junit5,mapstruct,Spring Boot,Junit5,Mapstruct,我创建了一些用spring构造函数注入注释的mapstruct映射器。我知道在测试中,我可以使用springboottest自动连接映射器,但它看起来很重,加载速度很慢。我可以在不初始化spring上下文的情况下获取映射程序吗?这是一个很好的实践吗?您总是可以通过Mappers.getMapper(MapperClass.class)获得映射器。但是,当该映射器使用其他映射器时,问题就会出现 当使用其他映射器(@Mapper.uses)时,您可能还希望使用(@Mapper.injectionSt
springboottest
自动连接映射器,但它看起来很重,加载速度很慢。我可以在不初始化spring上下文的情况下获取映射程序吗?这是一个很好的实践吗?您总是可以通过Mappers.getMapper(MapperClass.class)
获得映射器。但是,当该映射器使用其他映射器时,问题就会出现
当使用其他映射器(
@Mapper.uses
)时,您可能还希望使用(@Mapper.injectionStrategy
)和构造函数注入。由于映射器在设计上是无状态的,因此您可以自己编写一些反射代码来构造一个映射器,其中包含所有依赖的使用的映射器 你的意思是我必须手动实例化映射器,包括任何注入链吗?是的。如果您在测试期间不使用框架来安排注入,则需要使用反射(这也是Mappers.getMapper
所做的。我建议只在单元测试中执行此操作。对于测试,也要签出。oops..抱歉,粘贴按钮下的内容错误..以下是正确的链接: