Creating the test code

We have implemented our microservice, but it is extremely important to test it in order to improve its quality and make future code evolution easier.

Vert.x gives developers the opportunity to easily integrate JUnit in order to implement a strong test suite. In our example, we will implement an integration test in order to verify the good behaviour of our code, and to see it in action.

To use JUnit, you need to have these dependencies in your Maven pom.xml:

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-unit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit-platform-launcher.version}</version>
<scope>test</scope>
</dependency>

Then we should be ready to create our test class to verify the results of our APIs:

@ExtendWith(VertxExtension.class)
public class TestFootballPlayerVerticle {

@BeforeEach
void deploy_verticle(Vertx vertx, VertxTestContext testContext) {
vertx.deployVerticle(new FootballPlayerVerticle(), testContext.
succeeding(id -> testContext.completeNow()));
}

@Test
@DisplayName("Should start a Web Server on port 8080 and the GET all API"
+ "returns an array of 24 elements")
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS)
void findAll(Vertx vertx, VertxTestContext testContext) throws Throwable {
System.out.println("FIND ALL *****************");
vertx.createHttpClient().getNow(8080, "localhost", "/footballplayer",
response -> testContext.verify(() -> {
assertTrue(response.statusCode() == 200);
response.bodyHandler(body -> {
JsonArray array = new JsonArray(body);
assertTrue(23 == array.size());
testContext.completeNow();
});
}));
}

@Test
@DisplayName(
"Should start a Web Server on port 8080 and, using the POST API,"
+ "insert a new football player")
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS)
public void create(Vertx vertx, VertxTestContext context) {
System.out.println("CREATE *****************");
final String json = Json.encodePrettily(new FootballPlayer(null, "Mauro", "Vocale", 38,
"Juventus", "central midfielder", new BigInteger("100")));
final String length = Integer.toString(json.length());

vertx.createHttpClient().post(8080, "localhost", "/footballplayer/save")
.putHeader("content-type", "application/json")
.putHeader("content-length", length)
.handler(response -> {
assertTrue(response.statusCode() == 201);
assertTrue(response.headers().get("content-type").contains("application/json"));
response.bodyHandler(body -> {
final FootballPlayer footballPlayer = Json.decodeValue(
body.toString(), FootballPlayer.class);
assertTrue(footballPlayer.getName().equalsIgnoreCase("Mauro"));
assertTrue(footballPlayer.getAge() == 38);
assertTrue(footballPlayer.getId() != null);
context.completeNow();
});
}).write(json).end();
}
...

}

To run our test, we used the VertxExtension class, which allows the injection of Vert.x and VertxTestContext parameters, as well as an automatic life cycle on the VertxTestContext instance.

With the @BeforeEach annotation, we set our routine to execute deployment of the verticle class before the execution on every single test, in order to have the data needed for their execution.

Then, in every method, we created an HTTP client, using the vertx.createHttpClient() method, against which we perform the HTTP verb operation (GET, POST, PUT, or DELETE).

We set the parameter needed for the invocation and verified the assertion.

Now you are ready to launch the test with this command:

$ mvn test

You will see that all tests have passed:

[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.677 s - in com.packtpub.vertx.footballplayermicroservice.TestFootballPlayerVerticle
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset