I have a Java project with numerous unit tests. In Visual Studio Code, I can run all the tests from the “Testing” tab. Several fail, however, because I need to provide some required VM arguments for them to run properly. We use Maven as our build system.
I know the syntax to provide VM args, but I can’t find the launch configurations for the JUnit tests. I just auto-generated a launch.json
, but it doesn’t have a Junit configuration. Yet, VS Code clearly knows how to launch the unit tests, because when I tell it to do it (from the Testing pane), it runs them (even the ones that error-out).
This answer (Visual Studio Code launch.json file for JUnit?) indicates I can just right-click on the unit test to provide options, but all I get are:
- Run Test
- Debug Test
- Go to Test
- Hide Test
The auto-generated launch.json is pretty basic. The two configurations it generated are kind of useless, because it’s a library, not an executable program.
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Launch RunObject",
"request": "launch",
"mainClass": "com.ourcompany.RunObject",
"projectName": "OurCommon"
}
]
}
Where do I set the VM args for unit tests?
CodePudding user response:
For Java Tests
(Run from the editor,) We can use "java.test.config"!
With this settings.json
:
{
"java.test.config": {
"vmArgs": ["-Dfoo=bar", "-Dbar=baz"]
},
...
This test succeeds:
@Test
public void testFoo() {
assertEquals("bar", System.getProperty("foo"));
assertEquals("baz", System.getProperty("bar"));
}
(run from editor).
See here: https://code.visualstudio.com/docs/java/java-testing
..and completely different to:
launch.json
Where it is (also):
...,
"vmArgs": "-Dfoo=bar"
...
..but something totally different in vs code infrastructure.
See:
- https://code.visualstudio.com/docs/java/java-debugging
- https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
- https://code.visualstudio.com/docs/java/java-debugging#_configuration-options
See also:
tasks.json
(For "build tasks" (which can also be tests)) : https://code.visualstudio.com/docs/editor/tasks.