Search code examples
javaspring-bootunit-testingspockmapstruct

Spock inmemory implementation mapstruct autowired inside mapper


Hi I want to test some method using in memory implementation without h2.

enter image description here

In impl of userMapper I have.

enter image description here

When I try to run my test, I have NullPointerException of this passwordEncoderMapper. How can I inject to filed autowire like on first screenshoot(authService) without load spring context (@SpringBootTest).


Solution

  • The reason why you have a NullPointerException for the passwordEncoderMapper is because you are mixing the spring componentModel with Mappers#getMapper. You shouldn't do that.

    When you are using a custom componentModel you should always use that dependency injection in order to get the mappers.

    What you can do for tests is to use Mapper#injectionStrategy with InjectionStrategy#CONSTRUCTOR and then instantiate the mapper on your own by providing the correct dependencies to it.