23

I am a Gradle rookie and I am not sure whether Gradle will start the new JVM when it runs the test set.

Like Passing JVM arguments to Gradle test task I want to pass some parameters to the JVM running the test set.

I added the following lines to build.gradle:

...
test {
    groovy {
        jvmArgs '-agentpath:/usr/lib/code_dependency_capturer.so' // add line
        srcDirs = ['src/test']
        if (!JavaVersion.current().isJava8Compatible()) {
            exclude '**/v8/*'
            exclude '**/vm8/*'
        }
    }
    resources {
        srcDirs = ['src/test-resources']
    }
}
...

But it tells me:

A problem occurred evaluating root project 'groovy'.
Could not find method jvmArgs() for arguments[-agentpath:/usr/lib/code_dependency_capturer.so] on source set 'test' of type org.gradle.api.internal.tasks.DefaultSourceSet. 

I googled this error but failed to solve it.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
nail fei
  • 2,179
  • 3
  • 16
  • 36

1 Answers1

29

Try setting the jvmArgs of the enclosing test task rather than trying to set them on groovy.

The error you are getting suggests that jvmArgs isn’t present on groovy.

Example:

...
test {
    jvmArgs '-agentpath:/usr/lib/code_dependency_capturer.so' // add line
    groovy {      
        srcDirs = ['src/test']
        ...
    }
    ...
}

This is just a guess as I don’t have a gradle setup handy on which to confirm but worth a try as jvmArgs is documented as a property for test:

https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle.api.tasks.testing.Test:jvmArgs

List<String> jvmArgs

The extra arguments to use to launch the JVM for the process. Does not include system properties and the minimum/maximum heap size.

Since jvmArgs is a list of String you can pass it multiple arguments, refer to:

http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html#_list_literals

Example:

  jvmArgs ["-Xarg1", "-Xarg2"]

For "-Dprop=value" system properties use the systemProperties of the test task instead:

https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle.api.tasks.testing.Test:systemProperties

Drew MacInnis
  • 8,267
  • 1
  • 22
  • 18
  • 1
    TL DR; jvmArgs is an array. – ThomasRS Mar 02 '19 at 22:40
  • ... present on the `test` task not `groovy` – Drew MacInnis Mar 02 '19 at 22:50
  • I need to run unit tests with different timeZone at one sub module. So I update like this: test{ jvmArgs '-Duser.country=US -Duser.language=en -Duser.timezone="Asia/Colombo"' } But stil it's not getting what I changed. any idea? – YAS_Bangamuwage Apr 02 '20 at 07:10
  • Setting like this solved above issue. test{ systemProperty "user.country", "US" systemProperty "user.language", "en" systemProperty "user.timezone", "Asia/Colombo" useTestNG() { suites "src/test/resources/testng.xml" } } – YAS_Bangamuwage Apr 02 '20 at 11:51
  • 1
    Original question wasn’t about system properties but I’ve updated my answer to show a few more examples and to highlight that jvmArgs is List and linked to the test task docs which is the best reference @YAS_Bangamuwage – Drew MacInnis Apr 02 '20 at 12:05
  • I can't making it work :/ ``` tasks.test{ jvmArgs("-XX:+ShowCodeDetailsInExceptionMessages") allJvmArgs = listOf("-XX:+ShowCodeDetailsInExceptionMessages") } ``` – apflieger May 07 '20 at 12:17