Unit testing External Command Handles and Event Handlers

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)
}

}

Hi Jerry,

Sadly, your question is a very short answer: you cannot test external command and event handlers with the AggregateTestFixtures.
This is intentional, as those fixtures are meant for testing the Command Handlers of Aggregates, or External Command Handler which execute logic for Aggregates.

For testing your event handlers, I would suggest to write ‘regular’ unit tests which create your Event Handling Component as a test subject.
We have an issue laying around to provide fixtures for your projections as well.

Any how, for now, I can tell you the Aggregate Test Fixtures aren’t there for integration like tests which you’re trying to achieve.

Hope this sheds some light on your situation and options!

Cheers,

Steven

Thanks for your reply, that gives us some info to think about

Hi Jerry,

Sadly, your question is a very short answer: you cannot test external command and event handlers with the AggregateTestFixtures.
This is intentional, as those fixtures are meant for testing the Command Handlers of Aggregates, or External Command Handler which execute logic for Aggregates.

For testing your event handlers, I would suggest to write ‘regular’ unit tests which create your Event Handling Component as a test subject.
We have an issue laying around to provide fixtures for your projections as well.

Any how, for now, I can tell you the Aggregate Test Fixtures aren’t there for integration like tests which you’re trying to achieve.

Hope this sheds some light on your situation and options!

Cheers,

Steven