2

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?

Mehrdad Dolatkhah
  • 668
  • 2
  • 9
  • 28
  • 1
    Why does this have a minus 1 vote? Please explain to the poster why his question is not up to standards. I think that the person is hinting to a lack of direct questioning. @Merhdad, can you ask your question more precise? It is quite vague to ask for tips in general when we don't know the context. – Quadrivics Feb 06 '20 at 08:26
  • I don't know why I got this minus point. sure I 'll explain it more – Mehrdad Dolatkhah Feb 06 '20 at 08:30
  • sounds like a http [charset issue](https://stackoverflow.com/questions/1808567/what-is-the-default-content-type-charset). Another header parameter may do (charset=utf-8). I mean, depending on what `Json.encodePrettily` produces. – Curiosa Globunznik Feb 06 '20 at 08:50
  • after added this section into header .putHeader("content-type", "application/json; text/html; charset=utf-8") io.vertx.core.json.DecodeException: Failed to decode:Unrecognized token 't': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false') – Mehrdad Dolatkhah Feb 06 '20 at 09:14
  • Does this answer your question? [Mocking a http response using vert.x rxjava2 and Mockito](https://stackoverflow.com/questions/59957813/mocking-a-http-response-using-vert-x-rxjava2-and-mockito) – Erunafailaro Feb 06 '20 at 19:57
  • 1
    If you're willing to test your web API, doing requests and performing assertions on response, we're working on this "assertions package" specifically designed for this purpose. Check it out here https://github.com/vert-x3/vertx-junit5/blob/master/vertx-junit5-web-client/src/main/java/examples/TestRequestExample.java . For now it's available only in 4.0.0-SNAPSHOT, but it will be included in next Vert.x 4 mileston. Disclaimer: I'm the mantainer of this package – Francesco Guardiani Feb 07 '20 at 10:06

1 Answers1

1

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.

Erunafailaro
  • 1,835
  • 16
  • 21