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

I have an instance of configuration properties with the 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; } } 

The 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 entered in AssetsProperties. What I am doing wrong, I do not understand, because spring fmk ref doc says

Sources of test properties have a higher priority than those loaded from the operating system environment or Java system properties, as well as property sources added declaratively via @PropertySource or programmatically.

Thanks in advance,

Paskos

+5
source share
1 answer

You have selected a limitation in Spring Boot , which means that it ignores property files configured with @TestPropertySource . Alternatively, you can configure one or more of the built-in properties:

 @TestPropertySource(properties = "assets.file=classpath:com/package/boot/web/ui/assets/tests/assets.json") 
+7
source

Source: https://habr.com/ru/post/1212991/


All Articles