I have below setup
I am able to run cucumber test with below configuration to test SpringBoot Application running locally using maven failsafe plugin.
Note: The configuration uses @SpringBootTest annotation to start the SubjectUnderTest(SUT) before the running the Test.
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.bdd.stepdefs")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME,
value = "pretty," +
"html:target/tests-reports/report.html," +
"json:target/tests-reports/report.json," +
"junit:target/tests-reports/cucumber-junit.xml,")
@CucumberContextConfiguration
@ActiveProfiles("local")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class FunctionalIT {
}
My Requirements:
-Dspring.profiles.active=local
or -Dspring.profile.active=qa
. And only that profile should be used by the Spring.I tried few options without much success so far, I faced below issues:
@SpringBootTest
annotation, but @CucumberContextConfiguration
is only allowed on single class.@CucumberContextConfiguration
needs to be applied on @SpringBootTest
or @ContextConfiguration
only.Any idea how to get the setup working for the above requirements ?
I solved the problem with the below changes.
package com.example.bdd.config;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@CucumberContextConfiguration
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME,
value = "pretty," +
"html:target/tests-reports/report.html," +
"json:target/tests-reports/report.json," +
"junit:target/tests-reports/cucumber-junit.xml")
public @interface CucumberTestSuite {
}
package com.example.bdd.env.test;
@CucumberTestSuite
@ConfigurationParameter(key = GLUE_PROPERTY_NAME,
value = "com.example.bdd.stepdefs," +
"com.example.bdd.env.test")
@ContextConfiguration(initializers = ApplicationInitializer.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class ComponentIT {
}
# Note: ApplicationInitializer takes care of spinning up required dependencies (mocks/stubs) for the Component Testing.
package com.example.bdd.env.acceptance;
@CucumberTestSuite
@ConfigurationParameter(key = GLUE_PROPERTY_NAME,
value = "com.example.bdd.stepdefs," +
"com.example.bdd.env.acceptance")
@ContextConfiguration
@TestPropertySource("classpath:application-${env}.properties")
@ActiveProfiles("${env}")
public class AcceptanceIT {
}
pom.xml
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*AcceptanceIT.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>acceptance-tests</id>
<properties>
<skip.surefire.tests>true</skip.surefire.tests>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/*AcceptanceIT.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Now for running
mvn clean verify
runs unit tests and ComponentIT by default (for local and CI environment)mvn clean verify -P acceptance-tests -Denv=qa
runs AcceptanceIT tests on qa
environment, like wise -Denv=stage
or -Denv=prod
to run tests against the stage
and prod
environments respectively.