3

I've set up integration tests with Gradle and want to use Spring's "property inheritance". But the application.properties from main gets completely replaced.

Directory structure:

src/
 intTest/
 kotlin/my/package/
 SomethingTest.kt
 resources/
 application.properties
 main/
 kotlin/my/package/
 Something.kt
 resources/
 application.properties

The reason could be that I'm using application.properties (with the same file name) in the intTest and in the main directory. However, if I'm using a profile, let's call it "intTest" and a file called application-intTest.properties, it doesn't work either. The profile specific file is not picked up at all.

Gradle config:

sourceSets {
 intTest {
 compileClasspath += sourceSets.main.output
 runtimeClasspath += sourceSets.main.output
 }
}
configurations {
 intTestImplementation.extendsFrom testImplementation
 intTestRuntimeOnly.extendsFrom runtimeOnly
}
task integrationTest(type: Test) {
 description = 'Runs integration tests.'
 group = 'verification'
 useJUnitPlatform()
 systemProperty 'spring.profiles.active', 'intTest'
 testClassesDirs = sourceSets.intTest.output.classesDirs
 classpath = sourceSets.intTest.runtimeClasspath
 shouldRunAfter test
}
check.dependsOn integrationTest

The test class (based on JUnit 5) is annotated with:

@ExtendWith(SpringExtension::class)
@ComponentScan
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)

If I add @TestPropertySource to my test class the properties file is at least picked up:

@TestPropertySource(locations= ["classpath:application-intTest.properties"])

But the "properties inheritance" doesn't work either. So it is basically the same as using application.properties (without a profile part), what overrides all properties from main.

Adding @Profile("intTest") to my test doesn't solve the problem. The same applies for @ActiveProfiles("intTest").

How can I use "properties inheritance" with my integration test setup?

asked Mar 29, 2019 at 9:44
1
  • If you name the test properties file application.properties then it overwrites the application.properties file because you have just one classpath. But using profiles works for me. Commented Mar 29, 2019 at 10:28

1 Answer 1

5

It turned out that the problem was not my Gradle config but my IntelliJ config. The active profile has to be set in the run configuration -- namely for each and every run configuration in the integration test folder. This can be solved by annotating integration test classes with @ActiveProfiles("intTest").

answered Mar 29, 2019 at 15:38
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.