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?
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.
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.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With