Search code examples
kotlinvert.xvertx-verticle

vertx-web-client testing always times out, even though the assertion succeeds


Problem:

I am using the vertx-web-graphql dependency to run a graphql-server and now I want to test that said server.

But the vertx-web-client testing always times out, even after the assertion that is just before the testContext.completeNow() does not fail.

Versions:

  • Kotlin (1.7.20)

  • vertx(core, graphql, postgres, web-client, vertx-junit5) (4.3.4)

Problem-Code:

@ExtendWith(VertxExtension::class)
class GraphQlVerticleTest {

    @BeforeEach
    fun `deploy GraphQL Verticle`(vertx: Vertx, testContext: VertxTestContext) {
      vertx.deployVerticle(GraphQlVerticle(setupPostgreSqlClient(vertx), setupJWTAuthenticationProvider(vertx)),
                           testContext.succeedingThenComplete())
    }

    @Test
    fun `Wrong Login Credentials cause Login Failed message`(vertx: Vertx, testContext: VertxTestContext) {
        val webClient: WebClient = WebClient.create(vertx)
      
        testContext.assertComplete(webClient.post(graphQLPort, graphQLHostName, graphQLURI)
                                    .putHeader("Content-Type", "application/json")
                                    .sendJsonObject(createLoginAttemptQueryAsJsonObject(wrongEmail, wrongPassword)))
          .onComplete { asyncResult ->
              assertThat(asyncResult.result().bodyAsString()).isEqualTo(loginFailedResponse)
              println("hello I executed")
              testContext.completeNow()
          }
    }

    @AfterEach
    fun `check that the Verticle is still there`(vertx: Vertx, testContext: VertxTestContext) {
        assertThat(vertx.deploymentIDs()).isNotEmpty.hasSize(1)
    }
}

The Verticle and some of the functions don't really matter, as the Assert Statement does not fail, but for some reason the testContext never gets completed and then the Test times out.

I used the testContext.assertComplete(...).onComplete(...) in all my other Vertx-Tests, but with the Web-Client it just does not work. As far as I can tell, in this scenario the test-context has to be completed in different way, but I just can't figure out how.

Any help would be appreciated 🙂. And please tell me if there is anything to make the question clearer, as I have never asked anything on StackOverflow before.

What I tried:

I tried placing the the assertion and testContext completion statement into a testContext.verify {} block, but that didn't work either. I read through the vertx-junit5 & vertx-web-client documentation, but there wasn't anything there concerning web-client testing. And my search didn't had any fruitful results either.

What I expected:

That the test would complete after the assertion statement succeeded, like it did in all my other vertx-tests.


Solution

  • Found the problem: you must complete the testContext in the method annotated with @AfterEach:

    @AfterEach
    fun `check that Verticle is still there`(vertx: Vertx, testContext: VertxTestContext) {
        assertThat(vertx.deploymentIDs()).isNotEmpty.hasSize(1)
        testContext.completeNow()
    }
    

    Or simply not inject it:

    @AfterEach
    fun `check that Verticle is still there`(vertx: Vertx) {
        assertThat(vertx.deploymentIDs()).isNotEmpty.hasSize(1)
    }
    

    In both cases, the test passes.

    Originally posted by @tsegismont in https://github.com/eclipse-vertx/vertx-junit5/issues/122#issuecomment-1315619893