Tatiana Goretskaya
Tatiana Goretskaya

Reputation: 576

Multiple ObjectFactory instances for cucumber

I wrote a set of feature files for testing a custom framework and I want to allow testing of specific implementations of the interfaces of the framework. I want to run a whole lot of features with different implementations.

To do that, I have created a custom ObjectFactory and passing implementations using PicoContainer dependency injection. I added this factory to a cucumber.properties file and it works just fine. The only problem is - what if I have more than one set of implementations to test?

I can create several ObjectFactories, but how can I run the tests multiple times with different factories? Is it possible to pass ObjectFactory implementation to Runner class, using annotation or something alike? I run features with JUnit runner, and if I can have several of them with different factories, it should work, I think. However the only option to specify ObjectFactory I've found is cucumber.options file which is one for a module...

Upvotes: 2

Views: 7762

Answers (2)

Tatiana Goretskaya
Tatiana Goretskaya

Reputation: 576

@mpkorstanje provided an answer I came up with as well. In case someone needs an example of implementation - here it is:

@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/resources")
public class MyRunner {

    @BeforeClass
    public static void setup(){
        System.setProperty(EventProcessorPicoFactory.EVENT_BUS_HANDLER, IUserECNDataHandler.class.getName());
    }

}

public class MyFactory {
    public MyObject build() {
        String type = System.getProperty("my.property.name");
        switch (type) {
            case  "my.value":
                return new MyObject();
            default:
                throw new IllegalArgumentException("not implemented");
            }
        }   

    }

Upvotes: 1

M.P. Korstanje
M.P. Korstanje

Reputation: 12019

Currently it is not possible to use multiple object factories in Cucumber. As a work around you could implement a single object factory that delegates to a different object factory depending on some environment variable.

You may also want to consider using cucumber-spring instead of cucumber-pico as cucumber-spring can pick up springs context configuration annotations from step definitions. This can be done with minimal configuration if you structure your project like this:

 | - runners 
 | | - CucumberConfigATest.java // @CucumberOptions(glue="steps", extraGlue="config.a") 
 | | - CucumberConfigBTest.java // @CucumberOptions(glue="steps", extraGlue="config.b")
 | - steps
 | | - SomeSteps.java
 | | - MoreSteps.java
 | - config
 | | - a
 | | | - StepsWithContextConfigA.java
 | | - b
 | | | - StepsWithContextConfigB.java

Upvotes: 2

Related Questions