我有一个黄瓜测试:
@RunWith(CucumberWithSerenity.class)
@UsePersistantStepLibraries
@CucumberOptions(
features = "src/integrationTest/resources/features/",
glue = {"com/myorg/proj/integrationtest/stepdefinitions"},
plugin = {"pretty", "summary"}
)
public class integrationTestRunner extends ParentClassWithAllMethods {
}
这是测试,不是在测试文件夹中定义的,而是作为一个seprate文件夹(作为源集,其他人创建了这个结构(。
源集(测试所在地(的渐变配置如下:
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integrationTest/java')
}
resources.srcDir file('src/integrationTest/resources')
}
}
// Dependencies for integrationTest
dependencies {
integrationTestCompileOnly 'org.projectlombok:lombok:1.18.12'
integrationTestAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
integrationTestCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.4.2'
integrationTestImplementation 'net.serenity-bdd:serenity-core:2.2.13'
integrationTestImplementation 'net.serenity-bdd:serenity-cucumber5:2.2.5'
integrationTestImplementation 'net.serenity-bdd:serenity-rest-assured:2.2.13'
integrationTestImplementation 'org.assertj:assertj-core:3.8.0'
integrationTestImplementation 'org.slf4j:slf4j-simple:1.7.7'
}
task integrationTest(type: Test) {
doFirst {
systemProperty 'param1',
System.getProperty('param1')
param1 = System.getProperty('param1')
if (param1 == null)
throw new GradleException("Parameter not passed ./gradlew -DParam<VALUE> ")
}
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
outputs.upToDateWhen { false }
finalizedBy aggregate
}
看看gradle文件,有三件事正在发生,
- 有一个源集定义为
integrationTest
- compileCLassPath、runTimeClassPath、srcDir已为
- 甚至resources.srcDir也在设置中(我有应用程序ymls(
我在resources文件夹中创建了application.yml、application-integrationTest.yml,但仍然无法使用@Value读取任何比例。
例如:
public class ParentClassWithAllMethods {
@Value("${custom.parameter}")
private String customParameter;
public ResponseOptions<Response> getMethod() {
String debugVal = customParameter;
}
.....
}
运行测试时,我希望得到yml文件中定义的值,但它为null。
YML文件如下,
自定义:参数:https://something.com
为什么我无法读取应用程序配置?它总是空的!!
您的测试类没有启动Spring,这就是您获得null的原因。看看这里https://www.baeldung.com/cucumber-spring-integration,它展示了如何在JUnit测试中连接Spring和Cucumber。
然后,测试类将具有Spring和Cucumber的注释,如下所示:
@CucumberContextConfiguration
@SpringBootTest
public class SpringIntegrationTest {
// executeGet implementation
}