Search code examples
gradlegradle-kotlin-dsl

How to pass system properties to gradle tests with kotlin DSL


I am trying to pass system properties to my gradle tests. With standard gradle, I would use

test {
    //... Other configurations ...
    systemProperties = System.properties
} 

With kotlin DSL, this won't run:

tasks.withType<Test> {
    useJUnitPlatform()
    options {
        systemProperties(System.getProperties())
    }
}

The problem is that properties is HashTable and expected type is Map<String, String>.

Right now, I use following workaround:

tasks.withType<Test> {
    useJUnitPlatform()
    val props = mutableMapOf<String, String>()
    System.getProperties().forEach {
        val key = it.key
        val value = it.value

        if (key is String && value is String) {
            props[key] = value
        }
    }
    options {
        systemProperties(props)
    }
}

Any clue? Thank you.


Solution

  • This works for me:

    systemProperties = System.getProperties().map { e -> Pair(e.key as String, e.value) }.toMap()
    

    To pass a single system property:

    systemProperty("property.name", System.getProperty("property.name"))