2

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

Konrad
  • 355
  • 6
  • 18
perseger
  • 77
  • 2
  • 11
  • How about simply putting in a static block. public class MyCucumberTest{static{System.setproperty("spring.profiles.active","testProfile")}} – Surendra Rathore Sep 15 '20 at 20:43

5 Answers5

3

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 }

perseger
  • 77
  • 2
  • 11
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).

justderb
  • 2,835
  • 1
  • 25
  • 38
Jax Gibb
  • 21
  • 2
2

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 {

}
Tom Howard
  • 6,516
  • 35
  • 58
1

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");
    }
}
PaulNUK
  • 4,774
  • 2
  • 30
  • 58
0

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:

How to activate spring boot profile with cucumber

Community
  • 1
  • 1
BitfulByte
  • 4,117
  • 1
  • 30
  • 40