How to write Post, Get, Put, Delete method web client unit test for Vert.x for end-points

Mehrdad Dolatkhah :

I want to write some unit tests with Junit 5 for End-Points with a post, get, put and delete method in web client in Vertx 3.8. but I have some problem to write these tests. I visit this link but I need more help to write a unit test for these End-Points. may please share with me a sample for each request. I have a database connection that in init method with before each annotation configured.

@BeforeEach:

DeploymentOptions options = new DeploymentOptions()
            .setConfig(new JsonObject().put("http.port", port));
    vertx.deployVerticle(new HttpVerticle(configuration, vertx, operations), options, context.completing());

Thanks for your tips friends.

Vertx JUnit 5 integration

Edit:

in this site, I found a good sample for write unit tests in vert.x and then start writing my own test for end-points before service be alive and work on CI/CD. when I called a get end-point everything is ok. but when I want to try a Post end-point and use The English Language character test pass, but when I try Post end-point test with Persian Language Character, it won't pass and got this error : io.vertx.core.json.DecodeException: Failed to decode:Unrecognized token 't': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

@Test
public void testAddNewCountry(Vertx vertx, VertxTestContext context) {
    System.out.println(port);

    final String json = Json.encodePrettily(new Country("مالزی", true));
    final String length = Integer.toString(json.length());
    vertx.createHttpClient().post(port, "localhost", AllRoutes.COUNTRY)
            .putHeader("content-type", "application/json")
            .putHeader("content-length", length)
            .handler(response -> {
                response.bodyHandler(body -> {
                    response.statusCode();
                    context.completeNow();
                });
            })
            .write(json)
            .end();
}

so this site is good to help but in Post end-point I have a problem and I have not any put or delete sample. and I can't either use assert for check result. then I decide to ask a question for help on how I must write a unit test for Vert.x End-Points?

Erunafailaro :

I answered a similar question on vert.x and junit5 here.

A very nice tool that helps you testing code that contains http requests is MockWebServer. See a full code example in the linked answer.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=417614&siteId=1