java - Unit test consumers in Vertx


Keywords:java 


Question: 

I have a snippet of code that I want to unit test.

this.vertx.eventBus().consumer(VERTICLE_ID).toObservable()
        .subscribe(msg -> doSomethingCool());

and my consumer method:

 private void doSomethingCool(){ 
  // Some cool stuff.
 }

Now I want to unit test doSomethingCool() without using powermockito (I want to have code coverage) and I dont want to make my method visible (public). How can I do that? Any hook in vertx to do that ?


1 Answer: 

It actually hard to tell how you should write your test if nothing is known about the purpose of doSomethingCool

  • Does it return a value? (i.e. via msg.reply())
  • Does it modify state of the Verticle? or global state?
  • Does it make a downstream call?
  • Does your method invoke a handler once it's done with whatever it does?

A unit test should verify an observable result. So write your unit test to verify one of these outcomes. In case a handler is invoked, you could work with the vertxunit TestContext and count down an async.

... and stay away from Powermockito.