Testcontainers for Java 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 — a lightweight open sourced library which acts as a thin client for Synthesized TDK integrated with TestContainers.
In order to use
tdk-tc, find the most recent version of it on Maven Central, then include it in your project’s test dependencies using Maven or Gradle:
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.
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 """ );
Am creation of two containers on the same network and starting them in parallel threads can look like this:
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.