Our main application is using Config Server for storing all of its configuration, but the tests were so far configured using a copy of that config as application.yml
and overridden properties in application.properties
plugged into the test class using
@TestPropertySource(locations= { "classpath:application.properties" })
I have converted the properties file into yml, to keep consistency, since we were moving on to that format everywhere. Now the unit tests won't run at all due to:
Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor$ImportException: No spring.config.import set
But this property is present in the new converted application-test.yml
. If I move the property to the application.yml
then the error just moves on to the next missing property
Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"
I tried switching to the config server as the main property source, but the overridden properties are still ignored. I have also tried putting both configs in @TestPropertySource(locations=
in order in which they should be overridden, but also no luck.
Here is my test class config:
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = {LoaderApplication.class})
@TestPropertySource(locations= { "classpath:application-test.yml" })
application.yml:
spring:
polling:
file:
cron: 0/20 * * * * MON-FRI
maxMessagesPerPoll: 1
# a lot of polling configs, some ftp configuration and credentials
application-test.yml
spring:
batch:
job:
enabled: false
cloud:
aws:
s3:
enabled: false
config:
import: 'optional:configserver:'
datasource:
driverClassName: org.h2.Driver
password: ''
url: jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;MV_STORE=FALSE;MODE=MSSQLServer
username: sa
flyway:
baseline-on-migrate: true
baselineOnMigrate: true
enabled: false
ignoreMissingMigrations: true
locations: classpath:db/migration/{vendor}
table: ${spring.application.name}_schema_version
url: jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;MV_STORE=FALSE;MODE=MSSQLServer
h2:
console:
enabled: true
path: /h2-console
settings:
web-allow-others: true
jpa:
hibernate:
ddl-auto: none
properties:
hibernate:
default_schema: renewals
location:
inputPath: src/main/resources/tests/input
outputPathFailure: src/main/resources/tests/output/failure
outputPathSuccess: src/main/resources/tests/output/success
cn:
app:
datasource-populate:
enabled: true
jasypt:
encryptor:
password:
loader:
scope: TEST
logging:
level:
org:
springframework: ERROR
root: ERROR
vendor: h2