Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to add Environment Variables to a JUnit Tests in Gradle

Tags:

gradle

I've managed to get the test task to run my unit tests, but they fail because env properties I am using are not set, eg: String base=System.getenv("TESTNG_BASE_PATH");

So, I've done something like:

tasks.withType(Test) {
   systemProperty 'TESTNG_BASE_PATH','long\\path\\to\env\var\value'   
}

But I still get the same exception from my code that the file is not found, so its obviously not the right way of doing this.

So how to do this, please?

like image 778
Carmageddon Avatar asked Jan 09 '18 21:01

Carmageddon


1 Answers

If you are getting via System.getenv(...) you'll need to set an environment variable. I've also included a command line flag for switching on/off standard streams

tasks.withType(Test) { 
    environment 'TESTNG_BASE_PATH','long\\path\\to\env\var\value'
    testLogging.showStandardStreams = Boolean.parseBoolean(findProperty('showStandardStreams'))
}

To run you could do

./gradlew check -PshowStandardStreams=true
like image 120
lance-java Avatar answered Sep 28 '22 08:09

lance-java