spring-dynamic-property

Spring DynamicProperty Resolver

License

License

GroupId

GroupId

com.jupiter-tools
ArtifactId

ArtifactId

spring-dynamic-property
Last Version

Last Version

0.1
Release Date

Release Date

Type

Type

jar
Description

Description

spring-dynamic-property
Spring DynamicProperty Resolver
Project URL

Project URL

https://github.com/jupiter-tools/spring-dynamic-property
Source Code Management

Source Code Management

https://github.com/jupiter-tools/spring-dynamic-property

Download spring-dynamic-property

How to add to project

<!-- https://jarcasting.com/artifacts/com.jupiter-tools/spring-dynamic-property/ -->
<dependency>
    <groupId>com.jupiter-tools</groupId>
    <artifactId>spring-dynamic-property</artifactId>
    <version>0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.jupiter-tools/spring-dynamic-property/
implementation 'com.jupiter-tools:spring-dynamic-property:0.1'
// https://jarcasting.com/artifacts/com.jupiter-tools/spring-dynamic-property/
implementation ("com.jupiter-tools:spring-dynamic-property:0.1")
'com.jupiter-tools:spring-dynamic-property:jar:0.1'
<dependency org="com.jupiter-tools" name="spring-dynamic-property" rev="0.1">
  <artifact name="spring-dynamic-property" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.jupiter-tools', module='spring-dynamic-property', version='0.1')
)
libraryDependencies += "com.jupiter-tools" % "spring-dynamic-property" % "0.1"
[com.jupiter-tools/spring-dynamic-property "0.1"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter jar
org.springframework.boot : spring-boot-starter-test jar
org.junit.platform : junit-platform-engine jar 1.5.0

test (6)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.5.0
org.junit.jupiter : junit-jupiter-engine jar 5.5.0
org.junit.platform : junit-platform-launcher jar 1.5.0
org.testcontainers : testcontainers jar 1.11.3
org.testcontainers : postgresql jar 1.11.3
org.testcontainers : junit-jupiter jar 1.11.3

Project Modules

There are no modules declared in this project.

Spring Dynamic Property

Build Status badge

Tools to define a dynamic property in Spring Boot integration tests.

How to set a property value in tests by the standard spring mechanics

In the Spring Framework we have an annotation TestPropertySource which allows define a final property value:

@SpringBootTest
@ExtendWith(SpringExtension.class)
@TestPropertySource(properties = "my.variable=12345")
class FinalPropertyValueTest {

	@Value("${my.variable}")
	private String variable;

	@Test
	void testVariableValue() {
		assertThat(variable).isEqualTo("12345");
	}
}

On this way we have a small limitation - we cannot define a dynamic value of properties in this annotation, even use of a static method not allowed here.

How to define a dynamic value of properties in tests

spring-dynamic-property supports an ability to define a dynamic value of properties and declare static property provider methods in your test class.

All that you need to use the spring-dynamic-property is the next dependency:

<dependency>
    <groupId>com.jupiter-tools</groupId>
    <artifactId>spring-dynamic-property</artifactId>
    <version>0.1</version>
</dependency>

Let’s look at the simple example of using a dynamic value in properties:

Simple example

@SpringBootTest
@ExtendWith(SpringExtension.class)
class SimpleTest {

	@DynamicTestProperty
	private static TestPropertyValues props() {
		return TestPropertyValues
				.of("variable=" + (int) Math.sqrt(64));
	}

	@Value("${variable}")
	private int variable;

	@Test
	void testSqrt() {
		assertThat(variable).isEqualTo(8);
	}
}

Example with the using of TestContainers

A most primary case to use dynamic properties is a TestContainers initialization in your spring-boot applications.

Let’s consider an example of initialization standard spring-boot properties after start Redis test container:

@SpringBootTest
@Testcontainers
@ExtendWith(SpringExtension.class)
class RedisTestcontainersTest {

    private static final Integer REDIS_PORT = 6379;

    @Container
    private static GenericContainer redis = new GenericContainer("redis:latest")
            .withExposedPorts(REDIS_PORT);

    @DynamicTestProperty
    private static TestPropertyValues props() {
        return TestPropertyValues.of("spring.redis.host=" + redis.getContainerIpAddress(),
                                     "spring.redis.port=" + redis.getMappedPort(REDIS_PORT));
    }

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void readWriteValueByRedisTemplate() {
        String key = "test";
        String value = "sabracadabra";
        // Act
        redisTemplate.opsForValue().set(key, value);
        // Assert
        assertThat(redisTemplate.opsForValue().get(key)).isEqualTo(value);
    }
}

Include dynamic property in meta-annotations

If you need to make you own annotation with dynamic property declaration then you can specify a file with @DynamicProperty methods by the using of @IncludeDynamicProperty annotation as you can see in the example below:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@IncludeDynamicProperty(DynamicPropertyHolder.class)
public @interface AnnotationWithDynamicProperty {

}

the file with dynamic properties:

public class DynamicPropertyHolder {

    @DynamicTestProperty
    private static TestPropertyValues property(){
        return TestPropertyValues.of("key=12345");
    }
}

and now you can use this annotation in your tests to set this property:

@SpringBootTest
@AnnotationWithDynamicProperty
class MetaAnnotationTest {

    @Value("${key}")
    private String key;

    @Test
    void checkPropertyValueInjectInAnnotation() {
        assertThat(key).isEqualTo("12345");
    }
}
com.jupiter-tools

Versions

Version
0.1