You can extend the library and reuse services accross tests
How to use it?
Examples
Mongo
val mongo =newMongoDBContainer(DockerImageName.parse("mongo:4.0.10"))mongo.start()val uri =s"mongodb://${mongo.getContainerIpAddress}:${mongo.getPort}/$database"// and then you can create your client...
val node: KafkaContainer =newKafkaContainer( DockerImageName.parse("confluentinc/cp-kafka:6.2.1")) node.start()val settings =ConsumerSettings( system,new LongDeserializer,new StringDeserializer)// or any other deserializer.withBootstrapServers(kafkaContainer.getBootstrapServers).withGroupId("group-id").withProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest")
Avoid traps
Container lifecyle
Don’t forget to start the container, don’t forget to stop it.
One container per test suite
Reference to the container as a field of the class
Start the container during the test initialization
Create the clients
Use BeforeAfterAll to override afterAll to
stop the container
A container per test
Cannot have a single reference to the container like previously.
How to create a container per test and stop it each time?