4

Does anyone know of a way to spy on the Vert.x event bus in Quarkus tests? Ideally I'd like to assert that during a method in the service layer we are sending an event to the correct address, but does anyone know if this is possible?

If I just try to use @InjectMock I get the following error

io.vertx.core.eventbus.impl.EventBusImpl@5769679b is not a normal scoped CDI bean, make sure the bean is a normal scope like @ApplicationScoped or @RequestScoped
bnjns
  • 41
  • 4
  • I have no experience with vert.x in particular, but wouldn't it possible to register a (test-scoped) bean, listening on the `EventBus` and spy on this bean instead? – Turing85 Dec 29 '20 at 22:49
  • Yeah that definitely works - I also guess you can also just spy on the actual bean that consumes the event (we use the eventbus for some async processing). Not as ideal, but does the job well enough. Thanks. – bnjns Dec 29 '20 at 22:55
  • Depends on what you want to verify, doesn't it? The end result is that the listener(s) get called. The means are provided by vert.x. I would argue that we can trust vert.x to do its job, hence we do not need to test its behaviour. The desired behaviour is that the listeners are called. A more "complete" approach would be to verify the actions triggered by this listeners (which may result in an integration- or even end-to-end test). – Turing85 Dec 29 '20 at 23:12
  • Yeah, fair point. We were also going to test that the correct actions are performed once an event is consumed, but that would be in a separate test suite to avoid the service tests becoming bloated. – bnjns Dec 29 '20 at 23:38

2 Answers2

1

As suggested here https://github.com/quarkusio/quarkus/issues/8983

@InjectMock(convertScopes = true)

should solve your problem. If convertScopes is true, then Quarkus will change Singleton to ApplicationScoped to make the bean mockable.

NOTE: the documentation states that this is an advanced setting and should only be used if you don't rely on the differences between Singleton and ApplicationScoped beans

RugUrmet
  • 13
  • 4
0

I solved this Problem, by creating an ApplicationScoped Delegate around the EventBus. This Delegate can be mocked and inspected as a normal bean in Quarkus. All the Beans which were using the EventBus directly need to use the EventBusDelegate instead. In your test you can use the @InjectMock annotation to inject the EventBusDelegate mocked.

Jakudaba
  • 131
  • 1
  • 3