JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

Looking for an Expert Development Team? Take two weeks Trial! Try Now

@DynamicPropertySource in Spring 5

Technology: Spring framework is the most used java framework for developing all types of the applications.

In recent version of Spring( aka Spring 5), spring-test artifact introduced the new annotation @DynamicProperySource which will be used to resolve the properties during the run-time.

And also Spring-test is integrated with testcontainer framework, which is mostly used for external system dependency applications.

It provides the containers for each instance of the test, so that without depending on external system, we can execute integration tests.

It provides the containers for mostly used database like MySql, Oracle, postgres etc..

The benefits of using testcontainer framework

  • Database Access layer integration tests:

    using this containerized instance of database, developer/CI tools, no need to install the database software. As it is creating new instance for each tests, we can execute the integration tests as needed without any dependency of database state.

  • Application integration tests:

    for running your application in a short-lived test mode with dependencies, such as databases, message queues or web servers.

  • UI/Acceptance tests:

    use containerized web browsers, compatible with Selenium, for conducting automated UI tests. Each test can get a fresh instance of the browser, with no browser state, plugin variations or automated browser upgrades to worry about.

  • Custom Containers:

    This library will also provide interface for creating new custom containers using GenericContainer as base container.

For each type of containers, we have dependency for that type, suppose if the application needs mysql as database, then we can add the below dependency in pom.xml file:

<dependency> <groupId>org.testcontainers</groupId> <artifactId>mysql</artifactId> <version>1.13.0</version> <scope>test</scope> </dependency>

For bootstrapping the testcontainer, we need to annotate class with @Testcontainers, and declare the type of the container and annotate with @Container annotation.

First lets understand the code need to before @DynamicPropertySource and next will whow the usage of it in reducing lines of code, and better readability.

For suppose if the application is depends on one of the graph database like Neo4j, then we need to create container for it,use in our tests.

As Neo4Jcontainer instance details are available at runtime, we need to configure using spring-boot-test starter to provide the information to test execution environment.

@SpringBootTest @Testcontainers @ContextConfiguration(initializers = ExampleIntegrationTests.Initializer.class) public class ExampleIntegrationTests { @Container private static Neo4jContainer < ? > neo4j = new Neo4jContainer < > (); static class Initializer implements ApplicationContextInitializer < ConfigurableApplicationContext > { @Override public void initialize(ConfigurableApplicationContext context) { TestPropertyValues.of("spring.data.neo4j.uri=" + neo4j.getBoltUrl()) .applyTo(context.getEnvironment()); } } }

Each time Neo4JContainer new instance is created and host details are added to environment using TestPropertyValues, and as we learned above we need to @Testcontainers and @Container from testcontainers library.

Spring test 5, is introduced the new annotation @DynamicPropertySource to provide the property to execution context run-time, but one thing need to remember is that the method used for this annotation must be static and the argument is of the type DynamicPropertyRegistry. Where these run-time properties will be added to environment via DynamicPropertyRegistry registry class.

So, if we use @DynamicPropertySource the above code will look like below:

@SpringBootTest @Testcontainers class ExampleIntegrationTests { @Container static Neo4jContainer < ? > neo4j = new Neo4jContainer < > (); @DynamicPropertySource static void neo4jProperties(DynamicPropertyRegistry registry) { registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl); } }

One more important point is, we are adding the properties to DynamicPropertyRegistry as key and supplier, these suppliers will only executes whenever property is resolved.

Conclusion

We learned the new feature added in spring-test 5 version, we also learned how the complex code can be converted to easily readable and easy maintainable code using this @DynamicPropertySource annotation.

For More Information about Spring 5, and Microsoft Dynamics 365 Services, Visit Aegis Infoways – Leading Offshore Software Development Company.