Skip to content

Conversation

@thorlauridsen
Copy link
Owner

@thorlauridsen thorlauridsen commented Aug 24, 2025

Testcontainers allow us to spin up a temporary Docker image during tests. For this project, we will spin up a temporary PostgreSQL database to run the controller tests against a real PostgreSQL database instead of an in-memory H2 database. There are differences between PostgreSQL and H2 databases so if you plan to use a PostgreSQL database in production, it also makes sense to mimic this by using a PostgreSQL database in the tests. H2 databases are useful for quickly testing and running the system locally, but it is better to use Testcontainers to run a real PostgreSQL database for the controller tests.

Use Testcontainers for controller tests:

  • Add Gradle dependencies to local.versions.toml and api/build.gradle.kts:
    • org.springframework.boot:spring-boot-testcontainers
    • org.testcontainers:postgresql
  • Add TestContainerConfig which defines the PostgreSQL container and database settings.
  • Add annotations @Import(TestContainerConfig.class) and @ActiveProfiles("postgres") to CustomerControllerTest to ensure that a PostgreSQL database is used for the tests.
  • Add application-postgres.yml with connection settings for PostgreSQL.
  • Update README.md.

[Testcontainers](https://github.com/testcontainers) allow us to spin up a temporary Docker image during tests. For this project, we will spin up a temporary PostgreSQL database to run the controller tests against a real PostgreSQL database instead of an in-memory H2 database. There are differences between PostgreSQL and H2 databases so if you plan to use a PostgreSQL database in production, it also makes sense to mimic this by using a PostgreSQL database in the tests. H2 databases are useful for quickly testing and running the system locally, but it is better to use Testcontainers to run a real PostgreSQL database for the controller tests.

Use Testcontainers for controller tests:
- Add Gradle dependencies to `local.versions.toml` and `api/build.gradle.kts`:
  - `org.springframework.boot:spring-boot-testcontainers`
  - `org.testcontainers:postgresql `
- Add `TestContainerConfig` which defines the PostgreSQL container and database settings.
- Add annotations `@Import(TestContainerConfig::class)` and `@ActiveProfiles("postgres")` to `CustomerControllerTest` to ensure that a PostgreSQL database is used for the tests.
- Add `application-postgres.yml` with connection settings for PostgreSQL.
- Update `README.md`.
@thorlauridsen thorlauridsen self-assigned this Aug 24, 2025
@thorlauridsen thorlauridsen added documentation Improvements or additions to documentation enhancement New feature or request labels Aug 24, 2025
@thorlauridsen thorlauridsen marked this pull request as ready for review August 24, 2025 11:49
@thorlauridsen thorlauridsen merged commit a4393ea into main Aug 24, 2025
1 check passed
@thorlauridsen thorlauridsen deleted the use-testcontainers branch August 24, 2025 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants