Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AxonFramework: How to test @EventHandler

Tags:

testing

axon

I have this component which integrates with other services through a RabbitMQ queue:

@Component
@ProcessingGroup("amqpProcessor")
public class ExternalEventsHandler {

   @EventHandler
    public void on(SomeOtherServiceEvent event) {
        // Dispatches some command 
    }

}

How should I test this?

@Test
public void shouldReactToSomeOtherServiceEvent() {
    //TODO
}
like image 433
Marcos J.C Kichel Avatar asked Oct 14 '25 09:10

Marcos J.C Kichel


1 Answers

The best way is just to instantiate or inject your event handler class in the unit test, instantiate a test event, and simply call the method. Something like this:

    @Mock
    private FooRepository fooRepository;

    private FooEventHandler fooEventHandler;

    @Before
    public void before() {
        fooEventHandler = new FooEventHandler(fooRepository);
    }

    @Test
    public void createFoo() {
        fooEventHandler.createFoo(new FooCreatedEvent("fooId");

        ArgumentCaptor<Foo> argument = ArgumentCaptor.forClass(Foo.class);
        verify(fooRepository, times(1)).save(argument.capture());
        assertTrue(argument.getValue().getId(), "fooId"));
    }
like image 106
Mzzl Avatar answered Oct 18 '25 03:10

Mzzl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!