vert-x3 / vertx-examples

Vert.x examples
Apache License 2.0
3.55k stars 2.09k forks source link

Junit non-REST test example #367

Open abelsromero opened 4 years ago

abelsromero commented 4 years ago

I am struggling to test a verticle isolated and all documentation and examples found only show how to test rest endpoints.

Something as simple as this, always fails with timeout

    @Rule
    public RunTestOnContext rule = new RunTestOnContext();

    @Rule
    public Timeout timeout = Timeout.seconds(5L);

    @Test
    public void should_run_verticle(TestContext context) {
        // given
        final String address = "hello";
        final String message = "world";

        // when
        final Async async = context.async();

        rule.vertx()
                .eventBus()
                .publish(address, message)
                .consumer(address, event -> {
                    context.assertEquals(message, event.body());
                    System.out.println(event.body());
                    async.complete();
                });

        async.awaitSuccess();
    }