Skip to content
Advertisement

How to pass gradle.properties key/value pairs into JUnit test when clicking VSCode “Run Test” codelens

We set several key/value pairs in our gradle.properties file. (eg LOGIN_UID, LOGIN_PWD)

We can successfully reference them in our build.gradle (eg $LOGIN_UID) and our JUnit tests (eg System.getProperty("LOGIN_UID") when running gradle test from the cmdline.

However when clicking on a Run Test codelens within a JUnit’s Java file within VSCode gradle.properties are not passed in. Guessing the Gradle test task is not being executed?

Greatly appreciate it if someone can help us understand and solve this running from a codelens.

Advertisement

Answer

Basically what you need is to add a setting in VS Code, something like this:

{
    "java.test.config": {
        "vmArgs": ["-DLOGIN_UID=id", ...]
    }
}

Then test runner will pick up them and pass the properties to JVM.

At last, the configuration can be persisted in your workspace’s .vscode/settings.json. If the properties have any sensitive data, like password, don’t commit them to the source control!

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement