Axon 3. Test command bus dispatch

Good evening,

I was wondering if someone could help me. I would like to test the dispatch in a command bus. For example:

CommandBus commandBus = config.getBean(CommandBus.class); //config is the application context

commandBus.dispatch(new GenericCommandMessage<>(new CreateAccountCommand(1, “john”, “doe”)));

The above code was from an example that I retype and it was located in the application main, and for that reason it is getting the bean. Now, I am trying to do something similar in my own class, but of I do not have the context. I can get the context again, but it feels like complicated. There is a CommandGateway class, but it seems like is for sagas. Is there something similar to dispatch a command?

For the testing I am using mockito and I have something similar to this:

private GenericMessage mockMessage;

private CommandGateway gateway; // this is because it seems easy with this, but I am not using sagas


public void testOnMessage() throws Exception {
Account account = new Account();


//here I am missing the mock dispatch


Thanks in advance


tge CommandGateway isn’t just for Sagas. You can use it in any component.

As for testing, seems like you can use the Axon test fixtures (axon-test) module for this. Otherwise, your issue sounds more Spring (injection) related than Axon. Use @RunWith(Spring…Runner.class).



Thanks Allard,

Another question, what fixture implementation should I use? I see in one example that it is using AnnotatedSagaTestFixture
but I am not using sagas yet. Is there another Fixture class that I can use.

Thanks in advance

Try the org.axonframework.test.GivenWhenThenTestFixture. It can be used to test the behavior of Aggregates.