Hello,
We are trying to spin up unit testing of our event and command handlers
I have this snippet of code:
We got the DummyCommandHandler (See below) to work
but we are not sure how to get the DummyEventHandler to
work.
When I run the “” test< get this error:
org.axonframework.test.FixtureExecutionException:
Cannot inject resources after command handler has been created.
Configure all resource before calling registerCommandHandler() or registerAnnotatedCommandHandler()
Any code examples would be great!
thanks
Jerry
package com.aramark.ess.com.aramark.ess.command.domain
import com.aramark.common.AuditEntry
import com.aramark.ess.command.domain.aggregates.SyncHost
import com.aramark.ess.command.domain.aggregates.SyncHostHandler
import com.aramark.ess.command.domain.aggregates.SyncPoint
import com.aramark.ess.command.domain.aggregates.SyncPointHandler
import com.aramark.ess.common.*
import org.axonframework.commandhandling.CommandHandler
import org.axonframework.config.Configurer
import org.axonframework.eventhandling.*
import org.axonframework.spring.config.AxonConfiguration
import org.axonframework.test.aggregate.AggregateTestFixture
import org.axonframework.test.aggregate.FixtureConfiguration
import org.junit.Before
import org.junit.Test
import java.util.*
import org.springframework.beans.factory.annotation.Autowired
class DummyCommandHandler() {
@CommandHandler
fun handle(event: RegisterSyncPointCommand) {
print(“GOT HERE”)
}
}
class DummyEventHandler() {
@EventHandler
fun handle(event: SyncPointRegisterRequested) {
print(“GOT HERE”)
}
}
class SyncPointAggregateTest {
private lateinit var fixture: FixtureConfiguration
//@Autowired
//private var eventBus: SimpleEventBus
@Autowired
private var dummyCommandHandler: DummyCommandHandler
@Before
fun setUp() {
fixture = AggregateTestFixture(SyncPoint::class.java)
fixture.registerAnnotatedCommandHandler(dummyCommandHandler)
fixture.registerInjectableResource(DummyEventHandler())
//fixture.registerAnnotatedCommandHandler(SyncPointHandler(repository = fixture.getRepository(), eventBus = eventBus))
}
@Test
fun on RegisterSyncPointCommand, SyncPointRegisterRequested event is fired
() {
val headers= mapOf(“header1” to “x”, “header2” to “y”)
val registerSyncPointCommand = RegisterSyncPointCommand(id = “foo”, hostName = “micky”, requestType = “GET”, requestUri = “http://test.org”,requestBody = “”, hostEnv = “dev”, requestHeaders = headers, cronSchedule = “" ,timezone = “EST”, model = “service_area”, modelTemplate = “”, kafkaTopic = “test”, active = true, auditEntry = AuditEntry(who = “test”, when
= Date()))
val syncPointRegisterRequested = SyncPointRegisterRequested(id = “foo”, hostName = “micky”, hostEnv = “dev”, requestType = “GET”, requestUri = “http://test.org”, requestBody = “”, requestHeaders = headers, cronSchedule = "”, timezone = “EST”, model = “service_area”, modelTemplate = “”, kafkaTopic = “test”, active = true)
fixture.given()
.when
(registerSyncPointCommand)
.expectSuccessfulHandlerExecution()
.expectEvents(syncPointRegisterRequested)
}
}