Requiring parameters for integration tests

I have code like the following in build.gradle.kts:

fun setSystemProperty(ctx: Test, propName: String) {
    val propValue = checkNotNull(project.findProperty(propName)) {
        "Please set project property `$propName`"
    }
    ctx.systemProperty(propName, propValue)
}

tasks {
    integrationTest {
        doFirst {
            setSystemProperty(this as Test, "foo")
            setSystemProperty(this as Test, "bar")
        }
    }
}

IDEA warns “No cast needed” about the second setSystemProperty call: is this because this as Test actually mutates this? I’m not sure if I’m doing this right.

What I actually want is to be able to require a number of parameters to be passed on the command-line (or via env vars) for my integration tests. These parameters are things like tokens for APIs. Is there a better way to do this?

I want nice error messages if users don’t set the right properties, not just a generic error, hence checkNotNull.

I think I’ve found a partial solution for this:

tasks {
    integrationTest {
        systemProperty("foo", project.property("foo")!!)
        systemProperty("bar", project.property("bar")!!)
    }
}

However, this means that IDEA can’t build my project unless I give it values for foo and bar, which I don’t want to do. (Although I guess I could give it dummy values; I’m happy to run my integration tests from the terminal.)