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

deamon :

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?

deamon :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=91912&siteId=1