有任何Spring Boot与cucumber-jvm一起工作吗?

时间:2013-12-21 09:51:02

标签: cucumber-jvm spring-boot

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

有人能指出我正确的方向让黄瓜和春天开始,做所有的自动配置和接线,让我的步骤定义使用自动有线豆来做东西吗?

4 个答案:

答案 0 :(得分:17)

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

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

还要确保在类路径上有黄瓜弹簧模块。

答案 1 :(得分:2)

杰克 - 我的最终代码在超类中有以下注释,每个黄瓜步骤定义类都扩展了,这样可以访问基于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);
        }
    }
}

答案 2 :(得分:1)

我的方法非常简单。在Before挂钩中(在env.groovy中,因为我使用的是Groovy的Cucumber-JVM),请执行以下操作。

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

答案 3 :(得分:0)

Thanks to @PaulNUK, I found a set of annotations that will work.

I posted the answer in my question here

My StepDefs class required the annotations: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = DemoApplication.class, loader = SpringApplicationContextLoader.class) @WebAppConfiguration @IntegrationTest

There is also a repository with source code in answer I linked.