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

Ability to configure PicoContainer #290

Closed
rapaul opened this issue Apr 4, 2012 · 5 comments
Closed

Ability to configure PicoContainer #290

rapaul opened this issue Apr 4, 2012 · 5 comments

Comments

@rapaul
Copy link
Contributor

rapaul commented Apr 4, 2012

This feature request rises from http://groups.google.com/group/cukes/browse_thread/thread/a1e034468df4b5f2

In order to test across a range of environments
I want to configure the instance of a dependency in PicoContainer

Example snipped from the mailing list:

public class MyHooks { 
    private final MutablePicoContainer pico; 
    public MyHooks(MutablePicoContainer pico) { 
        this.pico = pico; 
    } 
    @Before("@foo") 
    public void registerFooThing() { 
        pico.addComponent(new MyThing("FOO")); 
    } 
    @Before("@bar") 
    public void registerBarThing() { 
        pico.addComponent(new MyThing("BAR")); 
    }
    @Before 
    public void registerWebDriverBasedOnSystemProperty() {
        String seleniumGrid = System.getProperty("selenium.grid");
        if (seleniumGrid == null) {
            pico.add(new FirefoxDriver());
        } else {
            pico.addComponent(new RemoteWebDriver(seleniumGrid)); // This isn't exact syntax but you get the idea
        }
    }
} 

Currently it isn't possible to have an instance of MutablePicoContainer injected into the MyHooks.

@rapaul
Copy link
Contributor Author

rapaul commented Apr 12, 2012

I've added a basic Cucumber project at https://github.com/rapaul/cukejvm-web-driver-switching/ which has an ugly hack in the constructor of MyWebDriver to choose the right driver based on an environment variable. Provided as a more concrete example of why the MyHooks type approach would be nicer. I've yet to look into how this could be achieved.

@eric-thelin
Copy link

I am looking for a way to make the constructors of my step definition classes depend on interfaces instead of concrete classes. Is it possible to achieve this with the current implementation of cucumber-jvm and picocontainer? Otherwise, the fix above seems to address this problem.

@rapaul
Copy link
Contributor Author

rapaul commented Jan 11, 2013

I started looking into Guice for choosing an implementation while only depending on an interface. It all worked swimmingly, see https://github.com/rapaul/cucumber-jvm-guice-examples

The Guice support in Cucumber allows for the loading of custom Guice Modules which enables this behaviour https://github.com/rapaul/cucumber-jvm-guice-examples/blob/master/src/test/java/test/MyModule.java

So all of this is certainly possible using Guice, the only downside I see is that you need to annotate all your StepDefinition classes and any dependencies that also needs collaborators injected into its constructor. With PicoContainer this isn't required. I'm going to look at providing a similar function to custom Guice Modules for PicoContainer, adapting PicoFactory to support a custom 'Module' from which you can get access to the MutablePicoContainer and call pico.addComponent(componentKey, componentImplementationOrInstance).
e.g. pico.addComponent(WebDriver.class, FirefoxDriver.class);

@eric-thelin
Copy link

Thank you!

I agree with you in that it would be desirable not having to pollute the implementation with dependency annotations. I wish you the best luck going forward with PicoContainer and hope to hear from you should you wish to share any progress.

@lock
Copy link

lock bot commented Oct 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants