Can I use spring to autowire controller in cucumber test?
Asked Answered
I

3

15

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

}
Isador answered 9/5, 2014 at 12:41 Comment(1)
Autowiring with Spring in cucumber-jvm is explained here - liminescence.blogspot.com/2013/08/…Powwow
I
17

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;
}
Iain answered 8/7, 2014 at 7:57 Comment(0)
H
21

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>
Hardness answered 2/9, 2015 at 14:17 Comment(4)
This was very helpful given that the documentation I've found for Cucumber and Spring to be very sparse.Nephology
this is very helpful. Could you share me your dependence file.Aam
@Hardness Thanks!! Could you please add the versions of the dependencies?Eavesdrop
thanks, apparently It is enough to have the first executed step in a class that has the @ContextConfiguration annotation. I created a dummy steps class with a dummy \@Before step (\@io.cucumber.java.Before public void configureSpringContext() {}), so that all my other *Steps class do not have to be annotated. (The test runner class also does not have to be annotated at all, if you do it like this.) In other *Steps class you can simply inject your dependencies.Farber
I
17

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;
}
Iain answered 8/7, 2014 at 7:57 Comment(0)
C
5

In addition to Jewel's answer above do not forget to add in your own additional spring dependencies as "cucumber-spring" artifact seems to only provided the "Glue" between Cucumber and Spring.

In my case I got Spring Injection to work in my step definitions with the following added dependency.

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <scope>test</scope>
</dependency>
..... Plus the rest of the cucumber dependencies

My Step Definition:

@ContextConfiguration("/cucumber.xml")
@PropertySource("classpath*:qa-test.properties")
public class StepsDefinition {

    @Autowired
    private ServiceToInject serviceToInject;
}

CukesRunner

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

Cucumber.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <import resource="classpath:applicationContext.xml"/>
</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="com.mypackage"/>

    <bean id="propertiesPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="qa-test.properties"/>
    </bean>
    ---- rest of my beans
  </beans>
Chancechancel answered 27/2, 2015 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.