This module extends capabilities of Vert.x Junit 5 providing the injection of Vert.x Web Client into your tests. It also provides an API to easily test your Web API using the Web Client.
Use it in your build
-
groupId:io.vertx -
artifactId:vertx-junit5-web-client -
version: (current Vert.x release or SNAPSHOT)
Use Web Client in your tests
To use the Web Client, you need to register the extension VertxWebClientExtension after the
VertxExtension. You can do both:
---
`link:../../apidocs/examples/MultipleAnnotationsTest.html[MultipleAnnotationsTest]`
---
Or
---
`link:../../apidocs/examples/SingleAnnotationTest.html[SingleAnnotationTest]`
---
Look at JUnit 5 documentation for more details
Now you can add WebClient as a test method parameter, like:
---
`link:../../apidocs/examples/SimpleTest.html#myTest-io.vertx.ext.web.client.WebClient-io.vertx.junit5.VertxTestContext-[myTest]`
---
Use WebClientOptions
To define WebClientOptions, you must create a public field in your test class
annotated with @WebClientOptionsInject:
---
`link:../../apidocs/examples/WithOptionsTest.html[WithOptionsTest]`
---
Testing Http requests
Testing Http requests is usually tedious, error prone and requires a lot of boilerplate.
If you want to test a request with WebClient this is what you usually do:
---
`link:../../apidocs/examples/TestRequestExample.html#testWithWebClient-io.vertx.ext.web.client.WebClient-io.vertx.junit5.VertxTestContext-[testWithWebClient]`
---
This module provides TestRequest, a wrapper of HttpRequest that simplifies
the creation of test requests and asserts on the responses. First, add a static import for all static methods in TestRequest.
Now you can rewrite the code in the previous example:
---
`link:../../apidocs/examples/TestRequestExample.html#testWithTestRequest-io.vertx.ext.web.client.WebClient-io.vertx.junit5.VertxTestContext-[testWithTestRequest]`
---
When the request is completed and there are no assertion failures completeNow is called, otherwise the test fails.
You can flag a Checkpoint more than completing the VertxTestContext:
---
`link:../../apidocs/examples/TestRequestExample.html#testWithTestRequestCheckpoint-io.vertx.ext.web.client.WebClient-io.vertx.junit5.VertxTestContext-[testWithTestRequestCheckpoint]`
---
You can wrap an already existing HttpRequest:
---
`link:../../apidocs/examples/TestRequestExample.html#testWithTestRequestWrapping-io.vertx.ext.web.client.WebClient-io.vertx.junit5.VertxTestContext-[testWithTestRequestWrapping]`
---
Every send method returns a Future, so you can use compose to execute different test requests sequentially:
---
`link:../../apidocs/examples/TestRequestExample.html#testWithTestRequestChaining-io.vertx.ext.web.client.WebClient-io.vertx.junit5.VertxTestContext-[testWithTestRequestChaining]`
---
You can create your custom assertions:
---
`link:../../apidocs/examples/TestRequestExample.html#testWithTestRequestCustomAssert-io.vertx.ext.web.client.WebClient-io.vertx.junit5.VertxTestContext-[testWithTestRequestCustomAssert]`
---
Look at TestRequest static methods for all available asserts.