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.
// Security.kt
@Configuration
class SecurityConfig {
@Bean
fun passwordEncoder(): PasswordEncoder {
return BCryptPasswordEncoder()
}
}
// User.kt
@Aggregate
class User {
@AggregateIdentifier
private lateinit var id: UserId
@Autowired
lateinit var passwordEncoder: PasswordEncoder
// removed other crap
}
// UserTests.kt
class UserTests {
private lateinit var fixture: AggregateTestFixture<User>
private val userId = UserId()
@Before
fun setUp() {
fixture = AggregateTestFixture<User>(User::class.java)
}
fun tesChangeUserPassword() {
fixture.given(UserCreatedEvent(userId, "em...@email.com", "password"))
.`when`(ChangeUserPasswordCommand(userId, "password", "new password"))
.expectEvents(UserChangedPasswordEvent(userId, "new password"))
}
}
// Running the test above, gives me this exception
org.axonframework.test.AxonAssertionError: The published events do not match the expected events
Expected | Actual
-------------------------------------------------------|-------------------------------------------------------
com.testapp.domain.UserChangedPasswordEvent <|>
A probable cause for the wrong chain of events is an exception that occurred while handling the command.
kotlin.UninitializedPropertyAccessException: lateinit property passwordEncoder has not been initialized