'DDD, companion object and UninitializedPropertyAccessException lateinit property bus has not been initialized
i'm quite new to Kotlin and having troubles understanding my issue with initalizing a vertx EventBus.
I'm using Kotlin with Quarkus (which might not be to relevant here) and Vertx in a DDD context.
I have a model, which provides a static method for sending an update. This static method creates the model and the model tries to inject the EventBus:
package model
import java.util.*
import javax.enterprise.context.ApplicationScoped
@ApplicationScoped
class EventSet(
) {
var events = listOf<Event>()
@field:Default
@Inject
lateinit var bus: EventBus
// kotlin.UninitializedPropertyAccessException: lateinit property bus has not been initialized
fun sendAndStore() {
... persist() happens here and other things
bus.publish(UPDATE, entity)
}
companion object {
const val UPDATE = "update"
fun create(events: List<Event>): EventSet {
if (events.isEmpty()) {
throw Exception()
}
val eventSet = EventSet()
eventSet.events = events
return eventSet
}
}
}
When i'm running my unit test (Annotated with @QuarkusTest on the class, @Test and @TestTransaction on the function, i'm getting kotlin.UninitializedPropertyAccessException: lateinit property bus has not been initialized
I don't understand it at all. My assumption was that i can inject it whenever i want. Unfortunate the constructor injection would require me to loop through it through the companion object as i was also not able to inject anything into my static method (which i assume shouldn't work anyway).
How do i debug this type of issue? I checked the quarkus dev console and the Vertx/EventBus is marked as a @Singleton bean, not sure if that makes any difference.
Thanks,
Sigi
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
