{"id":23122314,"url":"https://github.com/andrecaiado/spring-boot-testcontainers","last_synced_at":"2026-05-05T12:32:24.761Z","repository":{"id":253804082,"uuid":"844570751","full_name":"andrecaiado/spring-boot-testcontainers","owner":"andrecaiado","description":"A Spring Boot project to demonstrate the use of testcontainers to implement integration tests with external systems","archived":false,"fork":false,"pushed_at":"2024-08-22T10:06:50.000Z","size":200,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T04:14:59.458Z","etag":null,"topics":["integration-testing","postgresql","rabbitmq","spring-boot","testcontainers"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andrecaiado.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-19T14:25:43.000Z","updated_at":"2024-08-22T10:06:53.000Z","dependencies_parsed_at":"2024-12-17T07:19:19.428Z","dependency_job_id":"77afb826-22ab-48b3-b9c3-71d7c49e63c0","html_url":"https://github.com/andrecaiado/spring-boot-testcontainers","commit_stats":null,"previous_names":["andrecaiado/spring-boot-testcontainers"],"tags_count":0,"template":false,"template_full_name":"andrecaiado/spring-boot-template","purl":"pkg:github/andrecaiado/spring-boot-testcontainers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrecaiado%2Fspring-boot-testcontainers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrecaiado%2Fspring-boot-testcontainers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrecaiado%2Fspring-boot-testcontainers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrecaiado%2Fspring-boot-testcontainers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrecaiado","download_url":"https://codeload.github.com/andrecaiado/spring-boot-testcontainers/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrecaiado%2Fspring-boot-testcontainers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32649545,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-05T11:29:49.557Z","status":"ssl_error","status_checked_at":"2026-05-05T11:29:48.587Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["integration-testing","postgresql","rabbitmq","spring-boot","testcontainers"],"created_at":"2024-12-17T07:18:42.464Z","updated_at":"2026-05-05T12:32:24.744Z","avatar_url":"https://github.com/andrecaiado.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Spring Boot Testcontainers project\n\nA Spring Boot project to demonstrate the use of testcontainers to implement integration tests with external systems\n\nThis README file will focus on the testcontainers features implementation. For more information about the other project features, please refer to the project template:  [spring-boot-template](https://github.com/andrecaiado/spring-boot-template).\n\n# Contents\n\n- [Getting Started](#getting-started)\n    - [Main features](#main-features)\n    - [Dependencies and requirements](#dependencies-and-requirements)\n    - [Run the project](#run-the-project)\n    - [Run the tests](#run-the-tests)\n- [System Under Test (SUT)](#system-under-test-sut)\n- [External services configuration](#external-services-configuration)\n    - [RabbitMQ configuration](#rabbitmq-configuration)\n    - [Postgres configuration](#postgres-configuration)\n- [Testcontainers configuration](#testcontainers-configuration)\n- [Tests implementation](#tests-implementation)\n    - [RabbitMQ tests](#rabbitmq-tests)\n    - [Postgres tests](#postgres-tests)\n\n# Getting Started\n\nThis section provides an overview of the main features, necessary dependencies, and step-by-step instructions to help you get the application up and running quickly.\n\n## Main features\n\nThe main features of this project are:\n- Message consumer from a RabbitMQ queue\n- Write the consumed messages to a Postgres database\n- Integration tests with external services using Testcontainers\n- Docker compose to launch external services (Postgres and RabbitMQ)\n\n## Dependencies and requirements\n\nThe following dependency are required to implement this project's main features:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n    \u003cartifactId\u003espring-boot-starter-amqp\u003c/artifactId\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.amqp\u003c/groupId\u003e\n    \u003cartifactId\u003espring-rabbit-test\u003c/artifactId\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.testcontainers\u003c/groupId\u003e\n    \u003cartifactId\u003ejunit-jupiter\u003c/artifactId\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n    \u003cartifactId\u003espring-boot-testcontainers\u003c/artifactId\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.testcontainers\u003c/groupId\u003e\n    \u003cartifactId\u003erabbitmq\u003c/artifactId\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.testcontainers\u003c/groupId\u003e\n    \u003cartifactId\u003epostgresql\u003c/artifactId\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n```\n\nTo launch and run the external services (Postgres, Prometheus and Grafana) in Docker containers, the following requirements are needed:\n\n- [Docker](https://docs.docker.com/get-docker/)\n- [Docker Compose](https://docs.docker.com/compose/install/)\n\nTo run the project, the requirements are:\n\n- [Java 17 (or higher)](https://www.oracle.com/java/technologies/javase-jdk17-downloads.html)\n- [Maven](https://maven.apache.org/download.cgi)\n\n## Run the project\n\nTo run the project, execute the following command:\n\n```shell\nmvn spring-boot:run\n```\n\n## Run the integration tests\n\nTo run the tests, execute the following command:\n\n```shell\nmvn failsafe:integration-test\n```\n\nThe application will be available at [http://localhost:8080](http://localhost:8080).\n\nThe external services (Postgres and RabbitMQ) configured in the [docker-compose.yaml](docker-compose.yaml) file will automatically be launched due to the `spring-boot-docker-compose` dependency.\n\nIn any case, the external services can also be launched manually by running on of the following command:\n\n```shell\n# Start all the external services\ndocker compose up -d\n```\n\n```shell\n# Start a specific external service \ndocker compose up -d \u003cservice-name\u003e\n# Replace \u003cservice-name\u003e with the service you want to start (postgres or rabbitmq)\n```\n\n# System Under Test (SUT)\n\nThe System Under Test (SUT) is a simple Spring Boot application with the following use cases:\n\n- Consume messages from a RabbitMQ queue and write them to a Postgres database.\n- Exposes endpoints to execute CRUD operations on the database.\n\n![sut.png](src%2Fmain%2Fresources%2Fsut.png)\n\nThe integration tests to be implemented will use Testcontainers to launch the external services (Postgres and RabbitMQ) and test the application's interaction with these services.\n\nThe tests will cover the following scenarios:\n\n- The application's ability to consume messages from a RabbitMQ queue and write them to a Postgres database.\n\n- The application's ability to execute CRUD operations on the database.\n\n# External services configuration\n\nThis section provides an overview of the configuration of the external services (Postgres and RabbitMQ).\n\nBecause we are using `spring-boot-docker-compose` dependency, Spring Boot will take care of the following:\n\n- When the application is started, the services defined in the [docker-compose.yaml](docker-compose.yaml) file will be automatically started.\n\n- The connection details for the services will be created and made available to the application, that's why we don't need to specify any connection details in the application configuration files.\n\n**Note:** This also applies to the integration tests. The Testcontainers will be launched before the tests are executed, and the connection details will be made available to the tests.\n\nFor more details on the `spring-boot-docker-compose` dependency, please refer to the [Docker Compose Support documentation](https://spring.io/blog/2023/06/21/docker-compose-support-in-spring-boot-3-1).\n\n## RabbitMQ configuration\n\nThe RabbitMQ service was added and configured in the [docker-compose.yaml](docker-compose.yaml). \n\nOn startup, 2 configuration files are mounted to the container:\n\n- [rabbitmq.config](docker/rabbitmq/rabbitmq.config)\n- [definitions.json](docker/rabbitmq/definitions.json)\n\nThe `rabbitmq.config` file is used to specify the location of the `definitions.json` file.\n\nThe `definitions.json` file contains the RabbitMQ configuration settings, such as:\n\n- Users and permissions\n- Virtual hosts\n- Exchanges\n- Queues\n- Bindings\n\nThe RabbitMQ management console is available at [http://localhost:15672](http://localhost:15672) with the default credentials:\n\n- Username: guest\n- Password: guest\n\nThe application connection to the RabbitMQ service and the RabbitMQ queues, exchanges, and bindings are configured in:\n\n- The [application.yml](src/main/resources/application.yml) file for the main application.\n\n- The [application-integration-test.yml](src/test/resources/application-integration-test.yml) file for the integration tests.\n\n## Postgres configuration\n\nThe Postgres service was added and configured in the [docker-compose.yaml](docker-compose.yaml).\n\n# Testcontainers configuration\n\nThe Testcontainers were configured at the tests level. \n\nThe following annotations were used to configure the Testcontainers:\n\n- The `@Testcontainers` annotation is used to enable the Testcontainers support in the test class.\n\n- The `@ExtendWith(OutputCaptureExtension.class)` annotation is used to capture the output of the application's logs.\n\n- The `@Container` annotation is used to define the Testcontainers that will be launched before the tests are executed.\n\n- The `@ServiceConnection` discovers the type of container that is annotated and creates a ConnectionDetails bean that can be used to connect to the service. The details of the connection are, once again, made available by the `spring-boot-docker-compose` dependency.\n\nBelow is an example of the configuration of the RabbitMQ and Postgres containers that can be found in the [EmployeeRabbitMQIT.java](src%2Fintegration-test%2Fjava%2Fcom.example.springboottemplate%2FEmployeeRabbitMQIT.java) file:\n\n```java\n@ActiveProfiles(\"integration-test\")\n@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)\n@Testcontainers\n@ExtendWith(OutputCaptureExtension.class)\npublic class EmployeeRabbitMQIT {\n    \n    ...\n\n    @Container\n    @ServiceConnection\n    static PostgreSQLContainer\u003c?\u003e postgres = new PostgreSQLContainer\u003c\u003e(\n            \"postgres:16.3-alpine\"\n    );\n\n    @Container\n    @ServiceConnection\n    static RabbitMQContainer rabbitMQContainer = new RabbitMQContainer(\n            \"rabbitmq:3.13.6-management-alpine\"\n    ).withCopyFileToContainer(\n            MountableFile.forClasspathResource(\"rabbitmq/rabbitmq.conf\"),\n            \"/etc/rabbitmq/rabbitmq.conf\"\n    ).withCopyFileToContainer(\n            MountableFile.forClasspathResource(\"rabbitmq/definitions.json\"),\n            \"/etc/rabbitmq/definitions.json\"\n    );\n    \n    ...\n}\n```\n\n\n\n# Tests implementation\n\nThis section provides an overview of the tests implemented to validate the application's interaction with the external services (RabbitMQ and Postgres).\n\n## RabbitMQ tests\n\nTo test the application's ability to consume messages from a RabbitMQ queue, the implemented test has the following steps:\n\n1. A message is sent to the RabbitMQ queue.\n\n2. The test waits for the message to be consumed by the application and verifies if specific log messages are printed to the console.\n\nBelow is an example of the test implementation that can be found in the [EmployeeRabbitMQIT.java](src/test/java/com/acaiado/employeerabbitmq/EmployeeRabbitMQIT.java) file:\n\n```java\n@Test\nvoid testCreateEmployee(CapturedOutput output) {\n    CreateUpdateEmployeeDto createUpdateEmployeeDto = getCreateUpdateEmployeeDto();\n\n    producer.sendMessageCreateEmployee(createUpdateEmployeeDto);\n\n    Pattern pattern = Pattern.compile(\"\\\\w*Received message with payload: CreateUpdateEmployeeDto\\\\(firstName=John, lastName=Doe, age=30, designation=Software Engineer, phoneNumber=1234567890, joinedOn=2021-01-01, address=123, Baker Street, London, dateOfBirth=1979-01-01\\\\) on \\\\d{4}-\\\\d{2}-\\\\d{2}T\\\\d{2}:\\\\d{2}:\\\\d{2}\\\\.\\\\d{3}\\\\w*\");\n    await().atMost(10, TimeUnit.SECONDS)\n            .until(findPatternInOutput(output, pattern), is(true));\n\n    Pattern pattern2 = Pattern.compile(\"\\\\w*Employee with id: \\\\d+ saved successfully\\\\w*\");\n    await().atMost(10, TimeUnit.SECONDS)\n            .until(findPatternInOutput(output, pattern2), is(true));\n}\n\nprivate Callable\u003cBoolean\u003e findPatternInOutput(CapturedOutput output, Pattern pattern) {\n    return () -\u003e pattern.matcher(output.getOut()).find();\n}\n```\n\n## Postgres tests\n\nTo test the application's ability to execute CRUD operations on the database, the implemented test has the following steps:\n\n1. An employee is created in the database.\n2. The test verifies the body of the response returned by the application when the employee is created.\n\nThe test implementation that can be found in the [EmployeePostgresIT.java](src/test/java/com/acaiado/employeerabbitmq/EmployeePostgresIT.java) file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrecaiado%2Fspring-boot-testcontainers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrecaiado%2Fspring-boot-testcontainers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrecaiado%2Fspring-boot-testcontainers/lists"}