3
votes

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
}
1
What is being suggested by @Mzzl is currently the best and pragmatic solution you'v got to test an Event Handler. It is just a function after all, so just call it. AxonIQ has slated an issue to provide 'Projection Testing', so eventually the framework will provide a nicer approach to testing Event Handlers.Steven

1 Answers

2
votes

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