我無法讓我的 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。
uj5u.com熱心網友回復:
@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嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/485761.html
上一篇:使用Junit和Mockito的RestAPI的Junit測驗用例
下一篇:從專案的.xml檔案[不在POM檔案中]中的Internet上的.properties檔案中讀取屬性以更新模塊的版本
