I'm learning spring boot and have some tests using TestNG which run just fine when running from the IntelliJ Run Configuration, in which I pass: spring.profiles.active=dev
as one of several Environment Variables.
In my Groovy Pipeline I have:
sh "./gradlew clean build test -Dspring.profiles.active=dev --no-daemon"
I have a properties file under src/main/resources
:
application-dev.properties
My Spring Boot base class under src/test/java
is as follows:
@SpringBootTest
public class SpringBootTestBase extends AbstractTestNGSpringContextTests {
public static Date date = new Date();
public static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
public static String dt = formatter.format(date);
public static ExtentSparkReporter spark;
public static ExtentTest test;
public static ExtentReports extent;
public static String reportDestination = "build/test-results/report_" + dt + ".html";
@BeforeSuite
public void testReport(){
spark = new ExtentSparkReporter(reportDestination);
extent = new ExtentReports();
extent.attachReporter(spark);
spark.config().setDocumentTitle("Smoke Tests");
spark.config().setReportName("Smoke Tests");
spark.config().setTheme(Theme.STANDARD);
spark.config().setTimeStampFormat("EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'");
}
@Override
@BeforeClass(alwaysRun = true)
protected void springTestContextPrepareTestInstance() throws Exception {
super.springTestContextPrepareTestInstance();
}
@AfterMethod
public void tearDown(ITestResult result) throws IOException {
if (result.getStatus() == ITestResult.FAILURE) {
test.fail(result.getName() + " test case is failed. " + "<span class='badge badge-danger'> Fail </span>" + result.getThrowable());
} else if (result.getStatus() == ITestResult.SKIP) {
test.skip(result.getName() + " test case is skipped." + "<span class='badge badge-warning'> Skip </span>");
} else if (result.getStatus() == ITestResult.SUCCESS) {
test.pass(result.getName() + " test case is Passed." + "<span class='badge badge-success'> Success </span>");
}
}
@AfterSuite
public void tearDown(){
extent.flush();
}
My Test Classes extend from this:
@SpringBootTest
public class SmokeTestOne extends SpringBootTestBase {
// test code
My main App under src/main/java
:
@SpringBootApplication(exclude = MongoAutoConfiguration.class)
public class SmokeTestsApplication {
public static void main(String[] args) {
SpringApplication.run(SmokeTestsApplication.class, args);
}
}
In Jenkins, the failure is: No active profile set, falling back to default profiles: default
and then a bunch of failure to Autowire beans, presumably because it cannot find the @Value attributes that need to know the active profile (environment).
A beginners error no doubt, but what am I failing to do, configuration-wise?