Search in sources :

Example 1 with ObjectFactory

use of cucumber.api.java.ObjectFactory in project cucumber-jvm by cucumber.

the class CucumberExecutor method createBackends.

private Collection<? extends Backend> createBackends() {
    final ObjectFactory delegateObjectFactory = ObjectFactoryLoader.loadObjectFactory(classFinder, Env.INSTANCE.get(ObjectFactory.class.getName()));
    final AndroidObjectFactory objectFactory = new AndroidObjectFactory(delegateObjectFactory, instrumentation);
    final List<Backend> backends = new ArrayList<Backend>();
    backends.add(new JavaBackend(objectFactory, classFinder));
    return backends;
}
Also used : JavaBackend(cucumber.runtime.java.JavaBackend) Backend(cucumber.runtime.Backend) JavaBackend(cucumber.runtime.java.JavaBackend) ObjectFactory(cucumber.api.java.ObjectFactory) ArrayList(java.util.ArrayList)

Example 2 with ObjectFactory

use of cucumber.api.java.ObjectFactory in project cucumber-jvm by cucumber.

the class WeldFactoryTest method shouldGiveUsNewInstancesForEachScenario.

@Test
public void shouldGiveUsNewInstancesForEachScenario() {
    ObjectFactory factory = new WeldFactory();
    factory.addClass(BellyStepdefs.class);
    // Scenario 1
    factory.start();
    BellyStepdefs o1 = factory.getInstance(BellyStepdefs.class);
    factory.stop();
    // Scenario 2
    factory.start();
    BellyStepdefs o2 = factory.getInstance(BellyStepdefs.class);
    factory.stop();
    assertNotNull(o1);
    assertNotSame(o1, o2);
}
Also used : ObjectFactory(cucumber.api.java.ObjectFactory) Test(org.junit.Test)

Example 3 with ObjectFactory

use of cucumber.api.java.ObjectFactory in project cucumber-jvm by cucumber.

the class CalculatorTest method cucumber.

@Test
public void cucumber() throws Exception {
    assertNotNull(injector);
    assertNotNull(bundleContext);
    final ResourceLoader resourceLoader = new FileResourceLoader();
    final ClassLoader classLoader = Runtime.class.getClassLoader();
    final ObjectFactory objectFactory = new PaxExamObjectFactory(injector);
    final ClassFinder classFinder = new OsgiClassFinder(bundleContext);
    final Backend backend = new JavaBackend(objectFactory, classFinder);
    final RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(getClass());
    final RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
    final Runtime runtime = new Runtime(resourceLoader, classLoader, Collections.singleton(backend), runtimeOptions);
    runtime.run();
    if (!runtime.getErrors().isEmpty()) {
        throw new CucumberException(runtime.getErrors().get(0));
    } else if (runtime.exitStatus() != 0x00) {
        throw new CucumberException("There are pending or undefined steps.");
    }
    assertEquals(runtime.getErrors().size(), 0);
}
Also used : ResourceLoader(cucumber.runtime.io.ResourceLoader) FileResourceLoader(cucumber.runtime.io.FileResourceLoader) JavaBackend(cucumber.runtime.java.JavaBackend) OsgiClassFinder(cucumber.java.runtime.osgi.OsgiClassFinder) Backend(cucumber.runtime.Backend) JavaBackend(cucumber.runtime.java.JavaBackend) Runtime(cucumber.runtime.Runtime) ObjectFactory(cucumber.api.java.ObjectFactory) PaxExamObjectFactory(cucumber.java.runtime.osgi.PaxExamObjectFactory) FileResourceLoader(cucumber.runtime.io.FileResourceLoader) PaxExamObjectFactory(cucumber.java.runtime.osgi.PaxExamObjectFactory) ClassFinder(cucumber.runtime.ClassFinder) OsgiClassFinder(cucumber.java.runtime.osgi.OsgiClassFinder) CucumberException(cucumber.runtime.CucumberException) RuntimeOptionsFactory(cucumber.runtime.RuntimeOptionsFactory) RuntimeOptions(cucumber.runtime.RuntimeOptions) Test(org.junit.Test)

Example 4 with ObjectFactory

use of cucumber.api.java.ObjectFactory in project cucumber-jvm by cucumber.

the class ObjectFactoryLoader method loadObjectFactory.

