How to give System property to my test via Gradle and -D
Asked Answered
E

7

142

I have a a Java program which reads a System property

System.getProperty("cassandra.ip");

and I have a Gradle build file that I start with

gradle test -Pcassandra.ip=192.168.33.13

or

gradle test -Dcassandra.ip=192.168.33.13

however System.getProperty will always return null.

The only way I found was to add that in my Gradle build file via

test {
    systemProperty "cassandra.ip", "192.168.33.13"
}

How Do I do it via -D

Expulsive answered 28/1, 2014 at 13:2 Comment(2)
What happens when you use gradle -Dcassandra.ip=192.168.33.13? Anyway, the test task forks one or several new JVMs. So you'll have to pass properties explicitely. Nobody forces you to hardcode their value in the build, though.Smuts
Also take a look in this answer: #23689554Hydrograph
K
180

The -P flag is for gradle properties, and the -D flag is for JVM properties. Because the test may be forked in a new JVM, the -D argument passed to gradle will not be propagated to the test - it sounds like that is the behavior you are seeing.

You can use the systemProperty in your test block as you have done but base it on the incoming gradle property by passing it with it -P:

test {
    systemProperty "cassandra.ip", project.getProperty("cassandra.ip")
}

or alternatively, if you are passing it in via -D

test {
    systemProperty "cassandra.ip", System.getProperty("cassandra.ip")
}
Kershner answered 28/1, 2014 at 13:17 Comment(5)
This doesn't work for me (tested using System.getProperties().stringPropertyNames().forEach(System.out::println); in the Java code, it doesn't appear)Variation
Warning: getProperty throws MissingPropertyException if property is not found. Use Eron's answer instead: https://mcmap.net/q/159335/-how-to-give-system-property-to-my-test-via-gradle-and-dAmylase
Adding default values to gradle.properties will prevent the MissingPropertyException.Jansson
I can get neither of these methods to work. Whatever I do myProperty is always null. This includes all of the methods mentioned below, too. I am wondering if this is something to do with a more recent version, as all comments are from 2018? @Variation did you find a solution to this?Lucifer
@HesterLyons I only wanted to know if the build was testing or normally running, so I used the property gradle itself adds when testing; you can see the code here: github.com/CLOVIS-AI/wildfyre-java/blob/master/src/main/java/…Variation
R
35

Came across this very much problem, except i don't want to list all properties given on the commandline in the gradle script again. Therefore i send all system properties to my test

task integrationTest(type: Test) {
    useTestNG()
    options {
        systemProperties(System.getProperties())
    }
}
Retinoscope answered 10/2, 2016 at 16:13 Comment(3)
Be careful when doing this. It might easily break Gradle's up-to-date checks when system properties change during invocations.Corelli
Same for JavaExec tasks. The props were present in the task, but not in the java program, unless the were set again in the task.Boxberry
Same problem as OP, adding options to test task helped: tasks.named('test') {... options {systemProperties(System.getProperties())}Oraorabel
P
13

I had a case where I needed to pass multiple system properties into the test JVM but not all (didn't want to pass in irrelevant ones). Based on the above answers, and by using subMap to filter the ones I needed, this worked for me:

task integrationTest(type: Test) {
    // ... Do stuff here ...
    systemProperties System.getProperties().subMap(['PROP1', 'PROP2'])
}

In this example, only PROP1 and PROP2 will be passed in, if they exist in gradle's JVM.

Peking answered 20/10, 2016 at 13:1 Comment(0)
U
10

Here's a variant that passes numerous project properties to the test JVM as system properties. I prefer project properties over system properties to increase flexibility.

task intTest(type: Test) {
    systemProperties project.properties.subMap(["foo", "bar"])
}

Which may be passed on the command-line:

 $ gradle intTest -Pfoo=1 -Pbar=2

And retrieved in your test:

String foo = System.getProperty("foo");
Ulcer answered 30/3, 2017 at 8:15 Comment(4)
When running System.getProperty("someprop") using that subMap method, I got {someprop=foo} instead of foo. I had to use systemProperty "foo", project.properties.subMap(["foo"]).get("foo") in build.gradleAmylase
@YngvarKristiansen where (how) were you using systemProperty "foo"? i.e. I'm asking to see the full line of code where this was used? I'm trying everything suggested in this question and still Gradle is not passing any arguments. Hoping this might resolve!Lucifer
@HesterLyons I'm sorry, I didn't keep my code, so I don't know anymore :\ I agree that it looks out of place.Amylase
Thank you @YngvarKristiansen. I have since worked out that my problem was being caused by the junit gradle plugin which was included in my gradle.build. Very odd.Lucifer
I
2

So I've stumbled on that issue today as well, and what worked for me was the following:

ext.env='prod'
test {
  systemProperty 'env', System.properties['env'] ?: "${env}"
  println "# test environment: " + systemProperties['env']
  ...
}

I'm calling my test task using -Penv=dev and I get my 'dev' value in my print, or 'prod' if I do not send any value, which is the expected behavior for me.

Value is also accessible on java side, using System.getProperty("env").

My conclusion on the matter is that input value (parameter) is actually stored under System, making it accessible through either System.properties['env'] or System.getProperty("env"), whereas output (system property) is stored in a systemProperties array, making it readable through systemProperties['env'].

I answered 29/7, 2019 at 10:12 Comment(1)
Work for me. thanks. in my case I was need to pass github token to tests by system properties.Wo
A
2

Here is something that worked for me

//in build.gradle file

    tasks.withType(Test) {
        systemProperties = [
           ip: System.getProperty('ip', '192.168.33.13'),
        ]
    }

    task integrationTests(type: Test){
        useTestNG()
    }

Suppose if you are using TestNG, you can add the annotation @Parameters as shown below

  public class IpAddress {
    @Test
    @Parameters("ip")
    public void printIpAddress(String ip) {
        System.out.println(ip);
    }
 }

Now you are good to execute a gradlew command

./gradlew clean -Dip="xx.xx.xx.xx" integrationTests --tests "IpAddress"

If you want to use @DataProvider to pass the test data, you can pass it like below and execute the same above gradle command to run the test

 public class IpAddress {
    @DataProvider(name = "GetIP")
    private static Object[][] getIp() {
        return new Object[][]{
                //if -Dip is not provided in command, then by default it gets the value assigned in build.gradle file i.e.'192.168.33.13'
                {System.getProperty("ip")}, 
        };
    }

    @Test(dataProvider = "GetIP")
    public void printIpAddress(String ip) {
        System.out.println(ip);
    }
}
Adhesion answered 5/7, 2021 at 17:56 Comment(0)
W
0

In my case I was need to pass github token to tests by system properties.

I have tried this:

systemProperty "github.token", System.getProperty("github.token")

But give me a error, so a passed a default value:

ext.github_token='TOKEN'
test {
    systemProperty 'github.token', System.properties['github.token'] ?: "${github_token}"
}
Wo answered 14/5, 2023 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.