Junit 5的弹簧靴黄瓜测试将不加载应用程序测试。
我无法获得我的Cucumber Test从application-test.properties文件中读取,该文件位于测试/资源文件夹中。黄瓜测试正在读取功能,但是应用程序无法启动,因为@Value注释没有从应用程序 - test.properties文件(值为null)中获取值。因此,所有测试都失败了。黄瓜测试与Junit 4的工作正常。为什么我的Cucumber测试无法从应用程序范围的文件中读取?
在我的一个应用程序类中,我使用@value(“ $ {Connection.String}”)
注释在应用程序 - test.properties文件中检索硬编码的连接字符串。连接字符串为null,因此由于实例错误,应用程序无法启动。
属性文件可以找到: 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,使用BOMS,Spring-Boot版本为5.8.1。
I am not able to get my Cucumber test to read from the application-test.properties file, which resides in the test/resources folder. The cucumber test is reading the features, but the application fails to start because the @Value annotation does not pick up the value from the application-test.properties file (value is null). So all the tests fail. The cucumber tests were working fine with Junit 4. Why are my cucumber tests unable to read from the application-properties file?
In one of my application classes, I am using the @Value("${connection.string}")
annotation to retrieve a hard-coded connection string in the application-test.properties file. The connection string is null, so the application fails to start due to an instantiation error.
Properties file can be found at:test/resources/application-test.properties
, target/test-classes/application-test.properties
Here is my Application class:
@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");
}
}
Here is the test app:
@CucumberContextConfiguration
@SpringBootTest(classes = SampleSpringApplication.class)
@ContextConfiguration(classes = TestHelper.class)
@ActiveProfiles("test")
public class SampleTestApp {
}
Here is the functional test:
@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 {
}
There is nothing wrong with the cucumber tests as they work using Junit 4.
partial 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 version is 5.8.1, spring-boot version is 2.6.7, and Cucumber version is 7.3.3, using BOMs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里
ActiveProfiles
什么都不做。该类并不用于构建测试应用程序上下文。此
SampleteSpapp
用于构建测试应用程序上下文,因为它被CucumberContextConfiguration吞噬了
。但是,SpringBoottest
和contextConfiguration
都将尝试配置应用程序上下文。您的意思是将testHelper
添加到类
吗?Here
ActiveProfiles
does nothing. This class isn't used to build the test application context.This
SampleTestApp
is used to build the test application context because it is anotated withCucumberContextConfiguration
. However bothSpringBootTest
andContextConfiguration
will attempt to configure the application context. Did you mean to addTestHelper
toclasses
?