Search code examples
spring-bootgradlegradle-pluginpitest

Set Environment variables in gradle-pitest-plugin


Have a project that builds successfully and will pass all tests, except when running pitest.

build.gradle is setup to have some environment variables configured in ext. On bootRun or test tasks those environment variables are set. However, pitest does not have a property of environment variables that can be set like the former does. Looking for a way to set the information needed so the tests will run successfully as part of pitest. Without the variables the tests don't have the correct context and fail.

using `info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.3.0`

ext {

    setEnvironmentVariables = { environment ->
        environment.put('MAJOR_VERSION', cfg.MAJOR_VERSION)
        environment.put('CONTEXT_ROOT', cfg.CONTEXT_ROOT)
        environment.put('PROJECT_NAME', cfg.PROJECT_NAME)
        environment.put('PROJECT_DESCRIPTION', cfg.PROJECT_DESCRIPTION)
    }
}

bootRun {
    setEnvironmentVariables(environment)
}

Solution

  • Pitest is a JavaExec type task in grade. So environment variables can be set in build.gradle as:

    tasks.withType(org.gradle.api.tasks.JavaExec) {
        environment(String name, Object value)
    }
    

    Like if you want to set spring active profiles to 'ci' then set it as below:

    tasks.withType(org.gradle.api.tasks.JavaExec) {
        environment('spring.profiles.active', 'ci')
    }