AxonFramework:如何测试@EventHandler

时间:2018-09-17 18:24:50

标签: testing axon

我有这个组件,它通过RabbitMQ队列与其他服务集成:

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

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

}

我应该如何测试?

@Test
public void shouldReactToSomeOtherServiceEvent() {
    //TODO
}

1 个答案:

答案 0 :(得分:2)

最好的方法是在单元测试中实例化或注入事件处理程序类,实例化测试事件,然后简单地调用该方法。像这样:

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