Any got Spring Boot working with cucumber-jvm?

耗尽温柔 提交于 2019-11-30 13:02:31
Petter Holmström

Try to use the following on your step definition class:

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

Also make sure you have the cucumber-spring module on your classpath.

Jake - my final code had the following annotations in a superclass that each cucumber step definition class extended, This gives access to web based mocks, adds in various scopes for testing, and bootstraps Spring boot only once.

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

where WebContextTestExecutionListener is:

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

My approach is quite simple. In a Before hook (in env.groovy as I am using Cucumber-JVM for Groovy), do the following.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!