0

Spring 2.4.5.
Gradle 6.5.1.

There are placeholders in PostgreSQL connection string in application.properties file under test folder.

spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://${PG_HOST}:${PG_PORT}/
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.max-size=5

In order to make tests working, I've added the following lines to build.gradle file:

test {
    ...
    systemProperty "PG_HOST", System.getProperty("PG_HOST")
    systemProperty "PG_PORT", System.getProperty("PG_PORT")
    ...
}

So, I'm able to tun test via command line as follows:

./gradlew test -DPG_HOST=localhost -DPG_PORT=5432

Then, I decided to run Gradle test task via IntelliJ Idea Gradle pane.

enter image description here I've added PG_HOST & PG_PORT as environment variables.

enter image description here

But the env variables are not being passed to Gradle test config, and test fail.

Any ideas on this matter?

Lesha Pipiev
  • 3,251
  • 4
  • 31
  • 65
  • if u would like to use environment variables you need to use System#getenv instead of System#getProperty. – Andrey B. Panfilov Jul 05 '22 at 10:50
  • Hi @AndreyB.Panfilov. Tried getenv, but no luck as well – Lesha Pipiev Jul 05 '22 at 11:10
  • 1
    well, seems to be intellij issue: https://youtrack.jetbrains.com/issue/IDEA-260217/Executing-gradle-in-IntelliJ-is-not-passing-environment-variables-eg-PATH-to-gradle – Andrey B. Panfilov Jul 05 '22 at 11:31
  • 1
    `-D` option is **not the same as environment variable property**. Also note that the variable you are specifying are for the Test process, not for the Gradle script usage. Better use `gradle.properties` to specify the properties for Gradle or also see this thread for means of setting the environment variables: https://stackoverflow.com/questions/36322536/how-to-set-an-environment-variable-from-a-gradle-build – Andrey Jul 05 '22 at 13:40

0 Answers0