/**
     * Loads an instance of {@link ObjectFactory}. The class name can be explicit, or it can be null.
     * When it's null, the implementation is searched for in the <pre>cucumber.runtime</pre> packahe.
     *
     * @param classFinder where to load classes from
     * @param objectFactoryClassName specific class name of {@link ObjectFactory} implementation. May be null.
     * @return an instance of {@link ObjectFactory}
     */
public static ObjectFactory loadObjectFactory(ClassFinder classFinder, String objectFactoryClassName) {
    ObjectFactory objectFactory;
    try {
        Reflections reflections = new Reflections(classFinder);
        if (objectFactoryClassName != null) {
            Class<ObjectFactory> objectFactoryClass = (Class<ObjectFactory>) classFinder.loadClass(objectFactoryClassName);
            objectFactory = reflections.newInstance(new Class[0], new Object[0], objectFactoryClass);
        } else {
            objectFactory = reflections.instantiateExactlyOneSubclass(ObjectFactory.class, "cucumber.runtime", new Class[0], new Object[0]);
        }
    } catch (TooManyInstancesException e) {
        System.out.println(e.getMessage());
        System.out.println(getMultipleObjectFactoryLogMessage());
        objectFactory = new DefaultJavaObjectFactory();
    } catch (NoInstancesException e) {
        objectFactory = new DefaultJavaObjectFactory();
    } catch (ClassNotFoundException e) {
        throw new CucumberException("Couldn't instantiate custom ObjectFactory", e);
    }
    return objectFactory;
}
Also used : TooManyInstancesException(cucumber.runtime.TooManyInstancesException) ObjectFactory(cucumber.api.java.ObjectFactory) CucumberException(cucumber.runtime.CucumberException) NoInstancesException(cucumber.runtime.NoInstancesException) Reflections(cucumber.runtime.Reflections)

Example 5 with ObjectFactory

use of cucumber.api.java.ObjectFactory in project cucumber-jvm by cucumber.

the class SpringFactoryTest method shouldGiveUsNewStepInstancesForEachScenario.

@Test
public void shouldGiveUsNewStepInstancesForEachScenario() {
    final ObjectFactory factory = new SpringFactory();
    factory.addClass(BellyStepdefs.class);
    // Scenario 1
    factory.start();
    final BellyStepdefs o1 = factory.getInstance(BellyStepdefs.class);
    factory.stop();
    // Scenario 2
    factory.start();
    final BellyStepdefs o2 = factory.getInstance(BellyStepdefs.class);
    factory.stop();
    assertNotNull(o1);
    assertNotNull(o2);
    assertNotSame(o1, o2);
}
Also used : ObjectFactory(cucumber.api.java.ObjectFactory) BellyStepdefs(cucumber.runtime.java.spring.contextconfig.BellyStepdefs) Test(org.junit.Test)

Aggregations

ObjectFactory (cucumber.api.java.ObjectFactory)26 Test (org.junit.Test)24 BellyBean (cucumber.runtime.java.spring.beans.BellyBean)4 Backend (cucumber.runtime.Backend)2 CucumberException (cucumber.runtime.CucumberException)2 ResourceLoader (cucumber.runtime.io.ResourceLoader)2 JavaBackend (cucumber.runtime.java.JavaBackend)2 AutowiresPlatformTransactionManager (cucumber.runtime.java.spring.commonglue.AutowiresPlatformTransactionManager)2 AutowiresThirdStepDef (cucumber.runtime.java.spring.commonglue.AutowiresThirdStepDef)2 OneStepDef (cucumber.runtime.java.spring.commonglue.OneStepDef)2 BellyStepdefs (cucumber.runtime.java.spring.contextconfig.BellyStepdefs)2 WithSpringAnnotations (cucumber.runtime.java.spring.contextconfig.WithSpringAnnotations)2 OsgiClassFinder (cucumber.java.runtime.osgi.OsgiClassFinder)1 PaxExamObjectFactory (cucumber.java.runtime.osgi.PaxExamObjectFactory)1 ClassFinder (cucumber.runtime.ClassFinder)1 Glue (cucumber.runtime.Glue)1 NoInstancesException (cucumber.runtime.NoInstancesException)1 Reflections (cucumber.runtime.Reflections)1 Runtime (cucumber.runtime.Runtime)1 RuntimeOptions (cucumber.runtime.RuntimeOptions)1