Spring Profiles in Cucumber Testing and Gradle

Viewed 24

I have an automation test suite that is using Cucumber and Spring running with a Gradle build. I am facing an issue where depending on environment the property file used will be different. I know there is a way to use Profile annotations to specify ContextConfiguration but I can not find any resources on how to get started on this.

I believe I have to create a ProfileManager Class but I am not sure how to connect this to the @ContectConfiguration annotating my Cucumber Step Definitions and the Property Sources in my Configuration Class.

Here are some of my code samples.

StepDefintions.java

@CucumberContextConfiguration
@ContextConfiguration(classes = {ApplicationConfiguration.class, DynamoDBConfiguration.class})
@TestPropertySource("classpath:application-test-local.properties")
//@TestPropertySource("classpath:application-test.properties")
public class StepDefinition {

        @Given("I set stuff up")
        public void myGivenStep() {
            app.assertTheThings()

        @When("I do things")
        public void myWhenStep() {
            app.checksStuff();
        }
    .....
}

ApplicationConfiguraton.java

@Configuration
public class ApplicationConfiguration {

    @Autowired
    private Environment env;

    .....
}

build.gradle

{
.....

task cucumber() {
    dependsOn assemble, testClasses
    doLast {
        javaexec {
            main='io.cucumber.core.cli.Main'
            classpath=configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output

            def env = System.getProperty('testingENV')
            println("Testing in " + env.toUpperCase() + ' Enviornment')
            systemProperties = System.properties

            def feature = ''
            if (project.hasProperty('feature')) {
                def arg1 = project.getProperty('feature')
                feature = arg1
            }


            args = [
                    '--plugin', 'pretty',
                    '--plugin', 'json:target/cucumber-report.json',
                    '--glue', 'com.testautomation.cucumber',
                        "src/test/resources/com/testautomation/cucumber/feature/${feature}"
                ]
            }
        }
    }
}

Basically I would like to use my testingENV system prop to define the profile and change which property source is being used without having to comment lines out everytime I am switching envs.

0 Answers
Related