lateinit property has not been initialized in aggregate

Hello,

I have a user domain aggregate which handles commands like, create, activate, change password. When I run the test I get the passwordEncoder not initialized exception. Why is that passwordEncoder bean not injected into User aggregate in test?

Then ran the web app and send command via Spring mvc, command is handled in user aggregate with no error.

Here is the simplified setup:

// Security.kt

@Configuration
class SecurityConfig {

    @Bean
    fun passwordEncoder(): PasswordEncoder {
        return BCryptPasswordEncoder()
    }

}

// User.kt

since the fixture test is no spring test, you have to manually “fake” the injection

fixture.registerInjectableResource(passwordEncoderMock)

BTW - I also tried running tests with spring runner and spring boot test, it didn’t work either.

@RunWith(SpringRunner::class)
@SpringBootTest

I’ll mock it as you suggested.

Thanks

Hi Altug, Jan,

Axon Framework works with ParameterResolvers to resolve parameters for you message handling functions (like @EventHandler and @CommandHandler annotated functions).

Additionally, it contains a SpringBeanParameterResolver, which thus wires a Spring Bean into your command/event/query handler directly.

This is typically the way to go to wire a service into your Aggregate.

Wiring it through the @Autowired annotation as you’ve done Altug is at this point not completely supported.

I believe it has something to do in combination with Event Sourcing (obviously guessing you’re doing Event Sourcing), as the EventSourcingRepository will recreate an Aggregate based on the events stored, without doing any Spring specific autowiring.

So, rather than having the @Autowired lateinit var passwordEncoder: PasswordEncoder, you can put that service directly in your Command/Event handling function as another parameter.

For testing, Jan his suggestion is definitely correct.

you need to register that service as a resource to the Test Fixture.
Otherwise it will not be wired for you.

Hope this helps!

Cheers,

Steven