Passing system property(user.home) to junit test classes not working for me


(prantorbora) #1

Passing system property(user.home) to junit test classes not working for me. I can print the user.home system property correctly in build.gradle(see below), but in my junit test class when I do System.getProperty(“user.home”) I dont see the value.

Also I see that gradle is not looking for gradle.properties in the user.home directory that I specify only looks at folder where I start gradle from.

============================================================== apply plugin: ‘java’ repositories {

mavenCentral()

flatDir dirs: [’/home/pbora/mydata/share/development/GitWorkArea/integration/integration-lib/internal-build/rib’, ‘lib’]

dependencies {

testCompile group: ‘rib-unittest’, name: ‘rib-unittest’, version: ‘+’

testCompile ‘junit:junit:4.8.1+’

testCompile ‘rib-private-kernel:rib-private-kernel:+’

testCompile ‘rib-public-api:rib-public-api:+’

}

} println “System.properties=” + System.properties

sourceSets.main.java.srcDirs ‘src’ sourceSets.test.java.srcDirs ‘srctest’ =======================================================================

What am i missing?

Thanks, PB


(spencer_allain) #2

Does your test code programmatically not do what you expect when you examine the user.home system property, or is it that you cannot see any value when you do a System.out from your unit test?

System.out and System.err are only written to the build/test-results/TEST-.xml file and not to the console when running unit tests.

If you really need to see stdout/stderr when running tests, then you can add the following line to your build.gradle:

test.testLogging.showStandardStreams = true

I cannot replicate the issue with a unit test not being able to examine SystemProperties based upon the sample build file you’ve listed (although I’m assuming there isn’t anything within the “rib” packages that are directly manipulating system properties other than trying to read them).

-Spencer


(Peter Niederwieser) #3

Tests are always run in a separate JVM. Hence you must set the system properties for that JVM:

test {

// set a single system property

systemProperty “user.home”, “/foo/bar”

// set multiple system properties at once

systemProperties someMap

}


(spencer_allain) #4

Likely what prnatorboar intended was that an overridden value for ‘user.home’ is not accessible by a test class, and that is related to system properties not being inherited by the forked JVM for tests (which is exactly what Peter is referring to).

I got hung up on the ‘user.home’ property and the phrase “I dont see the value” inside of a test class, and focused upon visualizing the value.

Also, the question noted that it did not recognize a gradle.properties file within user.home. That would be correct, as the gradle user home (environment variable GRADLE_USER_HOME, or system property gradle.user.home) default location is a subdirectory called “.gradle” underneath the java ‘user.home’ directory.

gradle.properties (either in project or gradle.user.home) will not be utilized by test cases unless explicitly assigned as Peter highlights.

-Spencer


(prantorbora) #5

Thanks for the reply, if the junit tests are run in seperate JVM process that does not inherit the properties from the parent JVM process the behavior I am seeing is consistent. Now my obvious follow up questions.

  1. Is there a way to configure gradle to run all tests in the same JVM i.e. not fork child processes? 2) Is there a way to tell gradle to inherit all properties from the parent process when child processes are forked?

Thanks, PB


(Peter Niederwieser) #6

ad 1) If you are asking whether there is a way to run the tests in the Gradle JVM, then the answer is no.

ad 2) If you really want to, you can always do ‘test.systemProperties System.properties’.