|
1 | | -# Testcontainers |
| 1 | +Testcontainers for Java |
2 | 2 |
|
3 | | -[](https://maven-badges.herokuapp.com/maven-central/org.testcontainers/testcontainers) |
| 3 | +Testcontainers is an open-source Java library that makes it easy to write integration tests with real dependencies |
| 4 | +java.testcontainers.org |
| 5 | +. It lets you run “throwaway” Docker containers (for databases, web browsers, message brokers, etc.) during JUnit or Spock tests, without manual setup. You simply declare the test dependencies in code and Testcontainers handles starting and stopping the containers automatically |
| 6 | +testcontainers.com |
| 7 | +. This means there’s no need for fixed ports, environment configuration, or complex mocks – all you need is Docker |
| 8 | +testcontainers.com |
| 9 | +. Testcontainers works with popular JVM test frameworks (e.g. JUnit 4, JUnit 5/Jupiter, Spock) |
| 10 | +java.testcontainers.org |
| 11 | +. |
| 12 | +Key Features |
4 | 13 |
|
5 | | -[](https://app.netlify.com/sites/testcontainers/deploys) |
| 14 | + Real containerized dependencies: Enables using actual services (e.g. MySQL, PostgreSQL, Oracle, Redis, Kafka, etc.) in tests, started from Docker images. For example, you can run a PostgreSQL or MySQL container to test your data layer against a real database |
| 15 | + java.testcontainers.org |
| 16 | + . |
6 | 17 |
|
7 | | -[](https://github.com/codespaces/new?hide_repo_select=true&ref=main&repo=33816473&machine=standardLinux32gb&devcontainer_path=.devcontainer%2Fdevcontainer.json&location=EastUs) |
| 18 | + Application integration tests: Run your application (or parts of it) with its dependencies (databases, message queues, caches) in isolated containers, ensuring tests use a clean, known environment |
| 19 | + java.testcontainers.org |
| 20 | + . |
8 | 21 |
|
9 | | -[](https://ge.testcontainers.org/scans) |
| 22 | + UI/Acceptance tests: Supports containerized Selenium-compatible browsers (Chrome, Firefox, etc.) for UI tests, with each test getting a fresh browser instance (including optional video recording) |
| 23 | + java.testcontainers.org |
| 24 | + . |
10 | 25 |
|
11 | | -> Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container. |
| 26 | + Extensible modules: Many specialized modules are available (for databases, queues, cloud services, etc.). You can also use GenericContainer to define any Docker image as a test resource, or write custom containers. Testcontainers’ [GenericContainer] base class lets you create custom container tests as needed |
| 27 | + java.testcontainers.org |
| 28 | + . |
12 | 29 |
|
13 | | - |
| 30 | +Installation |
14 | 31 |
|
15 | | -# [Read the documentation here](https://java.testcontainers.org) |
| 32 | +Add the Testcontainers core library to your project’s test dependencies. For example, with Maven in pom.xml: |
16 | 33 |
|
17 | | -## License |
| 34 | +<dependency> |
| 35 | + <groupId>org.testcontainers</groupId> |
| 36 | + <artifactId>testcontainers</artifactId> |
| 37 | + <version>1.21.3</version> |
| 38 | + <scope>test</scope> |
| 39 | +</dependency> |
18 | 40 |
|
19 | | -See [LICENSE](LICENSE). |
| 41 | +java.testcontainers.org |
20 | 42 |
|
21 | | -## Copyright |
| 43 | +Or with Gradle (Groovy DSL): |
22 | 44 |
|
23 | | -Copyright (c) 2015 - 2021 Richard North and other authors. |
| 45 | +testImplementation "org.testcontainers:testcontainers:1.21.3" |
24 | 46 |
|
25 | | -MS SQL Server module is (c) 2017 - 2021 G DATA Software AG and other authors. |
| 47 | +Additionally, if you use JUnit 5 you should include the JUnit Jupiter integration: |
26 | 48 |
|
27 | | -Hashicorp Vault module is (c) 2017 - 2021 Capital One Services, LLC and other authors. |
| 49 | +testImplementation "org.testcontainers:junit-jupiter:1.21.3" |
28 | 50 |
|
29 | | -See [contributors](https://github.com/testcontainers/testcontainers-java/graphs/contributors) for all contributors. |
| 51 | +java.testcontainers.org |
| 52 | + |
| 53 | +This pulls Testcontainers (and its transitive dependencies) from Maven Central. For managing multiple Testcontainers modules, you can also use the official Bill of Materials (testcontainers-bom) to avoid specifying versions on each dependency |
| 54 | +java.testcontainers.org |
| 55 | +. |
| 56 | +Usage Example |
| 57 | + |
| 58 | +Below is a simple JUnit 5 example. We annotate the test class with @Testcontainers and declare a @Container field. Testcontainers will start the container before the tests and stop it afterward automatically: |
| 59 | + |
| 60 | +@Testcontainers |
| 61 | +public class ExampleRedisTest { |
| 62 | + @Container |
| 63 | + public static GenericContainer<?> redis = |
| 64 | + new GenericContainer<>("redis:6-alpine") |
| 65 | + .withExposedPorts(6379); |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testRedisConnection() { |
| 69 | + String address = redis.getHost(); |
| 70 | + Integer port = redis.getFirstMappedPort(); |
| 71 | + // ... use the Redis container in assertions ... |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +This shows a JUnit 5 test that launches a Redis container for testing. (Adapted from the official quickstart guide |
| 76 | +java.testcontainers.org |
| 77 | +.) You could similarly use specific containers, e.g. PostgreSQLContainer, MySQLContainer, BrowserWebDriverContainer, etc. Testcontainers will pull the Docker image, start the container, wait until it’s ready, and then tear it down when tests complete. |
| 78 | +Contributing |
| 79 | + |
| 80 | +Testcontainers is a community-driven open-source project and welcomes contributions. See the CONTRIBUTING.md file for guidelines on reporting issues or submitting pull requests. (The repository is part of the GitHub Sponsors program, and sponsor-bounties may be available on issues |
| 81 | +raw.githubusercontent.com |
| 82 | +.) You can also find contribution tips and documentation guidelines in the project docs |
| 83 | +raw.githubusercontent.com |
| 84 | +raw.githubusercontent.com |
| 85 | +. |
| 86 | +License |
| 87 | + |
| 88 | +This project is licensed under the MIT License (see the LICENSE file) |
| 89 | +github.com |
| 90 | +. |
| 91 | +Copyright |
| 92 | + |
| 93 | +© 2015–2021 Richard North and other authors. |
| 94 | +MS SQL Server module © 2017–2021 G DATA Software AG and other authors. |
| 95 | +HashiCorp Vault module © 2017–2021 Capital One Services, LLC and other authors. |
| 96 | +See the CONTRIBUTORS list for all contributors |
| 97 | +github.com |
| 98 | +. |
| 99 | + |
| 100 | +References: Official Testcontainers documentation and GitHub repo |
| 101 | +java.testcontainers.org |
| 102 | +testcontainers.com |
| 103 | +java.testcontainers.org |
| 104 | +github.com |
| 105 | +. |
0 commit comments