Class TestPropertyValues
- java.lang.Object
- org.springframework.boot.test.util.TestPropertyValues
public final class TestPropertyValues extends Object
Test utilities for adding properties. Properties can be applied to a SpringEnvironment
or to thesystem environment
.- Since:
- 2.0.0
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TestPropertyValues.Pair
A single name value pair.static class
TestPropertyValues.Type
The type of property source.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TestPropertyValues
and(String... pairs)
Builder method to add more properties.void
applyTo(org.springframework.context.ConfigurableApplicationContext context)
Add the properties from the underlying map to the environment owned by anApplicationContext
.void
applyTo(org.springframework.core.env.ConfigurableEnvironment environment)
Add the properties from the underlying map to the environment.void
applyTo(org.springframework.core.env.ConfigurableEnvironment environment, TestPropertyValues.Type type)
Add the properties from the underlying map to the environment using the specified property source type.void
applyTo(org.springframework.core.env.ConfigurableEnvironment environment, TestPropertyValues.Type type, String name)
Add the properties from the underlying map to the environment using the specified property source type and name.<T> T
applyToSystemProperties(Callable<T> call)
Add the properties to thesystem properties
for the duration of thecall
, restoring previous values when the call completes.static TestPropertyValues
empty()
Return an emptyTestPropertyValues
instance.static TestPropertyValues
of(Iterable<String> pairs)
Return a newTestPropertyValues
with the underlying map populated with the given property pairs.static TestPropertyValues
of(String... pairs)
Return a newTestPropertyValues
with the underlying map populated with the given property pairs.static TestPropertyValues
of(Stream<String> pairs)
Return a newTestPropertyValues
with the underlying map populated with the given property pairs.
Method Detail
and
public TestPropertyValues and(String... pairs)
Builder method to add more properties.- Parameters:
pairs
- the property pairs to add- Returns:
- a new
TestPropertyValues
instance
applyTo
public void applyTo(org.springframework.context.ConfigurableApplicationContext context)
Add the properties from the underlying map to the environment owned by anApplicationContext
.- Parameters:
context
- the context with an environment to modify
applyTo
public void applyTo(org.springframework.core.env.ConfigurableEnvironment environment)
Add the properties from the underlying map to the environment. The default property source used isMapPropertySource
.- Parameters:
environment
- the environment that needs to be modified
applyTo
public void applyTo(org.springframework.core.env.ConfigurableEnvironment environment, TestPropertyValues.Type type)
Add the properties from the underlying map to the environment using the specified property source type.- Parameters:
environment
- the environment that needs to be modifiedtype
- the type ofPropertySource
to be added. SeeTestPropertyValues.Type
applyTo
public void applyTo(org.springframework.core.env.ConfigurableEnvironment environment, TestPropertyValues.Type type, String name)
Add the properties from the underlying map to the environment using the specified property source type and name.- Parameters:
environment
- the environment that needs to be modifiedtype
- the type ofPropertySource
to be added. SeeTestPropertyValues.Type
name
- the name for the property source
applyToSystemProperties
public <T> T applyToSystemProperties(Callable<T> call)
Add the properties to thesystem properties
for the duration of thecall
, restoring previous values when the call completes.- Type Parameters:
T
- the result type- Parameters:
call
- the call to make- Returns:
- the result of the call
of
public static TestPropertyValues of(String... pairs)
Return a newTestPropertyValues
with the underlying map populated with the given property pairs. Name-value pairs can be specified with colon (":") or equals ("=") separators.- Parameters:
pairs
- the name-value pairs for properties that need to be added to the environment- Returns:
- the new instance
of
public static TestPropertyValues of(Iterable<String> pairs)
Return a newTestPropertyValues
with the underlying map populated with the given property pairs. Name-value pairs can be specified with colon (":") or equals ("=") separators.- Parameters:
pairs
- the name-value pairs for properties that need to be added to the environment- Returns:
- the new instance
of
public static TestPropertyValues of(Stream<String> pairs)
Return a newTestPropertyValues
with the underlying map populated with the given property pairs. Name-value pairs can be specified with colon (":") or equals ("=") separators.- Parameters:
pairs
- the name-value pairs for properties that need to be added to the environment- Returns:
- the new instance
empty
public static TestPropertyValues empty()
Return an emptyTestPropertyValues
instance.- Returns:
- an empty instance