6

I have an application that was tested with Cucumber, but since upgrading (Cucumber 1.1.6 to 1.2.5, java 1.6 to 1.8, Spring 3.2.0 to 4.2.6) it no longer works because it complains about Annotations differs on glue classes found

The structure is as follows:

  • Some common stepdefs which need some property values
  • More specific stepdefs which require some @ContextConfiguration

Both of these should also share a bean.

The common part will never be run on its own. But I have multiple tests each using their own specific stepdefs. This now refuses to run because of Annotations differs on glue classes found.
Is there a way to get this to run without polluting the common context with the specifics of the concrete context?


The step definitions:

@ContextConfiguration("classpath:cucumber-common.xml")
public class CommonStepdefs {
    @Autowired
    private SharedBean sharedBean;
    @Value("${some.property}")
    private String someProperty;

    // actual step def methods
}

@ContextConfiguration("classpath:cucumber-concrete.xml")
public class ConcreteStepdefs {
    @Autowired
    private SharedBean sharedBean;
    @Autowired
    private OtherBean otherBean;

    // actual step def methods
}

The common Spring configuration:

<?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:property-placeholder location="classpath:com/example/cucumber-common.properties"/>
    <context:spring-configured/>
    <context:annotation-config/>

    <bean id="glueCodeScope" class="cucumber.runtime.java.spring.GlueCodeScope"/>
    <bean id="glueCodeScopeConfigurer" class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="cucumber-glue" value-ref="glueCodeScope"/>
            </map>
        </property>
    </bean>

    <bean id="sharedBean" class="com.example.SharedBean" scope="cucumber-glue"/>
</beans>

The other Spring configuration (which imports the common one):

<?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">

    <import resource="classpath:cucumber-common.xml"/>

    <context:spring-configured/>
    <context:annotation-config/>
    <context:component-scan base-package="com.example.rest"/>

    <!-- more bean definitions -->
</beans>

The tests are run using:

@RunWith(Cucumber.class)
@CucumberOptions(
    format = { "html:target/cucumber-html-report", "json:target/cucumber-json-report.json" },
    glue = { "com.example.common", "com.example.concrete" },
    monochrome = true,
    strict = true)

1 Answer 1

0

You can use qaf-gherkin which simplifies implementation for this kind of requirement. With QAF you can achieve without using spring. It also supports steps from packaged jar. If you have common steps in one package and platform specific steps in different package your configuration may look like below:

step.provider.pkg=com.myapp.steps.common;com.myapp.steps.web

In case if you want to run together with you can specify in TestNG xml config file as below:

<test name="Test-web">
   <parameter name="step.provider.pkg" value="com.myapp.steps.common;com.myapp.steps.web" />
   <parameter name="scenario.file.loc" value="resources/features" />
   <classes>
      <class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
   </classes>
</test>
<test name="Test-mobile">
   <parameter name="step.provider.pkg" value="com.myapp.steps.common;com.myapp.steps.mobile" />
   <parameter name="scenario.file.loc" value="resources/features" />
   <classes>
      <class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
   </classes>
</test>

One of the benefit is, common steps can be in jar which you can share across multiple projects. Like spring @Autowired, QAF supports @Inject annotation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.