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

后端 未结 3 819
误落风尘
误落风尘 2020-12-19 02:49

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 app

相关标签:
3条回答
  • 2020-12-19 02: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'
            }
            ...
          }
        }
      }
    }
    
    
    0 讨论(0)
  • 2020-12-19 02:57

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

    test { systemProperties = System.properties }

    0 讨论(0)
  • 2020-12-19 03:01

    You can also define individual properties like this:

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

    (From this answer)

    0 讨论(0)
提交回复
热议问题