![](/img/trans.png)
[英]Override application.properties for integration tests in spring-boot app
[英]How to override application.properties in Spring Boot integration test?
我已經使用Gradle設置了集成測試,並希望使用Spring的“屬性繼承” 。 但是main
的application.properties
完全被替換了。
目錄結構:
src/
intTest/
kotlin/my/package/
SomethingTest.kt
resources/
application.properties
main/
kotlin/my/package/
Something.kt
resources/
application.properties
原因可能是我在intTest
和 main
目錄中使用application.properties
(具有相同的文件名)。 但是,如果我使用的是配置文件,我們將其稱為“intTest”和名為application-intTest.properties
的文件,它也不起作用。 根本不提取特定於配置文件的文件。
Gradle配置:
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
測試類(基於JUnit 5)注釋為:
@ExtendWith(SpringExtension::class)
@ComponentScan
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
如果我將@TestPropertySource
添加到我的測試類中,則至少會選擇屬性文件:
@TestPropertySource(locations= ["classpath:application-intTest.properties"])
但是“屬性繼承”也不起作用。 所以它與使用application.properties
(沒有配置文件部分)基本相同,它覆蓋了main
所有屬性。
將@Profile("intTest")
添加到我的測試中並不能解決問題。 這同樣適用於@ActiveProfiles("intTest")
。
如何在集成測試設置中使用“屬性繼承”?
原來,問題不是我的Gradle配置,而是我的IntelliJ配置。 必須在運行配置中設置活動配置文件 - 即集成測試文件夾中的每個運行配置。 這可以通過使用@ActiveProfiles("intTest")
注釋集成測試類來解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.