deamon
deamon

Reputation: 92437

How to override application.properties in Spring Boot integration test?

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?

Upvotes: 3

Views: 1589

Answers (1)

deamon
deamon

Reputation: 92437

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").

Upvotes: 5

Related Questions