Spock inmemory implementation mapstruct autowired inside mapper

Spock inmemory implementation mapstruct autowired inside mapper

你好,我想测试一些在没有 h2 的内存实现中使用的方法。

我有 userMapper 的实现。

当我尝试 运行 我的测试时,我遇到了此 passwordEncoderMapper 的 NullPointerException。 我怎样才能像第一个屏幕截图一样注入到归档的自动装配中(authService)without load spring context (@SpringBootTest).

passwordEncoderMapper 使用 NullPointerException 的原因是因为您将 spring componentModelMappers#getMapper 混合在一起。你不应该那样做。

当您使用自定义 componentModel 时,您应该始终使用该依赖项注入来获取映射器。

您可以为测试做的是将 Mapper#injectionStrategyInjectionStrategy#CONSTRUCTOR 结合使用,然后通过向其提供正确的依赖项来自行实例化映射器。