![](/img/trans.png)
[英]I need to make an application-test.properties path 100% unique at startup for a spring boot unit test
[英]Spring Boot Cucumber Test with Junit 5 will not load application-test.properties
我無法讓我的 Cucumber 測試從位於 test/resources 文件夾中的 application-test.properties 文件中讀取。 黃瓜測試正在讀取特征,但應用程序無法啟動,因為 @Value 注釋沒有從 application-test.properties 文件中獲取值(值為 null)。 所以所有的測試都失敗了。 黃瓜測試在 Junit 4 上運行良好。為什么我的黃瓜測試無法從應用程序屬性文件中讀取?
在我的一個應用程序類中,我使用@Value("${connection.string}")
注釋來檢索 application-test.properties 文件中的硬編碼連接字符串。 連接字符串為空,因此應用程序由於實例化錯誤而無法啟動。
可以在以下位置找到屬性文件: test/resources/application-test.properties
、 target/test-classes/application-test.properties
這是我的應用程序類:
@SpringBootApplication
@EntityScan(basePackages = { "com.sams.payout.models.dto" })
@ComponentScan(basePackages = { "com.sams.payout", "com.samcash" })
@Slf4j
public class SampleSpringApplication {
public static void main(String[] args) {
SpringApplication.run(SampleSpringApplication.class, args);
log.info("Application Started");
}
}
這是測試應用程序:
@CucumberContextConfiguration
@SpringBootTest(classes = SampleSpringApplication.class)
@ContextConfiguration(classes = TestHelper.class)
@ActiveProfiles("test")
public class SampleTestApp {
}
下面是功能測試:
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameters({
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty,json:target/cucumber.json"),
@ConfigurationParameter(key = JUNIT_PLATFORM_NAMING_STRATEGY_PROPERTY_NAME, value = "long")
})
@ActiveProfiles("test")
public class SampleFT {
}
黃瓜測試沒有任何問題,因為它們使用 Junit 4 工作。
部分 pom.xml:
<dependency>
<groupId>au.com.dius.pact.provider</groupId>
<artifactId>junit5</artifactId>
<version>4.1.36</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<!--Functional tests -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>${cucumber-reporting.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
Junit 版本是 5.8.1,spring-boot 版本是 2.6.7,Cucumber 版本是 7.3.3,使用 BOM。
@Suite
@IncludeEngines("cucumber")
...
@ActiveProfiles("test")
public class SampleFT {
}
這里ActiveProfiles
什么都不做。 此類不用於構建測試應用程序上下文。
@CucumberContextConfiguration
@SpringBootTest(classes = SampleSpringApplication.class)
@ContextConfiguration(classes = TestHelper.class)
@ActiveProfiles("test")
public class SampleTestApp {
}
此SampleTestApp
用於構建測試應用程序上下文,因為它使用CucumberContextConfiguration
進行了注釋。 但是SpringBootTest
和ContextConfiguration
都將嘗試配置應用程序上下文。 您的意思是將TestHelper
添加到classes
嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.