JavaScript Required

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

WE ARE TECHNOLOGY, WE ARE INNOVATION, WE ARE GLOBAL, WE ARE GROWTH

@DynamicPropertySource in Spring 5

Quick Inquiry

captcha

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.

Awesome clients we worked for

Client Testimonials

  • Fabio Durso

    We found a reliable and efficient partner in Aegis Infoways, ready to support our strategy of offshore software development. Aegis Infoways has particularly impressed by the speed of response to any request for the development of software applications and for their maintenance.

  • Filipe

    We did hire full time Java developers from Aegis Infoways, to help us to improve a time to market of a product. The software platform is based on Java & Extjs, and they are delivering the software on time and with success. We strongly recommend Aegis Infoways as Reliable Development partner.

  • Steve

    Powerful solutions are given by Aegis Infoways dedicated developers for my projects. They suggest solutions as per current market trend. Other than this, the team is always ready for any type of changes or update. That is the main reason that I would like to give my next project to them.

Copyright © Aegis Infoways All rights reserved