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 Spring Environment or to the system environment.
Since:
2.0.0
Author:
Madhura Bhave, Phillip Webb, Stephane Nicoll
  • Method Details

    • and

      public TestPropertyValues and(String... pairs)
      Return a new TestPropertyValues instance with additional entries. Name-value pairs can be specified with colon (":") or equals ("=") separators.
      Parameters:
      pairs - the property pairs to add
      Returns:
      a new TestPropertyValues instance
    • and

      public TestPropertyValues and(Iterable<String> pairs)
      Return a new TestPropertyValues instance with additional entries. Name-value pairs can be specified with colon (":") or equals ("=") separators.
      Parameters:
      pairs - the property pairs to add
      Returns:
      a new TestPropertyValues instance
      Since:
      2.4.0
    • and

      public TestPropertyValues and(Stream<String> pairs)
      Return a new TestPropertyValues instance with additional entries. Name-value pairs can be specified with colon (":") or equals ("=") separators.
      Parameters:
      pairs - the property pairs to add
      Returns:
      a new TestPropertyValues instance
      Since:
      2.4.0
    • and

      public TestPropertyValues and(Map<String,String> map)
      Return a new TestPropertyValues instance with additional entries.
      Parameters:
      map - the map of properties that need to be added to the environment
      Returns:
      a new TestPropertyValues instance
      Since:
      2.4.0
    • and

      public <T> TestPropertyValues and(Stream<T> stream, Function<T,TestPropertyValues.Pair> mapper)
      Return a new TestPropertyValues instance with additional entries.
      Type Parameters:
      T - the stream element type
      Parameters:
      stream - the elements that need to be added to the environment
      mapper - a mapper function to convert an element from the stream into a TestPropertyValues.Pair
      Returns:
      a new TestPropertyValues instance
      Since:
      2.4.0
    • applyTo

      public void applyTo(ConfigurableApplicationContext context)
      Add the properties from the underlying map to the environment owned by an ApplicationContext.
      Parameters:
      context - the context with an environment to modify
    • applyTo

      public void applyTo(ConfigurableEnvironment environment)
      Add the properties from the underlying map to the environment. The default property source used is MapPropertySource.
      Parameters:
      environment - the environment that needs to be modified
    • applyTo

      public void applyTo(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 modified
      type - the type of PropertySource to be added. See TestPropertyValues.Type
    • applyTo

      public void applyTo(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 modified
      type - the type of PropertySource to be added. See TestPropertyValues.Type
      name - the name for the property source
    • applyToSystemProperties

      public void applyToSystemProperties(Runnable action)
      Add the properties to the system properties for the duration of the action, restoring previous values when it completes.
      Parameters:
      action - the action to take
      Since:
      3.0.0
    • applyToSystemProperties

      public <T> T applyToSystemProperties(Callable<T> call)
      Add the properties to the system properties for the duration of the call, restoring previous values when it 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 new TestPropertyValues 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 new TestPropertyValues 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 new TestPropertyValues 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(Map<String,String> map)
      Return a new TestPropertyValues with the underlying map populated with the given map entries.
      Parameters:
      map - the map of properties that need to be added to the environment
      Returns:
      the new instance
    • of

      public static <T> TestPropertyValues of(Stream<T> stream, Function<T,TestPropertyValues.Pair> mapper)
      Return a new TestPropertyValues with the underlying map populated with the given stream.
      Type Parameters:
      T - the stream element type
      Parameters:
      stream - the elements that need to be added to the environment
      mapper - a mapper function to convert an element from the stream into a TestPropertyValues.Pair
      Returns:
      the new instance
    • empty

      public static TestPropertyValues empty()
      Return an empty TestPropertyValues instance.
      Returns:
      an empty instance