Dominik Sandjaja
Dominik Sandjaja

Reputation: 6476

Vertx unit test never finishes

I have a vertx unit test with vertx version 3.3.2 and JUnit version 4.12. The following unit test never finishes:

@RunWith(VertxUnitRunner.class)
public class SomeVerticleTest {

    private Vertx vertx;

    @Before
    public void before(TestContext context) {
        vertx = Vertx.vertx();
        vertx.deployVerticle(SomeVerticle.class.getName(), r -> context.asyncAssertSuccess()); // problem is in this line, see answer for solution!
    }

    @Test
    public void doStuff(TestContext context) {
        String encodedMessage = Json.encode("myMessage");
        Async async = context.async();
        vertx.eventBus().send("someVerticle", encodedMessage, asyncResult -> {
            // some context.asserts on the message reply
            async.complete(); // this line is executed, I can see it in the debugger
        });
    }
}

The test itself runs fine, the message is sent, correctly processed by the handler in SomeVerticle and the reply is created and sent correctly.

But: Although the method doStuff is executed and its success is published to the Async object, the test just runs forever, it never ends. This can be verified by adding an after-test method, annotated with @After which is never executed.

I have tried to find the error for two hours now, but I simply don't know what exactly the problem might be, so I would be happy about any assistance.

[update] After two minutes, the test fails with a timeout.

[update 2] I found the problem and put it into a separate answer.

Upvotes: 3

Views: 2278

Answers (1)

Dominik Sandjaja
Dominik Sandjaja

Reputation: 6476

The answer is not within the actual testing method, but in the setup.

The following statement does NOT complete:

vertx.deployVerticle(SomeVerticle.class.getName(), r -> context.asyncAssertSuccess());

To get it actually working, we need to use the Async construct as mentioned in the docs here and here.

A correct statement which leads to a working test is as follows:

vertx.deployVerticle(SomeVerticle.class.getName(), context.asyncAssertSuccess()); // note the missing r-> 

I hope this answer helps someone being stuck with a similar problem.

Upvotes: 7

Related Questions