Java 如何通过 Gradle 和 -D 为我的测试提供系统属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21406265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:32:05  来源:igfitidea点击:

How to give System property to my test via Gradle and -D

javatestinggradle

提问by robkuz

I have a a Java program which reads a System property

我有一个 Java 程序,它读取系统属性

System.getProperty("cassandra.ip");

and I have a Gradle build file that I start with

我有一个 Gradle 构建文件

gradle test -Pcassandra.ip=192.168.33.13

or

或者

gradle test -Dcassandra.ip=192.168.33.13

however System.getPropertywill always return null.

但是System.getProperty将始终返回null

The only way I found was to add that in my Gradle build file via

我发现的唯一方法是通过以下方式将其添加到我的 Gradle 构建文件中

test {
    systemProperty "cassandra.ip", "192.168.33.13"
}

How Do I do it via -D

我如何通过 -D 做到这一点

采纳答案by Jeff Storey

The -P flag is for gradle properties, and the -D flag is for JVM properties. Because the test may be forked in a new JVM, the -D argument passed to gradle will not be propagated to the test - it sounds like that is the behavior you are seeing.

-P 标志用于 gradle 属性,-D 标志用于 JVM 属性。因为测试可能会在新的 JVM 中分叉,所以传递给 gradle 的 -D 参数不会传播到测试 - 听起来这就是您所看到的行为。

You can use the systemProperty in your testblock as you have done but base it on the incoming gradle property by passing it with it -P:

您可以test像以前一样在块中使用systemProperty,但通过将其传递给传入的 gradle 属性 -P 将其作为基础:

test {
    systemProperty "cassandra.ip", project.getProperty("cassandra.ip")
}

or alternatively, if you are passing it in via -D

或者,如果您通过 -D 传递它

test {
    systemProperty "cassandra.ip", System.getProperty("cassandra.ip")
}

回答by MrSpock

Came across this very much problem, except i don't want to list all properties given on the commandline in the gradle script again. Therefore i send all system properties to my test

遇到了这个非常多的问题,除了我不想再次在 gradle 脚本中列出命令行上给出的所有属性。因此我将所有系统属性发送给我的测试

task integrationTest(type: Test) {
    useTestNG()
    options {
        systemProperties(System.getProperties())
    }
}

回答by avivr

I had a case where I needed to pass multiplesystem properties into the test JVM but not all(didn't want to pass in irrelevant ones). Based on the above answers, and by using subMapto filter the ones I needed, this worked for me:

我有一个案例,我需要将多个系统属性传递到测试 JVM 但不是全部(不想传递不相关的)。基于上述答案,并通过使用subMap来过滤我需要的答案,这对我有用:

task integrationTest(type: Test) {
    // ... Do stuff here ...
    systemProperties System.getProperties().subMap(['PROP1', 'PROP2'])
}

In this example, only PROP1and PROP2will be passed in, if they exist in gradle's JVM.

在这个例子中,只有PROP1PROP2才会被传入,如果它们存在于 gradle 的 JVM 中。

回答by Eron Wright

Here's a variant that passes numerous project properties to the test JVM as system properties. I prefer project properties over system properties to increase flexibility.

这是一个将大量项目属性作为系统属性传递给测试 JVM 的变体。我更喜欢项目属性而不是系统属性以增加灵活性。

task intTest(type: Test) {
    systemProperties project.properties.subMap(["foo", "bar"])
}

Which may be passed on the command-line:

可以在命令行上传递:

 $ gradle intTest -Pfoo=1 -Pbar=2

And retrieved in your test:

并在您的测试中检索:

String foo = System.getProperty("foo");

回答by Olivier B.

So I've stumbled on that issue today as well, and what worked for me was the following:

所以我今天也偶然发现了这个问题,对我有用的是以下内容:

ext.env='prod'
test {
  systemProperty 'env', System.properties['env'] ?: "${env}"
  println "# test environment: " + systemProperties['env']
  ...
}

I'm calling my test task using -Penv=devand I get my 'dev' value in my print, or 'prod' if I do not send any value, which is the expected behavior for me.

我正在使用-Penv=dev调用我的测试任务,并且在我的打印中获取我的“dev”值,或者如果我不发送任何值,则获取“prod”值,这是我的预期行为。

Value is also accessible on java side, using System.getProperty("env").

值也可以在 java 端访问,使用System.getProperty("env")

My conclusion on the matter is that input value (parameter) is actually stored under System, making it accessible through either System.properties['env']or System.getProperty("env"), whereas output (system property) is stored in a systemPropertiesarray, making it readable through systemProperties['env'].

我对此事的结论是,输入值(参数)实际上存储在System下,使其可以通过System.properties['env']System.getProperty("env") 访问,而输出(系统属性)存储在一个systemProperties数组,使其可以通过systemProperties['env']读取。