Skip to content

jaspervz/testcontainers-zio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Testcontainers ZIO

This is a sample project that shows how to use Testcontainers for (integration) tests in combination with ZIO.

Testcontainers

Testcontainers provides a way to run a Docker container with e.g. a database for testing. It is also possible to run a docker-compose for testing.

If the Docker container exposes a port, it will map this exposed port to a free port number on the external host, avoiding port conflicts. In your test you can retrieve the mapping port number so you can connect to the Docker container(s).

Sample project

The sample project is a simple REST API for to do items with a create, delete, get, and get all endpoint.

Besides that, it contains the example AerospikeContainer on how to use Testcontainers' GenericContainer for when there is no container available in the Testcontainers project.

Repository

The TodoRepository is responsible for storing the to do's in the database. TodoRepositoryTest contains the tests for the repository. There are different Specs that use TodoRepositoryTest, each injecting different layers for the database:

Spec Description
TodoRepositoryMySqlLayerSharedSpec uses a shared MySQL Docker container for the tests
TodoRepositoryMySqlLayerNonSharedSpec uses a non-shared MySQL Docker container for the test, this means that for every test a new Docker container is started
TodoRepositoryH2Spec uses a H2 database instead of a Docker container, which is faster, but only an emulation of MySQL

MysqlDockerContainer shows how to start a MySQL docker container and obtain the correct jdbc connection url, username, and password to connect to the MySQL instance.

TestDatabase contains two test layers: one for MySQL and one for H2, to be able to switch easily between the two test databases.

Service

The TodoService is a lightweight service that doesn't do a lot, it is added to show how to test such a service that depends on a repository that depends on a database without requiring a database. This is done in TodoServiceSpec using ZIO mock.

Api and application

The TodoApi defines the routes for the REST API and TodoApp is the application.

Integration test

The integration test isn't very extensive, it is meant to show how to test the entire application using a docker compose.

The command sbt integrationTest will build a Docker container of the application and will run the test(s) in the integration-test project.

TodoAppSpec tests the application. DockerCompose starts the docker compose file, which contains the application and the database. It retrieves the mapped host and port and composes the URL for the application so we can use a HTTP client to connect the application's REST end points.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages