Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override config value from @PropertySource used in a @ConfigurationProperties config class in a unit test using @TestPropertySource

I have a config properties instance with prefix "assets."

@Configuration
@ConfigurationProperties( prefix = "assets", ignoreUnknownFields = true )
public class AssetsProperties
{
   @NotNull
   private Resource     file;
   
   public Resource getFile()
   {
     return file;
   }

   public void setFile( Resource file )
   {
     this.file = file;
   }
}

Its default configuration is defined in:

@Configuration
@PropertySource( name = "assetsConfig", value = "classpath:com/package/boot/web/ui/assets/config/default-assets-config.properties" )
@Order( LOW_ORDER )
public class AssetsConfig
{
}

default-assets-config.properties contains:

assets.file=classpath:assets.json

In my unit test I want to override the default value using:

@TestPropertySource( locations = "classpath:com/package/boot/web/ui/assets/tests/assets-config.properties" )

assets-config.properties contains

assets.file=classpath:com/package/boot/web/ui/assets/tests/assets.json

Unfortunately this value is never injected into AssetsProperties. What do I do wrong, I don't understand because spring fmk ref doc says

Test property sources have higher precedence than those loaded from the operating system’s environment or Java system properties as well as property sources added by the application declaratively via @PropertySource or programmatically.

Thanks in advance,

Paskos

like image 785
paskos Avatar asked Feb 09 '15 19:02

paskos


People also ask

How do I override a Spring boot property?

To override your Spring Boot application properties when it's running on Kubernetes, just set environment variables on the container. To set an environment variable on a container, first, initialise a ConfigMap containing the environment variables that you want to override.

How do I change the application properties value at runtime Spring boot?

To change properties in a file during runtime, we should place that file somewhere outside the jar. Then we tell Spring where it is with the command-line parameter –spring. config. location=file://{path to file}.

How do you externalize a constants from a Spring configuration file into a properties file?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.


1 Answers

You've hit a limitation in Spring Boot which means that it ignores properties files configured using @TestPropertySource. As an alternative, you can configure one or more inlined properties instead:

@TestPropertySource(properties = "assets.file=classpath:com/package/boot/web/ui/assets/tests/assets.json")
like image 100
Andy Wilkinson Avatar answered Sep 19 '22 12:09

Andy Wilkinson