Unable to access System property in Src code

I am adding a system property in build.gradle like this - System.setProperty ‘env’,systemProperty ‘env’ ?: ‘dev’

Then trying to access in cucumber step def class using System.getProperty(“env”) but it’s throwing Null pointer exception, however I am successfully able to print System.getProperty(“env”) value in build.gradle and other test classes which are not step def files. Can anyone suggest something ?

The jvm which is executing the Gradle build is different from the jvm which is executing your cucumber tests. How are you executing cucumber (which task are you using?). There’s probably an option to set system properties on the test jvm. Eg something like

cucumberTest {
  systemProperty('foo', 'bar')
} 

Thank you Lance, I wasn’t aware that JVM is different from Gradle and Cucumber, Now I understand why the system property value is printing in other classes but not in cucumber class.

I implemented the code as you suggested but still getting null pointer exception, my cucumber task is -

task cucumber() {
dependsOn assemble, compileTestJava
System.setProperty ‘env’,System.properties[‘env’] ?: ‘dev’
println System.getProperty(“env”) //printing value successfully
doLast {
javaexec {
main = “io.cucumber.core.cli.Main”
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = [’–plugin’, ‘pretty’, ‘–glue’, ‘CucumberTest’, ‘src/test/resources’]
}
}
}
Any guidance is appreciated.

1 Like

I’d guess something like

task cucumber(type: JavaExec) {
   dependsOn assemble, compileTestJava
   systemProperty('env', System.properties['env'] ?: 'dev') 
   main = “io.cucumber.core.cli.Main”
   classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
   args = [’–plugin’, ‘pretty’, ‘–glue’, ‘CucumberTest’, ‘src/test/resources’]
}
2 Likes

Thank you a lot , Now I am able to access provided System property in Cucumber class, can you please explain why it wasn’t working before,

Now I am able to access provided System property in Cucumber class,

Great!

can you please explain why it wasn’t working before

Now where’s the fun in that? You have all the information you need to investigate what you tried vs a working solution.

May be because JavaExec starts a JVM with the given classpath and I was setting it up in doLast{}

You were calling System.setProperty(x, y) whereas I’m calling JavaExec.systemProperty(x,y)

1 Like

Because the JVM is different in Gradle and Cucumber, when I am running gradle cucumber there is no report in build. Report is only coming when running gradle test.I am able to generate cucumber-html-report using pretty plugin but Any idea how can I get Gradle report(index.html ) as well.