3

I am bumping into an issue in my integration test. My code uses a system property (System.getProperty(...) ) and I am not being able to set the system property when running the integration test. Any idea on how to define system properties that are visible inside code running in integration test?

I am using Grails 3.3.1. Slimmed down example of integration test not seeing the system property:

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
    }
}
fassisrosa
  • 31
  • 2
  • 1
    You could just use `System.setProperty(...)` maybe in your setup method or you can e.g. define properties in your IDE's runtime config for tests/integration tests – Mike W Jan 05 '18 at 07:10

1 Answers1

0

A new JVM is forked to run the test, so you have to pass the command-line system properties to the test's JVM.

In Grails 4, which uses gradle, you can pass the system properties in your gradle file:

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

I adapted this from the answers to this question for Grails 3: How to give System property to my test via Gradle and -D.