Any got Spring Boot working with cucumber-jvm?

谁都会走 提交于 2019-11-30 15:05:50

问题


I'm using spring boot as it removes all the boring stuff and let's me focus on my code, but all the test examples use junit and I want to use cucumber?

Can someone point me in the right direction to get cucumber and spring to start things up, do all the auto config and wiring and let my step definitions use auto wired beans to do stuff?


回答1:


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.




回答2:


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



回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/20717677/any-got-spring-boot-working-with-cucumber-jvm

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