How to start testing vert.x?

226 views
Skip to first unread message

Martin Gonzalez

unread,
Jul 6, 2019, 10:33:31 AM7/6/19
to vert.x

Hi everybody! 
I've been using vert.x a couple of months ago and it's really interesting! I'm trying to find a way to test the delivery part of my code (RoutingContext handlers).
I've seen in most pages that people just mock the RoutingContext class, but is this really the right path? I mean, mocking code that I don't own?
Also there are a lot of parts of the routing context that I would have to mock, for example the response object, the request object and so on.

My objective is to test my handlers receiving information that I want to test and I want to verify that the response was called with certain headers/body/status code etc.

How do you test your delivery part?

Thanks!

Francesco Guardiani

unread,
Jul 7, 2019, 5:13:13 AM7/7/19
to vert.x
I think that you can go through different paths: or you organize the code to have your business logic encapsulated in methods/classes that has as parameters objects simple to mock; or you do a complete test starting an http server that serves the router with all your business logic.

In the second case, I would proceed in this way. First of all, if you don't know, check out vertx-junit5, the module for testing. Then, on each test case:
  • In setUp function of the test, i start the verticle that bootraps the http server with all the logic
  • In test methods, i use vertx-web-client or vertx-core HttpClient to send requests
  • I assert requests result
If you are interested, in 3.8 vertx-junit5 will bring a new submodule called vertx-junit5-web-client that simplifies the creation of test requests and the asserts on it

Martin Gonzalez

unread,
Jul 7, 2019, 12:25:52 PM7/7/19
to vert.x
Well, vertx-junit5 sounds interesting but I don't know if it fits in my designed code. Perhaps you can give me a clue.
I have the following code:

In my Routes class (class where handler are registered into the router) I have the following line

router.use(MyHandler(Services.SomeService()))

And then I have my Handler class:

private const val PATH = "/some-resources"

class MyHandler(someService: SomeService) : Handler{

    override fun register(router: Router) {
        router.put(PATH).handler(this::handle)
    }

    private fun handle(context: RoutingContext) {
        // Do stuff with context
        val someParam context.getParam("someParam")

        val result = someService.doSomethingWith(someParam) //output is some string

        context
        .response
        .setStatusCode(200)
        .putHeader("Content-Type", "application/json")
        .end(result)
    }
}


I would like to test that the response has a statusCode 200, a Content-Type header and a body, but I guess vertx-junit5 will not work for this right?

Francesco Guardiani

unread,
Jul 9, 2019, 10:41:48 AM7/9/19
to vert.x
In this case or you mock everything to do unit tests of single handlers or you create the server, you mount the router and then with web client you do test requests. Then, with your favourite assertions library, you check the response of the requests
Reply all
Reply to author
Forward
0 new messages