【问题标题】:How to efficiently test mapstruct spring mapper?如何有效地测试mapstruct spring mapper?
【发布时间】:2020-04-25 14:59:09
【问题描述】:

我创建了一些使用 spring 构造函数注入注解的 mapstruct 映射器。我知道在测试中我可以用springboottest 自动连接映射器,但它看起来很重,加载速度也很慢。我可以在不初始化弹簧上下文的情况下获取映射器吗?这是好的做法吗?

【问题讨论】:

    标签: spring-boot junit5 mapstruct


    【解决方案1】:

    您始终可以通过Mappers.getMapper(MapperClass.class) 获取映射器。但是,当该映射器使用其他映射器时,就会出现问题。

    当使用其他映射器 (@Mapper.uses) 时,您可能还想使用 (@Mapper.injectionStrategy) 并使用构造函数注入。由于映射器在设计上是无状态的,因此您可以自己编写一些反射代码来构造一个映射器及其所有依赖的uses 映射器。

    【讨论】:

    • 你的意思是我必须手动实例化映射器,包括任何注入链?
    • 是的。如果您在测试期间不使用框架来安排注入,则需要使用反射(这也是Mappers.getMapper 所做的。我建议仅在单元测试中执行此操作。对于测试,也请检查stackoverflow.com/questions/61398218/…
    • oops.. 抱歉,我的粘贴按钮下有错误的内容.. 这是正确的链接:stackoverflow.com/questions/60986556/…
    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2012-10-25
    • 2023-02-07
    • 2020-12-20
    相关资源
    最近更新 更多