Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use spring to autowire controller in cucumber test?

I am using Cucumber to automate the testing of services and controllers in my app. In addition, I am using the Cucumber Junit runner @RunWith(Cucumber.class) in the test steps. I need to instantiate my controller and was wondering if I could use Spring to autowire this. The code below shows what I want to do.

public class MvcTestSteps {

//is it possible to do this ????
@Autowired
private UserSkillsController userSkillsController;



/*
 * Opens the target browser and page objects
 */
@Before
public void setup() {
    //insted of doing it like this???
    userSkillsController = (UserSkillsController) appContext.getBean("userSkillsController");
    skillEntryDetails = new SkillEntryRecord();

}
like image 665
Billy Sneddon Avatar asked May 09 '14 12:05

Billy Sneddon


People also ask

How do I integrate cucumber in a spring application?

We'll now look at how we can integrate Cucumber in a Spring application. First, we'll create a Spring Boot application – for which we'll follow the Spring-Boot application article. Then, we'll create a Spring REST service and write the Cucumber test for it. 3.1. REST Controller First, let's create a simple controller: 3.2. Cucumber Step Definitions

When to use cucumber for testing?

You can use Cucumber to implement different types of tests. Normally, you use it to cover the ones on top of the test pyramid: integration or end-to-end tests. It’s the place where better fits the needs of mapping business language into feature testing, which usually crosses multiple modules (or components, or microservices…).

Where can I find the source code for cucumber with spring?

I’ll use a practical example to show you how to use Cucumber with Spring. If you want to check out the complete project’s source code, you can clone it from GitHub. All the code in this post is available on GitHub: Spring Boot Cucumber. If you find it useful, please give it a star!

How do I add dependency injection to a cucumber test?

Adding dependency injection to Cucumber tests The piece of code that brings dependency injection to our Cucumber tests is the CucumberSpringConfiguration class. The @CucumberContextConfiguration annotation tells Cucumber to use this class as the test context configuration for Spring.


2 Answers

I just made cucumber and spring working with java based configuration and I think it is worth to share here. here what I have done:

@Configuration
@ComponentScan(basePackages = { "com.*" })
@PropertySource("classpath:application-${environment}.properties")
public class AppConfiguration {

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();

}
}

my step defintion class:

@ContextConfiguration(classes= AppConfiguration.class)
public class Stepdefs {
@Autowired
private MyBean mybean;

and here the test class:

@RunWith(Cucumber.class)
@CucumberOptions(format = { "pretty", "html:target/cucumber-html-report", "json:target/cucumber-json-report.json" })
@ContextConfiguration(classes= AppConfiguration.class)
public class RunCucumberTest {
}

and the maven command is:

mvn -Denvironment=dev clean test

the maven dependencies I have in my pom are:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--spring test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>${hamcrest.version}</version>
            <scope>test</scope>
        </dependency>

        <!--cucumber -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-jvm</artifactId>
            <version>${cucumber.version}</version>
            <type>pom</type>
        </dependency>
like image 150
ttati Avatar answered Sep 30 '22 17:09

ttati


I used cucumber-jvm to autowire Spring into Cucumber.

<dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-core</artifactId>
        <version>1.1.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>1.1.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.1.5</version>
        <scope>test</scope>
    </dependency>

And import applicationContext.xml into Cucumber.xml

<import resource="/applicationContext.xml" />   // refer to appContext in test package
<context:component-scan base-package="com.me.pos.**.it" />   // scan package IT and StepDefs class

In CucumberIT.java call @RunWith(Cucumber.class) and add the CucumberOptions.

@RunWith(Cucumber.class)
@CucumberOptions(
    format = "pretty",
    tags = {"~@Ignore"},
    features = "src/test/resources/com/me/pos/service/it/"  //refer to Feature file
)
public class CucumberIT {  }

In CucumberStepDefs.java you can @Autowired Spring

@ContextConfiguration("classpath:cucumber.xml")
public class CucumberStepDefs {

    @Autowired
    SpringDAO dao;

    @Autowired
    SpringService service;
}
like image 42
Jewel Avatar answered Sep 30 '22 18:09

Jewel