[JVM] How to use Spring @Autowire, @Value in cucumber groovy step files

1,507 views
Skip to first unread message

Mohan

unread,
May 17, 2016, 10:21:08 AM5/17/16
to Cukes

Problem:

I have earlier created automation test framework with cucumber-jvm(java) and Spring


I now  want to convert into cucumber groovy , convert all code into groovy  and use Spring with annotation like @Autowired,  @Value


Earlier my all Cucumber Steps classes(java) used to extend BaseStep which has Spring components like mentioned below

Sample Base Step class


@ContextConfiguration(locations = {"classpath:cucumber.xml"})

public abstract class BaseStep {


    @Autowired

    protected ScenarioSession scenarioSession;


    public static final String TIME_STAMP = new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date());


    public static final String VERSION = "999";


    public static  final int TIN_IN_SECS = 10;


    // Environment name like QA, Stage, Dev etc

    @Value("${env.name}")

    public String env;


    public ScenarioSession getScenario() {

        return scenarioSession;

    }




}

1. Above ScenarioSession is singleton object shared across various steps to share data  between steps and also share state of webdriver/selenium state


2. @Value use to retrieve test environment name from property file


3 Environment related configuration parameter like baseUrl,  userName and password read from property file e.g Dev.properties,  Stage.properties, Prod.properties.

 environment is specified from command line .eg mvn clean test -DEnv=Dev


4.  Cucumber.xml has following


<context:component-scan base-package="com.company.prooject.protests"/>


<context:property-placeholder location="classpath:env/${Env}.properties"/>


<context:annotation-config />



<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">

    <property name="scopes">

        <map>

            <entry key="cucumber-glue">

                <bean class="cucumber.runtime.java.spring.GlueCodeScope"/>

            </entry>

        </map>

    </property>

</bean>



Question:



1. As Cucumber groovy Step is DSL script without class. It can not extend equivalent Base Common Step Class, How to share singleton common ScenarioSession Object with various groovy Step file


2. How to inject various configuration parameter with the help of Spring @Value in Step class?  When  I added @Autowired or @Value in Step class. It says "not applicable to local variable"


3. How to inject various property read from properties file into step  file with help of Spring



Thanks 

Mohan

Paul King

unread,
May 19, 2016, 5:37:11 AM5/19/16
to cu...@googlegroups.com
I haven't used cucumber for a while. Does using @Field help?

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jason....@compass.com

unread,
May 23, 2016, 5:30:23 PM5/23/16
to Cukes
For 1, I would use a Singleton pattern to hold onto said object. Just keep in mind that this object needs to be set during the before hook AND after hook, or it will become invalid.

For 2, I think you need to use the cucumber-spring dependency in your code and then you should be able to access spring beans correctly. I am assuming your problem is related to this.

For 3, I think this is related to #2. If you are scanning your spring .xml files using the spring runner, you should be ok. (in theory)  
Reply all
Reply to author
Forward
0 new messages