How to Set Jvm Parameters for Junit Unit Tests

How to set JVM parameters for Junit Unit Tests?

In IntelliJ you can specify default settings for each run configuration. In Run/Debug configuration dialog (the one you use to configure heap per test) click on Defaults and JUnit. These settings will be automatically applied to each new JUnit test configuration. I guess similar setting exists for Eclipse.

However there is no simple option to transfer such settings (at least in IntelliJ) across environments. You can commit IntelliJ project files to your repository: it might work, but I do not recommend it.

You know how to set these for maven-surefire-plugin. Good. This is the most portable way (see Ptomli's answer for an example).

For the rest - you must remember that JUnit test cases are just a bunch of Java classes, not a standalone program. It is up to the runner (let it be a standalone JUnit runner, your IDE, maven-surefire-plugin to set those options. That being said there is no "portable" way to set them, so that memory settings are applied irrespective to the runner.

To give you an example: you cannot define Xmx parameter when developing a servlet - it is up to the container to define that. You can't say: "this servlet should always be run with Xmx=1G.

How to create a Spring Boot test that runs with specific JVM arguments

I solved this by adding a static bloc in the class and setting the system properties:

static {
System.setProperty("http.proxyHost", "myproxyserver.com");
System.setProperty("http.proxyPort", "80");
System.setProperty("https.proxyHost", "myproxyserver.com");
System.setProperty("https.proxyPort", "80");
}

Eclipse - passing VM parameter to every test in the project

We change the JRE Configuration in Eclipse (Preferences>Java>Installed JREs). There you can set default VM Arguments which are used for anything you run with that JRE.

Now simply configure your project to use this JRE (Java Build Path) and you should be on the right way.

Edit JRE - Eclipse

Where to set vmArgs for JUnit in VS Code?

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.

How to make IntelliJ IDEA run tests with -ea JVM option

Add -ea to the VM options of the active run/debug configuration. Add it to the Template JUnit configuration as well so that all the new configurations inherit this default (it should be already like this out of the box unless you've modified the template configuration).

current

template



Related Topics



Leave a reply



Submit