Zac Tolley
Zac Tolley

Reputation: 2390

Any got Spring Boot working with cucumber-jvm?

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?

Upvotes: 9

Views: 7157

Answers (4)

adarshr
adarshr

Reputation: 62573

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

Upvotes: 1

Petter Holmström
Petter Holmström

Reputation: 189

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.

Upvotes: 17

jakehschwartz
jakehschwartz

Reputation: 1005

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.

Upvotes: 0

PaulNUK
PaulNUK

Reputation: 5209

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

Upvotes: 2

Related Questions