【问题标题】:Spring boot + cucumber in main scope. Can I autowire a step definition?Spring Boot + Cucumber 在主范围内。我可以自动装配步骤定义吗?
【发布时间】:2019-09-10 20:12:41
【问题描述】:

我相信这是一个非常特殊的案例,但我正在为我们使用的一些第三方应用程序构建一些黄瓜测试。

由于我并没有真正测试自己的应用程序,因此我创建了一个 maven 项目并将 cucumber 配置为在主文件夹(而不是测试文件夹)中运行。

这是我的入口点类:

@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);
    }
}

还有我的测试课:

@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
    }
}

这目前工作正常,但我想在我的测试中添加弹簧功能。 具体来说,我想在我的黄瓜步骤中自动连接一些东西。

步骤定义:

public class MyStepdefs {

    @Autowired
    private ConfigProperties properties;

    @Given("^Something")
    public void example() {
        //...
    }

我四处寻找,发现有人说我应该在步骤中添加 ContextConfiguration 注释。我是这样做的:

@ContextConfiguration(classes = ExecutableMain.class, loader = SpringBootContextLoader.class)
    public class MyStepdefs {

但这会导致启动过程中出现循环。

我能达到我的需要吗?

【问题讨论】:

    标签: spring-boot cucumber


    【解决方案1】:

    好的,所以我开始关注https://stackoverflow.com/a/37586547/1031162

    基本上我改变了:

    @ContextConfiguration(classes = ExecutableMain.class, loader = SpringBootContextLoader.class)
    

    收件人:

    @ContextConfiguration(classes = ExecutableMain.class, initializers = ConfigFileApplicationContextInitializer.class)
    

    我不是 100% 确定它是如何/为什么起作用的,但确实如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      相关资源
      最近更新 更多