Home » Java » java – vert.x testing with TestNg-Exceptionshub

java – vert.x testing with TestNg-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

i am new to vert.x , i am trying to make unit testing by calling my webservices .
i need an example of testing webservices with testng.

when i did it with junit5 it works fine with the below code

@BeforeMethod
private static void deploy_verticle(Vertx vertx , VertxTestContext testContext) {
    vertx.deployVerticle(new MainVerticle(), testContext.completing());
}


@Test
public void getTestFalse(Vertx vertx , VertxTestContext testContext) throws InterruptedException {
    Assert.assertTrue("3".contains("tesdfsdf"));

    req = new HttpRequestEntityTest();
    req.setHost("localhost");
    req.setPort(8089);
    req.setUri("/api/data");
    utilTest = new RestUtilTest();
    response = new HttpResponseEntityTest();
    utilTest.getTestUtil(req, testContext.succeeding(response -> {
        testContext.verify(() -> {
            Assert.assertTrue(response.getBodyStr().contains("tesdfsdf"));

        });
        testContext.completeNow();

    }));
}


@AfterMethod
public void finish(Vertx vertx , VertxTestContext testContext) {

context.getVertx().close(context.getTestContext().succeeding(response -> {
    context.getTestContext().completeNow();
}));
}

but when i try to work with TestNg like this :

@SuppressWarnings("deprecation")
    @Test
    public void getTestIqvia() throws InterruptedException {
        Future<HttpResponseEntityTest> future = Future.future();
        response = new HttpResponseEntityTest();
        req = new HttpRequestEntityTest();
        req.setHost("localhost");
        req.setPort(8089);
        req.setUri("/api/data");
        utilTest = new RestUtilTest();

        utilTest.getTestUtil(req, response -> {
            System.out.println(response.result().getBodyStr());

            System.out.println(response.result().getBodyStr().contains("dsds"));
            Assert.assertTrue(response.result().getBodyStr().contains("fdsfds"));

        });

GetTestUtil Function :

public Future<HttpResponseEntityTest> getTestUtil(HttpRequestEntityTest req,
        Handler<AsyncResult<HttpResponseEntityTest>> resultHandler) {
    future = Future.future();

    WebClient client = WebClient.create(vertx);
    response = new HttpResponseEntityTest();
    client.get(req.getPort(), req.getHost(), req.getUri()).send(ar -> {
        if (ar.succeeded()) {

            response.setStatusCode(ar.result().statusCode());
            response.setBodyStr(ar.result().bodyAsString());
            future.complete(response);
            System.out.println("Received response with status code get" + response.getStatusCode());
            resultHandler.handle(Future.succeededFuture(response));

        } else {
            response.setErrorMsg(ar.cause().getMessage());
            System.out.println("Something went wrong " + ar.cause().getMessage());
            future.complete(response);

        }

    });

    return future;

} 

always sending to web client not entered in the arrow function ar and always test success because it doesn’t go to Assert function . except fewtimes in debugging mode only

can any one has ana example in testNG with Vert.x?? or what’s wrong in my code?

How to&Answers:

This happens because the getTestIqvia completes before the WebClient callback is invoked.

With jUnit5, the corresponding Vert.x test extension makes sure the test is not marked as passed until all the asynchronous assertions pass.

With TestNG you need to do this manually. In practice, you need to make the test wait until the future returned by getTestUtil is completed.