Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cucumber with Spring issue #786

Open
Sonali77 opened this issue Mar 20, 2019 · 7 comments
Open

Cucumber with Spring issue #786

Sonali77 opened this issue Mar 20, 2019 · 7 comments

Comments

@Sonali77
Copy link

Hi,

Initially we used fluentlenium 3.7.1,cucumber 3.0.2 , Spring 4 (Jdk 1.8) and our cucumber tests(using spring 4) seemed to work fine. Now we upgraded to fluentlenium 3.8.1,cucumber 4.2.6(Keeping back Spring 4 and JDk 1.8) and or cucumber tests (using spring 4) is having the below error-

Expected only one instance, but found too many: [cucumber.runtime.java.spring.SpringFactory@29372, cucumber.runtime.java.fluentlenium.FluentObjectFactory@3b3b2f]
More than one Cucumber ObjectFactory was found in the classpath

You probably may have included, for instance, cucumber-spring AND cucumber-guice as part of
your dependencies. When this happens, Cucumber falls back to instantiating the
DefaultJavaObjectFactory implementation which doesn't provide IoC.
In order to enjoy IoC features, please remove the unnecessary dependencies from your classpath.

I tried excluding cucumber-guice in my POM.xml but still i am getting this error. Not sure if some other API also have this as a transitive dependency.

Please let me know, the possible solution for the same.

Regards,
Sonali

@slawekradzyminski slawekradzyminski changed the title Expected only one instance, but found too many: [cucumber.runtime.java.spring.SpringFactory@29372, cucumber.runtime.java.fluentlenium.FluentObjectFactory@3b3b2f] More than one Cucumber ObjectFactory was found in the classpath You probably may have included, for instance, cucumber-spring AND cucumber-guice as part of your dependencies. When this happens, Cucumber falls back to instantiating the DefaultJavaObjectFactory implementation which doesn't provide IoC. In order to enjoy IoC features, please remove the unnecessary dependencies from your classpath. Cucumber with Spring issue Mar 21, 2019
@koryl koryl added the cucumber label Mar 25, 2019
@koryl
Copy link
Collaborator

koryl commented Mar 25, 2019

@Sonali77, thank you for report. Actually, the case is different, because the fluentlenium-cucumber does not use Spring or Guice dependencies at all. I have implemented special ObjectFactory for FL and this is the reason why Cucumber says there are few Object Factories. The FluentObjectFactory was created to handle creating instances and injecting FluentLenium context into step definitions classes. You can check the mentioned class here: https://github.com/FluentLenium/FluentLenium/blob/develop/fluentlenium-cucumber/src/main/java/cucumber/runtime/java/fluentlenium/FluentObjectFactory.java
I would like to know, how did you start FluentLenium context for your tests? Did you add Before and After hooks for all you step definition classes?

@Sonali77
Copy link
Author

Basically i am getting this for my Spring related tests. I have defined one step def as below-
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/api/application-context.xml")
public class SpringSystemTestSteps {
@before
public void SetupCucumberSpringContext() {
//dummy method so cucumber will recognize this class as glue
//and use its context configuration
}

}

The other step definitions doesn't have the -
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/api/application-context.xml")
defined anymore. Its just the tests. I am getting this error.

I also have other stepdefinitions where Before and After hooks defined for all step defs. This error is even present there.

Please let me know, if i need to do something different now.

@Sonali77
Copy link
Author

public class StepDefinition1 extends FluentCucumberTest { 

 @Before("@UISetUp")
    public void beforeScenario(Scenario scenario) {
        this.scenario = scenario;
        super.before(scenario);
    }

    @After("@UISetUp")
    public void teardown(Scenario scenario) {
        if (scenario.isFailed()) {
            try {
                scenario.embed(((TakesScreenshot) getDriver()).getScreenshotAs(OutputType.BYTES), "image/png");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.after(scenario);
        super.setWebDriver(null);

    }
}

We have added before and after hooks in all of our step definitions which run UI tests(which uses Fluentlenium).

Additionally, we also have spring related tests, where we initialize spring context with only one step definition class-

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/api/application-context.xml")
public class SpringSystemTestSteps {
    @Before
    public void SetupCucumberSpringContext() {
        //dummy method so cucumber will recognize this class as glue
        //and use its context configuration
    }
}

We use Spring's context for both API testing (doesn't extend FluentCucumberTest) and UI testing (does extend FluentCucumberTest).

@koryl
Copy link
Collaborator

koryl commented Mar 27, 2019

Thank you for this input - I will try to find workaround for this matter and back to you with the anwser.

@koryl
Copy link
Collaborator

koryl commented Mar 30, 2019

@Sonali77,
try to use fluentlenium-core dependency instead of fluentlenium-cucumber.
Please chceck example here:
https://github.com/koryl/fluentlenium-cucumber-config-example/tree/spring
To work correctly, add two new classes:

Your BaseTest should inherit after you.package.FluentCucumberTest.
Everything will work as earlier but you have to add before and after hook for each step definition class (as you already did).
Let me know if it works for you.

@Sonali77
Copy link
Author

Sonali77 commented Apr 4, 2019

I will test your suggestion and let you know the update.

Just to let you know, we use Spring, Cucumber, Fluentlenium in our project. Also we would like to use Single step definition class with a before and after hook in our project, instead of adding to each step definition class. I work with Rich, who opened the below issue -
#738

We would like to have one step def to have before and after hook, instead of all. Before and After hook in all step definition classes will instance all step definiton files for every scenario regardless if they are used or not within the test.

@Sonali77
Copy link
Author

I tested your suggestion. It works but its the same as fluentlenium 3.7.1 and is not solving the issue if multiple step definition and also need to have my own FluentCucumberTest. So it does not make sense to make an upgrade to fluentlenium 3.8.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants