Grails 3.3.1 并将系统属性传递给测试应用程序



我在集成测试中遇到了一个问题。我的代码使用系统属性(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
    }
}

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

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

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

我从Grails 3的这个问题的答案中改编了这一点:如何通过Gradle和-D为我的测试提供系统属性。

相关内容

最新更新