Cucumber-jvm: cucumber-spring with @Configuration class fail with @Scope("cucumber-glue")

Created on 27 Feb 2016  路  7Comments  路  Source: cucumber/cucumber-jvm

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.

stale help wanted

Most helpful comment

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.

All 7 comments

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

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.

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鈥檓 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?

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.

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.

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

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.

Was this page helpful?
0 / 5 - 0 ratings