/just-another-testcontainer-integration

Backend application using Testcontainers for Integration Testing AWS S3, Azure Blob Storage, Kafka, MySQL, Egress calls including Chaos testing and Redis.

Primary LanguageJavaThe UnlicenseUnlicense

Just another Testcontainer integration

Just another Testcontainers integration workflow status

A reference proof-of-concept that leverages Testcontainers to execute integration tests in a Java Spring-boot backend application. The POC focuses on key modules including a Database, Cache, Cloud Storage Services, External HTTP calls including Chaos testing and a Message Broker to ensure their seamless integration.

Usage

Testcontainers has been used to test the below modules in the application:

AWS Simple Storage Service (S3)

  • Integration Test: AwsStorageServiceIT.java
  • Description: This test validates the integration between the application and the AWS S3 cloud storage service by utilizing Localstack. It ensures proper functionality for file upload, retrieval, and generation of Presigned-URLs for uploading and fetching objects with an expiration time constraint. Profile specific Beans are created in AwsS3Configuration.java

Azure Blob Storage

  • Integration Test: AzureStorageServiceIT.java
  • Description: This test validates the integration between the application and the Azure Blob Storage service using Azurite, an official emulator compatible against Azure Storage API. The test ensures proper functionality for file upload and verifies the scenarios of retrieving and deleting blobs from the provisioned container. A Generic container with Azurite v3 Docker Image is started as there is currently no official Testcontainers module for Java.

Kafka Consumer and Producer

  • Integration Test: CustomerRegisteredEventListenerIT.java
  • Description: This test validates the seamless integration between the application and the Kafka Message Broker. It verifies the application's ability to consume and produce messages from/to defined Kafka topic(s). Redpanda, a lightweight and ZooKeeper-free platform compatible with Apache Kafka, is utilized to execute the Kafka integration tests in the application. Reference Article for Redpanda can be viewed Here for more details.

MySQL and Flyway

  • Integration Test: PersistenceServiceIT.java
  • Description: This test verifies the integration between the backend and MySQL database, along with the execution of Flyway SQL scripts for database migration. It ensures the correct setup of the database schema and verifies the execution of basic CRUD operations against defined entities.

Redis

  • Integration Test: CacheServiceIT.java
  • Description: This test validates the integration between the application and the provisioned Redis Cache. It ensures the proper functioning of the caching mechanism and verifies the accurate storage and retrieval of cached data based on the configured time-to-live. To ensure compatibility with the main Spring context Bean, a password-protected container is started.

Outbound API Communication (Egress HTTP call)

  • Integration Test: EmailApiClientIT.java
  • Description: This test validates the egress/external HTTP call made by the application to send email notifications. The test utilizes MockServer to mock the server's response and verify the interaction with the server. The test covers scenarios such as successful email notification dispatch and failure scenarios with appropriate error handling.

Chaos Testing

  • Integration Test: EmailApiClientChaosIT.java
  • Description: This test validates the application's resilience to Network Failures and Timeouts during egress API communication which has been simulated by using Toxiproxy. The test covers scenarios such as handling socket timeout exceptions and proper error handling during API communication failures. RestTemplate has been specifically configured with low timeout durations for this purpose.

Local Setup and Prerequisites

To set up the project locally, ensure the following prerequisites are met:

  • Docker : The integration tests requires docker to be installed and running on local machines as well as on the servers where the project is being built. This workflow can be used as a reference.
  • Java 17 and Maven : Recommended to use Sdkman
sdk install java 17-open
sdk install maven

To run integration tests, execute the following command in the base directory:

mvn verify

Centralized TestContainer Extension

An alternative approach wherein a Centralized TestContainer Extension has been implemented to streamline the management and configuration of test containers across multiple test classes. This extension initializes and starts all the required test containers only once for the entire test suite.

The test classes are annotated with @ExtendWith(TestContainerExtension.class) which automates container initialization and configuration of necessary properties. This centralized approach simplifies the setup and management of test containers, promoting code reusability and reducing duplication across test classes.

Implementation Branch: feature/centralized-testcontainer-extension