Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass Java SystemProperties to a Gradle JunitTest

I got a JUnit test that relies upon a Java SystemProperty (more precisely the library used in it relies on it). I want to start the JUnit test from IntelliJ. Therefore I edited the run configuration and added VM options like "-DpropertyName=value". IntelliJ then starts gradle with something like this:

gradle cleanTest test --tests com.example.Test -DpropertyName=value

When I check from my Test with System.getProperty() / getProperties() the property was not passed and thus is null.

How can i pass the properties to the JunitTest?

like image 574
icyerasor Avatar asked Aug 11 '15 16:08

icyerasor


People also ask

How do I pass system property in Gradle?

Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command.

How do I pass JVM options to Gradle?

Try to use ./gradlew -Dorg. gradle. jvmargs=-Xmx16g wrapper , pay attention on -D , this marks the property to be passed to gradle and jvm. Using -P a property is passed as gradle project property.

How do I pass a Commandle argument to Gradle?

Types of Input Arguments When we want to pass input arguments from the Gradle CLI, we have two choices: setting system properties with the -D flag. setting project properties with the -P flag.

How do I set system properties in environment variables?

To get a specific system property you can use System. getProperty(String key) or System. getProperty(String key, String def) . Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime.


1 Answers

As it turns out, IntelliJ can only pass those JVM options to the JVM gradle runs in and gradle will not pass those to the tests. To specify a property for the JVM that will run the tests, either edit the build.gradle file and add:

test {
  systemProperty 'propertyName', 'value'
}

or, instead, add

test.jvmArgs System.getProperty("test.jvmArgs").split(" ")

to the build.gradle file and modify your run configuration VM options to be something like

-Dtest.jvmArgs="-DpropertyName=value -Dprop2=value2"
like image 79
icyerasor Avatar answered Oct 19 '22 23:10

icyerasor