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
}
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"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With