Mr.Tr33
Mr.Tr33

Reputation: 868

Gradle pass SystemProperties to JUnit test

How can I use gradle parameters in my JUnit test cases?

I tried this:

build.bradle:

test {
    systemProperty 'brand', System.properties['brand'] ? project.brand : 'abc'
}

In my test I displayd all properties with:

System.properties.each { k,v->
    log.info("$k = $v")
}

With that code I get a lot of properties, except for brand.

Is there another way to get brand into my test?

// I don't know if it's important but I'm using Gebish or rather Selenium for my tests too.

Upvotes: 0

Views: 850

Answers (2)

Mr.Tr33
Mr.Tr33

Reputation: 868

My solution is now:

build.gradle (in the test task):

systemProperty "brand", project.hasProperty("brand") ? project.brand : "abc" 

GebConfig.groovy:

brand = System.getProperty("brand") ? System.getProperty("brand") : "abc"

here abc again, because IntelliJ does not run the task properly, but it does through the command line

in the test itself:

def brand = browser.config.getRawConfig().getProperty("brand").toString(‌​)

Upvotes: 0

Gergely Toth
Gergely Toth

Reputation: 6977

Don't know why using systemProperty does not work in the test configuration, but for a workaround you can set the systemProperties variable like:

test {
    System.properties.'brand' = System.properties['brand'] ? project.brand : 'abc'
    systemProperties = System.properties
}

Or if you don't want to modify System.properties you can set systemProperties like

test {
    systemProperties = System.properties + ['brand1': System.properties['brand'] ? project.brand : 'abc']

}

Upvotes: 1

Related Questions