3

我在集成测试中遇到了一个问题。我的代码使用系统属性 (System.getProperty(...) ),运行集成测试时我无法设置系统属性。关于如何定义在集成测试中运行的代码中可见的系统属性的任何想法?

我正在使用 Grails 3.3.1。精简的集成测试示例没有看到系统属性:

package injecttest

import grails.testing.mixin.integration.Integration
import grails.transaction.*
import org.springframework.beans.factory.annotation.Autowired
import spock.lang.Specification

@Integration
@Rollback
class C1ITSpec extends Specification {

    void "test system property readable inside test"() {
        String val = System.getProperty("var1", "ERROR")
        expect:"system variable to match value passed as '-Dvar1=OK' in command line"
            "OK" == val
    }
}
4

1 回答 1

0

一个新的 JVM 被派生来运行测试,因此您必须将命令行系统属性传递给测试的 JVM。

在使用 gradle 的 Grails 4 中,您可以在 gradle 文件中传递系统属性:

integrationTest.doFirst {
   systemProperties System.getProperties().subMap(["var1"])
}

我根据 Grails 3: How to give System property to my test via Gradle 和 -D的这个问题的答案改编了这个 。

于 2021-04-19T17:57:53.943 回答