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-spring with @Configuration class fail with @Scope("cucumber-glue") #965

Closed
JSlain opened this issue Feb 27, 2016 · 7 comments
Closed
Labels
⌛ stale Will soon be closed by stalebot unless there is activity 🙏 help wanted Help wanted - not prioritized by core team

Comments

@JSlain
Copy link

JSlain commented Feb 27, 2016

They way the cucumber-glue scope get registered won't work when you're using some of these scoped components in your non-scoped components declared in your configuration.

For example:

public interface ComponentA{}

@Component
public class DomainComponent{

    @Autowired
    private ComponentA beingInjected;
}

@Configuration
public class MyTestConfiguration{
    @Bean
    @Scope("cucumber-glue")
    public ComponentA componentA(){
        return Mockito.mock(ComponentA.class);
    }
}

@ContextConfiguration(classes=MyTestConfiguration.class)
public class MyGlueCode{

    @Autowired
    private DomainComponent domainComponent;

}

This is because the way the scope currently get registered, we need the ApplicationContext to add the scope into it.

JSlain added a commit to JSlain/cucumber-jvm that referenced this issue Feb 29, 2016
JSlain added a commit to JSlain/cucumber-jvm that referenced this issue Feb 29, 2016
JSlain added a commit to JSlain/cucumber-jvm that referenced this issue Feb 29, 2016
@whittle
Copy link

whittle commented Mar 2, 2016

I have also encountered this issue. Is there a work-around?

@JSlain
Copy link
Author

JSlain commented Mar 2, 2016

Yes there is.
You need to register the GlueScope directly in your configuration, like this:

    @Bean
    public CustomScopeConfigurer glueScopeConfigurer(){
        CustomScopeConfigurer toReturn = new CustomScopeConfigurer();

        toReturn.addScope("cucumber-glue", new GlueCodeScope());

        return toReturn;
    }

However, you can't do it directly since GlueCodeScope is package visible.
The workaround is to create a new class with the @configuration annotation in the same package as GlueCodeScope (which is cucumber.runtime.java.spring). Register the scope in it with the code above.

Then in your glue code, you must use that configuration in addition to your own.

The pull request i made does exactly this, except your don't have to add it manually to your config, it's done magically with ASM. I think it's a bad thing, but haven't found another way doing it; except maybe modifying something else directly in spring.

@whittle
Copy link

whittle commented Mar 21, 2016

Thank you, @JSlain! Your work-around make the cucumber-glue scope available across my test support classes.

For people looking for an answer in the future, the way I was able to tell that this was working was the following message from DefaultListableBeanFactory in my logs: Replacing scope 'cucumber-glue' from [cucumber.runtime.java.spring.GlueCodeScope@2c6f0ce3] to [cucumber.runtime.java.spring.GlueCodeScope@5a0dd59].

Using the work-around, I’m running into an issue where each class that uses @Resource to inject a bean annotated with the scope "cucumber-glue" gets a different instance of that bean. Is that the intended behavior?

@aslakhellesoy aslakhellesoy added Bug 🙏 help wanted Help wanted - not prioritized by core team labels Aug 30, 2016
@UncleLeoTheDad
Copy link

I've also followed this, trying to use it to open up a new webdriver instance for each scenario. I'm also seeing where you get a new instance for each autowired.

@stale
Copy link

stale bot commented Oct 26, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in a week if no further activity occurs.

@stale stale bot added the ⌛ stale Will soon be closed by stalebot unless there is activity label Oct 26, 2017
@stale
Copy link

stale bot commented Nov 2, 2017

This issue has been automatically closed because of inactivity. You can support the Cucumber core team on opencollective

@stale stale bot closed this as completed Nov 2, 2017
@lock
Copy link

lock bot commented Nov 2, 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 Nov 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
⌛ stale Will soon be closed by stalebot unless there is activity 🙏 help wanted Help wanted - not prioritized by core team
Projects
None yet
Development

No branches or pull requests

4 participants