James A Wilson
James A Wilson

Reputation: 14691

How can I add a Java system property during JUnit Test Execution

I need to define a system property for my JUnit tests. I've tried various steps to pass the name/value pair into gradle. (I've tried Milestone-3 and 4). None of these approaches worked:

I don't see the additional properties from "gradle properties" though I'm not sure I should. But more importantly, I dumped the System.properties in a static initializer and the property is not there. I need to pass System properties to the running tests to tell them what environment (local, Jenkins, etc) they are running in.

Upvotes: 10

Views: 5504

Answers (3)

kevin liu
kevin liu

Reputation: 53

with android, you can use

android {
  ...
  testOptions {
    ...
    // Encapsulates options for local unit tests.
    unitTests {
      // By default, local unit tests throw an exception any time the code you are testing tries to access
      // Android platform APIs (unless you mock Android dependencies yourself or with a testing
      // framework like Mockito). However, you can enable the following property so that the test
      // returns either null or zero when accessing platform APIs, rather than throwing an exception.
      returnDefaultValues true

      // Encapsulates options for controlling how Gradle executes local unit tests. For a list
      // of all the options you can specify, read Gradle's reference documentation.
      all {
        // Sets JVM argument(s) for the test JVM(s).
        jvmArgs '-XX:MaxPermSize=256m'

        // You can also check the task name to apply options to only the tests you specify.
        if (it.name == 'testDebugUnitTest') {
          systemProperty 'debug', 'true'
        }
        ...
      }
    }
  }
}

Upvotes: 0

Paul Hicks
Paul Hicks

Reputation: 13999

You can also define individual properties like this:

test {
  systemProperty "file", "test.story"
}

(From this answer)

Upvotes: 4

James A Wilson
James A Wilson

Reputation: 14691

Sorry to answer my own question. Just stumbled upon the solution here:

test { systemProperties = System.properties }

Upvotes: 14

Related Questions