如何在 JUnit 测试执行期间添加 Java 系统属性

发布于 2024-11-26 22:38:21 字数 362 浏览 1 评论 0原文

我需要为我的 JUnit 测试定义一个系统属性。我尝试了各种步骤将名称/值对传递到 gradle 中。 (我尝试过 Milestone-3 和 4)。 这些方法都不起作用:

  • 在 gradle.properties 文件中定义 systemProp.foo=bar
  • 在命令行传递 -Dfoo=bar 在
  • 命令行传递 -PsystemProp.foo=bar

我没有看到来自“gradle”的附加属性属性”虽然我不确定我应该这样做。但更重要的是,我将 System.properties 转储到静态初始化程序中,但该属性不存在。我需要将系统属性传递给正在运行的测试,以告诉它们正在运行的环境(本地、Jenkins 等)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

芸娘子的小脾气 2024-12-03 22:38:21

抱歉回答我自己的问题。刚刚此处偶然发现了解决方案:

测试{
系统属性=系统.属性
}

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

test {
systemProperties = System.properties
}

裸钻 2024-12-03 22:38:21

您还可以像这样定义单个属性:(

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

来自此答案

You can also define individual properties like this:

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

(From this answer)

谜泪 2024-12-03 22:38:21

对于安卓,你可以使用

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文