Askar Ibragimov Askar Ibragimov - 3 months ago 11
Java Question

Run JUnit test with external parameter using gradle

I have a Gradle project, in which gradle test (from the Java plugin https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html) would run JUnit tests.

The test is using a network connection, and I need to pass to the test some hostname, parameter from command line of the test, preferably attached to gradle command line, or otherwise, editing build.gradle file.

How I could pass to my test a certain string?

Answer

build.gradle

apply plugin: 'java'

test {
    systemProperty 'test.hostname', System.getProperty('test.hostname')
}

Test case

Assert.assertEquals("foo", System.getProperty("test.hostname"));

Then from command line

gradle test -Dtest.hostname=foo