Spring 启动 + 主范围内的黄瓜。我可以自动装配步骤定义吗?
Spring boot + cucumber in main scope. Can I autowire a step definition?
我认为这是一个非常特殊的案例,但我正在为我们使用的一些第三方应用程序构建一些黄瓜测试。
由于我并不是真正测试自己的应用程序,所以我创建了一个maven项目并将cucumber配置到主文件夹(不是测试文件夹)中的运行。
这是我的入口点class:
@SpringBootApplication
public class ExecutableMain implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ExecutableMain.class, args);
}
@Override
public void run(String... args) {
// args logic...
JUnitCore.runClasses(MyCucumberTest.class);
}
}
还有我的测试class:
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty", "html:target/cucumber", "json:target/cucumber/cucumber.json"},
glue = {"cucumber.app", "cucumber.steps"}
)
public class MyCucumberTest {
@AfterClass
public static void tearDown(){
// quit the browser
}
}
这目前工作正常,但我想在我的测试中添加 spring 功能。
具体来说,我想在我的黄瓜步骤中自动装配一些东西。
步骤定义:
public class MyStepdefs {
@Autowired
private ConfigProperties properties;
@Given("^Something")
public void example() {
//...
}
我四处搜索,发现有人说我应该在步骤中添加 ContextConfiguration 注释。我是这样做的:
@ContextConfiguration(classes = ExecutableMain.class, loader = SpringBootContextLoader.class)
public class MyStepdefs {
但这导致在启动过程中出现循环。
我能达到我需要的吗?
好的,所以我在
之后开始工作
基本上我改变了:
@ContextConfiguration(classes = ExecutableMain.class, loader = SpringBootContextLoader.class)
收件人:
@ContextConfiguration(classes = ExecutableMain.class, initializers = ConfigFileApplicationContextInitializer.class)
我不是 100% 确定 how/why 它有效,但确实有效。
我认为这是一个非常特殊的案例,但我正在为我们使用的一些第三方应用程序构建一些黄瓜测试。
由于我并不是真正测试自己的应用程序,所以我创建了一个maven项目并将cucumber配置到主文件夹(不是测试文件夹)中的运行。
这是我的入口点class:
@SpringBootApplication
public class ExecutableMain implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ExecutableMain.class, args);
}
@Override
public void run(String... args) {
// args logic...
JUnitCore.runClasses(MyCucumberTest.class);
}
}
还有我的测试class:
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty", "html:target/cucumber", "json:target/cucumber/cucumber.json"},
glue = {"cucumber.app", "cucumber.steps"}
)
public class MyCucumberTest {
@AfterClass
public static void tearDown(){
// quit the browser
}
}
这目前工作正常,但我想在我的测试中添加 spring 功能。 具体来说,我想在我的黄瓜步骤中自动装配一些东西。
步骤定义:
public class MyStepdefs {
@Autowired
private ConfigProperties properties;
@Given("^Something")
public void example() {
//...
}
我四处搜索,发现有人说我应该在步骤中添加 ContextConfiguration 注释。我是这样做的:
@ContextConfiguration(classes = ExecutableMain.class, loader = SpringBootContextLoader.class)
public class MyStepdefs {
但这导致在启动过程中出现循环。
我能达到我需要的吗?
好的,所以我在
之后开始工作基本上我改变了:
@ContextConfiguration(classes = ExecutableMain.class, loader = SpringBootContextLoader.class)
收件人:
@ContextConfiguration(classes = ExecutableMain.class, initializers = ConfigFileApplicationContextInitializer.class)
我不是 100% 确定 how/why 它有效,但确实有效。