Test containers provide a way to run tests in an isolated environment, which means that the tests are not affected by the state of the host system or any external dependencies. This makes it easier to write and run tests that are deterministic and repeatable. Test containers can start up quickly, which means that you can run tests more frequently and get faster feedback on the results.They provide a convenient way to manage the external dependencies required for your tests. You can easily spin up and tear down the necessary resources, such as databases or message brokers, as needed.
Add the below dependency in the pom.xml
<dependencies> <!-- Test Containers Start --> <dependency> <groupId>org.testcontainers</groupId> <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.testcontainers</groupId> <artifactId>mongodb</artifactId> <version>1.17.6</version> <scope>test</scope> </dependency> <!-- Test Containers End --> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.testcontainers</groupId> <artifactId>testcontainers-bom</artifactId> <version>1.17.3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> }
package com.thecodeshelf.learningtestcontainers; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; import org.testcontainers.containers.MongoDBContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; import org.testcontainers.utility.DockerImageName; import java.util.UUID; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) @Testcontainers class LearningTestcontainersApplicationTests { @Autowired private CartRepository cartRepository; @Container private static MongoDBContainer container = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10")); @DynamicPropertySource public static void overrideProps(DynamicPropertyRegistry registry){ registry.add("spring.data.mongodb.uri",container::getReplicaSetUrl); } @Test void validateCartItemsCount() { CartCollection cart = new CartCollection(); cart.setCartId(UUID.randomUUID().toString()); cartRepository.save(cart); System.out.println("Cart has been saved to the Test Container"); int totalCartCount = (int) cartRepository.count(); System.out.println("Cart Count from Mongo Test Container is : " + totalCartCount); } }
@DynamicPropertySource : This annotation helps with overriding the values from the property file at athe runtime.
Overall, test containers are a valuable tool for testing Java applications that depend on external resources, helping to improve the reliability and consistency of your tests.
Code is available here : GitHub Project
Happy Coding !
Quick Links
Legal Stuff