I am looking for a nice way to active a spring profile for my cucumber tests. The cucumber tests need to use a stubbed version of a service which is marked with:
@Profile("test")
@Component
class FooServiceStub extends FooService {...}
The regular service looks like this:
@Profile("prod")
@Component
class FooService {...}
My requirements:
- Run cucumber tests with mvn: $ mvn test
- Run cucumber tests in IDE
- Run cucumber tests on build server
- No need to use the -Dspring.profiles.active=... argument
Sources I've found but don't solve my issue:
- http://www.baeldung.com/cucumber-spring-integration (using @ContextConfiguration loader with SpringApplicationContextLoader.class which is not present in the latest version of Spring Boot, 1.5.2.RELEASE at the time of writing.)
- programatically set Spring profile in Cucumber (messing with system property)
I've solved this issue with an annotation that I put on my FeatureStep class.
The annotation:
Note the @ActiveProfiles on it.
The configuration class is very basic:
Using the annotation:
Adding the annotation to the feature steps:
Now when running the Cucumber feature tests, either from my IDE, from the command line with maven or on the build server, my test is using the FooServiceSTub and my tests pass.