'How to programmatically set Spring profile in Cucumber
I have begun to use cucumber tests for my Spring application. I set active Spring profile by passing a JVM argument spring.profiles.active=testProfile.
Is there any way to do this programmatically? Something like:
@RunWith(Cucumber.class)
@Cucumber.Options(...)
@ActiveProfiles("testProfile")
public class MyCucumberTest
Using cucumber 1.1.6
Solution 1:[1]
I'm not sure this is the proper way to do it, but at least it get it working for me. I'm using cucumber version 1.2.4
I specialized the Cucumber junit runner to set the wanted spring profile in the constructor:
public class SpringProfileCucumber extends Cucumber {
public SpringProfileCucumber(Class clazz) throws InitializationError, IOException {
super(clazz);
System.setProperty("spring.profiles.active", "testProfile");
}
}
In the test use the SpringProfileCucumber junit runner
@RunWith(SpringProfileCucumber.class)
@CucumberOptions(features = {"src/test/java/tests/cucumber/test.feature"}, glue = { "tests.cucumber"})
public class MyCucumberTest {
// ... the test
}
Solution 2:[2]
This is becoming out-dated. The use of cucumber.xml is not applicable for cucumber-jvm.
I have a similar problem, trying to use:
@ActiveProfiles(value = "DEV", inheritProfiles = false)
@IfProfileValue(name= "ENV", value = "DEV")
with a command line of:
-DENV=DEV -Dspring.active.profiles=DEV
It seems that these directives don't work with the spring-cucumber library (1.1.5, at least).
Solution 3:[3]
Extending on @perseger's answer, the following specialised runner allows you to use the @ActiveProfiles annotation
public class SpringProfileCucumber extends Cucumber {
public SpringProfileCucumber(Class clazz)
throws InitializationError, IOException {
super(clazz);
ActiveProfiles ap = (ActiveProfiles) clazz
.getAnnotation(ActiveProfiles.class);
if (ap != null) {
System.setProperty("spring.profiles.active",
String.join(",", ap.value()));
}
}
}
In the test, you can then use
@RunWith(SpringProfileCucumber.class)
@CucumberOptions(...)
@ActiveProfiles("testProfile")
public class RyvrTests {
}
Solution 4:[4]
You can provide an environment variable for spring boot to use as the active profile. Using the @BeforeClass annotated method allows you to set desired profile before the application test starts
@RunWith(Cucumber.class)
@Cucumber.Options(...)
public class MyCucumberTest {
@BeforeClass
public static void setUp() {
System.setProperty("spring.profiles.active", "testProfile");
}
}
Solution 5:[5]
Alternatively, you can also create an annotation to activate a profile for cucumber. It looks like this:
import java.lang.annotation.*;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ContextConfiguration
@ActiveProfiles("test")
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = FeatureTestConfiguration.class)
public @interface FeatureFileSteps {
}
Quoted from my answer from this question:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | justderb |
| Solution 3 | Tom Howard |
| Solution 4 | PaulNUK |
| Solution 5 | Community |
