Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

vertx-howtos/test-web-application-howto

Repository files navigation

Test your Web application with Vert.x & JUnit 5

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.

What you will build

You will build a Web application using Vert.x Web and test it with JUnit 5 doing HTTP requests.

What you need

  • A text editor or IDE

  • Java 8 or higher

  • Maven

Create a project

Here is the content of the pom.xml file you should be using:

Maven pom.xml
link:pom.xml[role=include]

The pom contains JUnit 5 dependencies, vertx-junit5 and reactiverse-junit5-web-client modules

Create the Web application Verticle

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 the Web application test

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.

Setup and Tear down the test

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]

Configure the Web Client

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.

Test get pet

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.

Test get pet error codes

To test the Bad Request and Page not found responses:

link:src/test/java/io/vertx/howtos/web/test/WebApplicationTest.java[role=include]

Post a pet and assert that is correctly added

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.

Complete code

You can find the complete source code of the test class at WebApplicationTest on this how-to repo.

Summary

This how-to explained to you:

  1. How to inject WebClient into your unit tests

  2. How to setup and tear down your HTTP web application in your unit tests

  3. How to do test requests

  4. How to assert HTTP responses

  5. How to to multiple test requests in the same unit test

About

Test your Web application with Vert.x & JUnit 5

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •