I have a class that is injected with the application.yaml
properties located in the src/main/resources
directory.
I wrote a test that is asserting if those properties are injected as expected.
The test does work fine but it is using the application.yaml
file from src/main/resources
.
I want it to use the test-application.yaml
file from src/test/resources
to use some bogus values.
I followed many guides and read many questions here on Stackoverflow but could not get any of those approaches to work. Maybe because I mixed up different solution's annotations or whatever.
Status Quo
I am using the spring versions provided by the spring management system
id("io.spring.dependency-management") version "1.0.9.RELEASE"
Edit: Also I use the following relevant dependencies:
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-webflux")
The property file src/main/resources/application.yaml
:
controlroom:
ftpConnections:
foo-importer:
host: ftp.stage.company.com
port: 22
user: foo-ftp
password: password
bar-importer:
host: ftp.stage.company.com
port: 22
user: bar-ftp
password: password
This is the property configuration holder class:
@Configuration
@ConfigurationProperties(prefix = "controlroom")
class FtpConnectionsProvider(
) {
lateinit var ftpConnections: Map<String, Map<String, String>>
}
And this is the test class that currently is injecting the properties from main/resources/application.yaml
:
@ExtendWith(SpringExtension::class)
@SpringBootTest
internal class FtpConnectionsProviderTest(
@Autowired
val ftpConnectionsProvider: FtpConnectionsProvider
) {
@Test
fun `should fill FtpConnectionsProvider with properties from yaml`() {
assertThat(ftpConnectionsProvider.ftpConnections["foo-importer"]).containsAllEntriesOf(
mapOf(
"host" to "ftp.stage.company.com",
"port" to "22",
"user" to "foo-ftp",
"password" to "password"
))
assertThat(ftpConnectionsProvider.ftpConnections["bar-importer"]).containsAllEntriesOf(
mapOf(
"host" to "ftp.stage.company.com",
"port" to "22",
"user" to "bar-ftp",
"password" to "password"
))
}
}
How to use the test-application.yaml ?
Now I wonder how I can give the test the instruction to only use src/test/resources/test-application.yaml
.
First Try
@ExtendWith(SpringExtension::class)
@SpringBootTest
@TestPropertySource("classpath:test-application.yaml")
internal class FtpConnectionsProviderTest
Result: Just ignores it and keeps using src/main/resources/application.yaml
Second Try
People say that @TestPropertySource
can't read .yaml
files and therefore I added the @ContextConfiguration
annotation since this was the suggested fix.
@ExtendWith(SpringExtension::class)
@SpringBootTest
@TestPropertySource("classpath:test-application.yaml")
@ContextConfiguration(initializers=[ConfigFileApplicationContextInitializer::class])
internal class FtpConnectionsProviderTest
Result: Just ignores it and keeps using src/main/kotlin/resources/application.yaml
Nothing seems to work. I do not grasp why this is such a big deal and why it is so complicated to find a solution