Issue using cucumber-spring with Spring Profiles

1,135 views
Skip to first unread message

DK

unread,
May 12, 2015, 1:30:41 PM5/12/15
to cu...@googlegroups.com
In my Cucumber Test Class I've set:

```
@ActiveProfiles(profiles = "Development")
@RunWith(Cucumber.class)
```

However, beans in My AppConfig are not getting injected into my Hooks and StepDefs

```
@Configuration
@Profile("Development")
public class AppConfig {
    @Bean
    public String devString() {
        return "Hello Development";
    }
}
```

Bean never gets injected by Spring

```
@Profile("Development")
public class Hooks {
    @Inject
    private String devString;  /// this is null & doesn't get injected

    @Before("@Before")
    public void beforeScenario() {
        System.out.println("devString = " + devString);
    }
}
```

Björn Rasmusson

unread,
May 12, 2015, 2:31:36 PM5/12/15
to cu...@googlegroups.com
DK wrote:
In my Cucumber Test Class I've set:

```
@ActiveProfiles(profiles = "Development")
@RunWith(Cucumber.class)
```

Spring annotations have no effect on Cucumber runner classes. The only classes that cucumber-spring (or other backends of Cucumber-JVM) will know about is Step Definition/Hook classes. So only Spring annotations on those classes can be taken into account. Specifically you need to have a @ContextConfiguration or @ContextHierarchy annotation on (at least) one of the Step Definition/Hook classes, to supply the definition of the Spring context to be used (or you can use a Cucumber.xml file on the base of the classpath to define the Spring context).

 
However, beans in My AppConfig are not getting injected into my Hooks and StepDefs

```
@Configuration
@Profile("Development")
public class AppConfig {
    @Bean
    public String devString() {
        return "Hello Development";
    }
}
```

Bean never gets injected by Spring

```
@Profile("Development")
public class Hooks {
    @Inject
    private String devString;  /// this is null & doesn't get injected

    @Before("@Before")
    public void beforeScenario() {
        System.out.println("devString = " + devString);
    }
}
```
AFAIK cucumber-spring has not been specifically developed or tested to work with @Profile annotations. It could work, in that case I would say that you need to have the @ActiveProfiles annotation on the same class that have the @ContextConfiguration/@ContextHierarchy annotation so that when the Spring context is created the @ActiveProfile annotation can be taken into account

Best Regards
Björn

Roberto Lo Giacco

unread,
May 13, 2015, 4:07:54 AM5/13/15
to cu...@googlegroups.com
Il giorno martedì 12 maggio 2015 20:31:36 UTC+2, Björn Rasmusson ha scritto:

AFAIK cucumber-spring has not been specifically developed or tested to work with @Profile annotations. It could work, in that case I would say that you need to have the @ActiveProfiles annotation on the same class that have the @ContextConfiguration/@ContextHierarchy annotation so that when the Spring context is created the @ActiveProfile annotation can be taken into account

I did use the @ActiveProfile in my tests, even if I do not remember specifically on which project, and I can confirm it works as expected. 
Reply all
Reply to author
Forward
0 new messages