Home > Software design >  How to run Java tests in VSCode with -parameters flag?
How to run Java tests in VSCode with -parameters flag?

Time:08-16

I opened a Gradle Spring boot project in VS Code, and it picked up all the unit tests as expected. Most of the tests run without any problems, but for the ones using the embedded database as I get this error:

org.springframework.dao.InvalidDataAccessApiUsageException: For queries with named parameters you need to use provide names for method parameters. Use @Param for query method parameters, or when on Java 8 use the javac flag -parameters.

Both the problem and the solution are pretty clear from the error, but I'd prefer not to set @Param everywhere and I can't figure out how to set this flag in VS Code when I'm running the tests. Is it possible to set javac flags in VS Code, and if so how do I do it?

I've tried adding several variations on the following to settings.json, without success:

"java.test.config": {
    "args": ["parameters"]
}

These tests work fine if I run them with gradle test or in IntelliJ.

CodePudding user response:

The -parameters should be the argument passed to Java Compiler, you can follow this guide: https://github.com/redhat-developer/vscode-java/wiki/Settings-Global-Preferences

  1. Set the setting java.settings.url pointing to the .prefs file. (You can create it wherever you want)
  2. In the .prefs file, add following line:
org.eclipse.jdt.core.compiler.codegen.methodParameters=generate
  • Related