2014-07-30

Gradle and Dynamically Setting Test.forkEvery

I ran into an interesting problem today when customizing my teams Gradle build. I wanted to be able to dynamically set the forkEvery value for our test tasks. So, using common property techniques, wrote:

test {

    if(project.hasProperty("test.forkEvery")){
        println "Setting the tests to fork every ${project.getProperty("test.forkEvery")} for project ${project.name}"
        forkEvery = project.getProperty("test.forkEvery")
    }
}

Through simple observation I knew the tests were completing too fast. No errors, but something was off. I hard coded the value to 1 (not quoted), and that worked fine. Hmmmm. Considering Groovy would print 1000 for this:
def l = 1L; l = "1000"; println l;
I had to assume something must be happening with type conversion, and Gradle is not giving me an error for it. The fix was Long.parseLong:
test {

    if(project.hasProperty("test.forkEvery")){
        println "Setting the tests to fork every ${project.getProperty("test.forkEvery")} for project ${project.name}"
        forkEvery = Long.parseLong(project.getProperty("test.forkEvery"))
    }
}