How to test async vertx eventbus handlers without sending reply

Viewed 2812

I have two microservices written with java vertx. They communicate via eventbus. First one sends a message and second receives the message.

First idea: Only reply to Vertx eventbus messages in unit test.

I want to test that message is being processed without errors and therefore I write unit test on second microservice that checks the reply

eventbus.send("address", message, reply -> {
    if (reply.succeeded()) {
        context.async().complete();
    } else {
        context.fail();
    }});

Now I have to send reply in my consumer, but I want to do it only in test, I do not need to send replies in production. I do not want to consume cpu and network to send replies in production. So what I am looking for is something like this:

vertx.eventBus().consumer("address", handler -> {
    Boolean success = methodMayFail();
    if ( MY_CONTEXT_IS_TEST || HANDLER_IS_LOCAL_AS_I_ONLY_SENT_LOCAL_MESSAGES_FROM_TEST) {
        success ? handler.reply("ok") : handler.fail();
    }
});

Second idea came after cdelmas comment and is in my own answer

2 Answers
Related