【问题标题】:Grails 3.3.1 and passing system properties to test-appGrails 3.3.1 并将系统属性传递给 test-app
【发布时间】:2018-01-04 16:23:33
【问题描述】:

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

【问题讨论】:

  • 你可以在你的设置方法中使用System.setProperty(...),或者你可以例如在 IDE 的运行时配置中为测试/集成测试定义属性

标签: grails grails-3.3


【解决方案1】:

一个新的 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 and -D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-18
    • 2019-06-22
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 2015-06-30
    相关资源
    最近更新 更多