【问题标题】:Any got Spring Boot working with cucumber-jvm?任何让 Spring Boot 与 cucumber-jvm 一起工作?
【发布时间】:2013-12-21 09:51:01
【问题描述】:

我正在使用 spring boot,因为它消除了所有无聊的东西,让我专注于我的代码,但是所有测试示例都使用 junit,我想使用 cucumber?

谁能指出我正确的方向,让黄瓜和弹簧启动,做所有的自动配置和连线,让我的步骤定义使用自动连线的 bean 来做事?

【问题讨论】:

    标签: cucumber-jvm spring-boot


    【解决方案1】:

    尝试在步骤定义类中使用以下内容:

    @ContextConfiguration(classes = YourBootApplication.class, 
                          loader = SpringApplicationContextLoader.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    public class MySteps {
        //...
    }
    

    还要确保你的类路径中有 cucumber-spring 模块。

    【讨论】:

    • @PedroLopez 这能找到故事文件吗?在我的构建中,故事没有被执行。问题是,如果您使用 Cucumber.class 作为运行程序,则会找到故事但 tomcat 不是 startet,如果您使用 SpringJUnit4ClassRunner,则 tomcat ist startet 但不会执行故事。有一个开放增强:github.com/spring-projects/spring-boot/issues/2525你没有这个问题吗?
    • Cucumber runner 必须在不同的类中使用(实际上是在您的 Runner 类中),这是为您的步骤定义。唯一的事情是使用 @Ignore 注释这个类,以防止它在运行整个测试套件时也作为单元测试运行。希望这会有所帮助。
    • 这甚至适用于网络测试!您也可以添加 @WebIntegrationTest({"server.port=9794", "management.port=0"}) 注释,配置的容器将启动并让您运行 Web 集成测试。
    • 这个很好用,谢谢你的回复。如果您有多个 Step Definition 类都想针对同一个 Spring Boot 应用程序运行会发生什么?您如何防止为每个步骤定义类加载应用程序(我们希望为整个套件加载一次)。
    • @PaulNUK 你搞清楚了吗?
    【解决方案2】:

    Jake - 我的最终代码在每个黄瓜步骤定义类扩展的超类中有以下注释,这可以访问基于 Web 的模拟,添加各种测试范围,并且仅引导 Spring 启动一次。

    @ContextConfiguration(classes = {MySpringConfiguration.class}, loader = SpringApplicationContextLoader.class)
    @WebAppConfiguration
    @TestExecutionListeners({WebContextTestExecutionListener.class,ServletTestExecutionListener.class})
    

    WebContextTestExecutionListener 在哪里:

    public class WebContextTestExecutionListener extends
            AbstractTestExecutionListener {
    
        @Override
        public void prepareTestInstance(TestContext testContext) throws Exception {
    
            if (testContext.getApplicationContext() instanceof GenericApplicationContext) {
                GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext();
                ConfigurableListableBeanFactory beanFactory = context
                        .getBeanFactory();
                Scope requestScope = new RequestScope();
                beanFactory.registerScope("request", requestScope);
                Scope sessionScope = new SessionScope();
                beanFactory.registerScope("session", sessionScope);
            }
        }
    }
    

    【讨论】:

    • 嘿抱歉,我花了这么长时间才回复你。我提出了太多的声誉,我无法发布。我能够让它在没有 TestExecutionListeners 的示例项目中工作,但我在 @EnableAutoConfiguration(exclude={RabbitAutoConfiguration.class}) 周围的主项目中遇到问题,这似乎不相关。感谢所有的帮助。
    【解决方案3】:

    我的方法很简单。在 Before 钩子中(在 env.groovy 中,因为我正在使用 Cucumber-JVM for Groovy),执行以下操作。

    package com.example.hooks
    
    import static cucumber.api.groovy.Hooks.Before
    import static org.springframework.boot.SpringApplication.exit
    import static org.springframework.boot.SpringApplication.run
    
    def context
    
    Before {
        if (!context) {
            context = run Application
    
            context.addShutdownHook {
                exit context
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      感谢@PaulNUK,我找到了一组可行的注释。

      我在我的问题here中发布了答案

      我的 StepDefs 类需要注释: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = DemoApplication.class, loader = SpringApplicationContextLoader.class) @WebAppConfiguration @IntegrationTest

      我链接的答案中还有一个包含源代码的存储库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-11
        • 1970-01-01
        • 1970-01-01
        • 2016-01-11
        • 2016-03-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多