Cegal tech blog

Automatic tests for webservice with RestAssured, WireMock and Testcontainers libraries

Written by Cegal Tech Advisors | Jan 19, 2018 4:01:00 PM

In this post, we will look at 3 libraries/tools for testing the end-points of web services.

Content

  1. Application under test
  2. RestAssured
    2.1. Definition
    2.2. Main features
    2.3. Use case (diagram)
  3. WireMock
    3.1. Definition
    3.2. Main features
    3.3. Use case (diagram)
  4. Testcontainers
    4.1. Definition
    4.2. Main features
    4.3. Use case (diagram)
    4.4. Additional (Awatility)
  5. Summary

1. Application under test

Application repository
The application is a card game RESTapi, built with: Spring-boot, kotlin, Netflix tech stack (Zuul, Ribbon, Eureka), and a micro-services architecture. The project is exam delivery for subject PG6100-1 Java enterprise-2 at Westerdals.

2. RestAssured

2.1. Definition

Library for testing and validation of REST APIs.

2.2. Main features

  • Maven / Gradle integration
  • Json- Xml-, path for simple parse
  • JVM languages support (Java, Kotlin, etc...)

In the current project, RestAssured is used in each module to perform HTTP calls and validate responses.

2.3. Use case

Create a new player.

Setup RestAssured configuration.

no.ern.game.player.controller.TestBase.kt


Test example.

no.ern.game.player.controller.PlayerControllerTest.kt

3. WireMock

WireMock is an HTTP mock server. Provide an opportunity to test services in isolation.

3.1. Definition

This library allows triggering outbound requests, mock target service, and stub responses. Wiremock provides a range of opportunities to test microservices in isolation from each other.

3.2. Main features

  • Mocking, stubbing, verifying, proxying
  • Support SSL (HTTPS)

3.3. Use case

Set up wiremock test base.

no.ern.game.player.controller.WiremockTestBase.kt

Kotlin did not support static when the application was developed (maybe it has been changed already), so we used a companion object instead.

Test example.

 

4. Testcontainers

4.1. Definition

In two words: tests in docker containers. Test containers offer opportunities to test -integration, -application layers, and UI tests(i.e., with selenium).

4.2. Main features

  • Simple access layer integration test: MySQL, PostgreSQL & Oracle.
  • Tests against the docker-compose environment
  • Containerized web browsers, compatible with Selenium
  • Opportunity to record video for UI tests
  • Generic containers (use own images)
  • Accessing containers from tests

4.3. Use case

We use this library for end-to-end tests, validation, and checking of the most important components in the application. Application deployed in docker-compose environment, using local docker-compose and generic containers.

  • Validate infrastructure (gateway, service discovery, and access)
  • Validate microservices security access
  • Validate main system features
  • Validate game logic processes

Setup docker-compose environment.

no.ern.game.e2etests.AuthFeatureIT


Test example.

no.ern.game.e2etests.AuthFeatureIT

 

4.4. Additional Awatility

Java library for sync async operations. It is a very convenient tool for writeing automated tests in an async environment.


We use it at:

  • Waiting until all nodes in the application are deployed, registered in service discovery, and be accessible. We wait max 4 minutes and while waiting call gateway with RestAssured GET /user in a loop until success, ignore any exceptions. Success symbolize that last microservice in application is UP and system run tests, if timeout riches than fail test suite
  • Async operations: message broker processing, node communications and etc...

5. Summary

That was a short overview of testing tools for web-services and applications with microservice architecture. RestAssured and WireMock libraries are well-known among Java developers. Testcontainers are something "new" (5 years history) to automate settings of containers environment. TestContainers run containers for automated tests and destroy them right after. Testcontainers library decreases integrated test complexity. And 'YES,' you do not need to install any more Oracle databases to run tests.

Links