开发者

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:

  • defining a systemProp.foo=bar in the gradle.properties file
  • passing -Dfoo=bar at the command line
  • passing -PsystemProp.foo=bar on the command line

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 init开发者_运维知识库ializer 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.


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

test { systemProperties = System.properties }


You can also define individual properties like this:

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

(From this answer)


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'
        }
        ...
      }
    }
  }
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