# Testcontainers integration

> Source: https://docs.synthesized.io/tdk/latest/user_guide/070_integrations/external_tools/testcontainers
> For the complete documentation index, see [llms.txt](https://docs.synthesized.io/llms.txt).

[Testcontainers for Java](https://www.testcontainers.org/) 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.

By combining Testcontainers with Synthesized’s Test Data Kit (TDK), developers can populate any Testcontainers database with synthetically generated data, enabling rapid development of tests for logic which involves interaction with the database, while avoiding the need to develop and maintain huge amounts of code.

In order to do this one can use [tdk-tc](https://github.com/synthesized-io/tdk-tc) — a lightweight open sourced library which acts as a thin client for Synthesized TDK integrated with TestContainers.

## Adding a dependency to `tdk-tc`

In order to use `tdk-tc`, find the [most recent version](https://search.maven.org/artifact/io.synthesized/tdk-tc) of it on Maven Central, then include it in your project’s test dependencies using Maven or Gradle:

<table class="tableblock frame-all grid-all stretch"><colgroup><col style="width: 50%;"> <col style="width: 50%;"></colgroup><tbody><tr><td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph"><p>Maven</p></div></div></td><td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph"><p>Gradle</p></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div class="content"><div class="listingblock"><div class="content"><pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">&lt;dependency&gt;
  &lt;groupId&gt;io.synthesized&lt;/groupId&gt;
  &lt;artifactId&gt;tdk-tc&lt;/artifactId&gt;
  &lt;version&gt;[version here]&lt;/version&gt;
  &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;</code></pre></div></div></div></td><td class="tableblock halign-left valign-top"><div class="content"><div class="listingblock"><div class="content"><pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">testImplementation("io.synthesized:tdk-tc:1.01")</code></pre></div></div></div></td></tr></tbody></table>

## Using `SynthesizedTDK` class

The class `io.synthesized.tdktc.SynthesizedTDK` runs Synthesized TDK in a Docker container using Testcontainers internally.

A transformation can be run by calling `transform` method on an instance of SynthesizedTDK.

```java
new SynthesizedTDK()
  // Use this method to alter container image name for the TDK-CLI container
  //.setImageName(...)

  // Use this method to set license key in case you are using paid version of TDK-CLI
  //.setLicense(...)

  .transform(
     //Input JdbcDatabaseContainer: empty database with schema
     input,
     //Output JdbcDatabaseContainer: output database with generated data
     output,
     //Workflow configuration in YAML format
        """
        default_config:
          mode: "GENERATION"
          target_row_number: 10
        global_seed: 42
        """
);
```

> **WARNING**
> Both `input` and `output` containers must be on the same Testcontainers `Network`. `SynthesizedTDK` will run the TDK container on the same network also.

Am creation of two containers on the same network and starting them in parallel threads can look like this:

```java
network = Network.newNetwork();
input = new PostgreSQLContainer<>("postgres:11.1")
                .withInitScript(...)
                .withNetwork(network);
output = new PostgreSQLContainer<>("postgres:11.1")
                .withNetwork(network);
Startables.deepStart(input, output).join();

new SynthesizedTDK()
                .transform(....);
```

A good place to use `SynthesizedTDK` is in the `@TestConfiguration` of your Spring application, where you produce connections to the databases used during the test. The complete example of `tdk-tc` usage in a sample backend project is located [here](https://github.com/synthesized-io/tdk-tc).
