Warning
|
This repository has been archived because the reactiverse-junit5-web-client is no longer relevant in Vert.x 5.
Consider using Future.expecting with HTTP expectations.
|
This document will show you how to test your web application with Vert.x & JUnit 5.
You will build a Web application using Vert.x Web and test it with JUnit 5 doing HTTP requests.
Here is the content of the pom.xml
file you should be using:
pom.xml
link:pom.xml[role=include]
The pom contains JUnit 5 dependencies, vertx-junit5 and reactiverse-junit5-web-client modules
The web application will have two endpoints:
-
GET /pet/:id
-
POST /pet
GET /pet/:id
serves a Pet with a specific id:
link:src/main/java/io/vertx/howtos/web/test/WebApplicationVerticle.java[role=include]
POST /pet
adds a new Pet to the collection:
link:src/main/java/io/vertx/howtos/web/test/WebApplicationVerticle.java[role=include]
To start the HTTP server:
link:src/main/java/io/vertx/howtos/web/test/WebApplicationVerticle.java[role=include]
You can find the complete source code of the web application at WebApplicationVerticle
on this how-to repo.
Create a new test class called WebApplicationTest
and add the following annotation to the class:
link:src/test/java/io/vertx/howtos/web/test/WebApplicationTest.java[role=include]
This ensures that JUnit 5 runs the test inside Vert.x and injects the WebClient
, which is used for doing HTTP requests to the web application.
To setup and tear down the test, deploy and undeploy the WebApplicationVerticle
:
link:src/test/java/io/vertx/howtos/web/test/WebApplicationTest.java[role=include]
link:src/test/java/io/vertx/howtos/web/test/WebApplicationTest.java[role=include]
Since the test server will be available during the tests at localhost:9000
, you can configure the injected WebClient
using @WebClientOptionsInject
:
link:src/test/java/io/vertx/howtos/web/test/WebApplicationTest.java[role=include]
This ensures that all WebClient
instances injected are properly configured to send requests to the application under test.
To test the get pet endpoint:
link:src/test/java/io/vertx/howtos/web/test/WebApplicationTest.java[role=include]
The testRequest
method creates a TestRequest
instance, where you can add asserts about HTTP response.
In this case, asserts about the status code, the x-pet-id
header and body are added.
After the response is received, if the assertions are correct the test completes, otherwise it fails.
To test the Bad Request and Page not found responses:
link:src/test/java/io/vertx/howtos/web/test/WebApplicationTest.java[role=include]
To test if adding a pet works, first add the pet and then get it with another request:
link:src/test/java/io/vertx/howtos/web/test/WebApplicationTest.java[role=include]
When you pass a Checkpoint
to send*
methods of TestRequest
, They don’t complete the VertxTestContext
but just flag the Checkpoint
.
For more details on VertxTestContext
and Checkpoint
, look at Vertx JUnit 5 documentation.
TestRequest.send*
methods return a Future
that is completed after all assertions pass, so you can execute another request after the first one.
You can find the complete source code of the test class at WebApplicationTest
on this how-to repo.
This how-to explained to you:
-
How to inject
WebClient
into your unit tests -
How to setup and tear down your HTTP web application in your unit tests
-
How to do test requests
-
How to assert HTTP responses
-
How to to multiple test requests in the same unit test