How to set JVM parameters for Junit Unit Tests?

前端 未结 8 700
青春惊慌失措
青春惊慌失措 2020-11-28 03:47

I have some Junit unit tests that require a large amount of heap-space to run - i.e. 1G. (They test memory-intensive functionality for a webstart app that will only run with

相关标签:
8条回答
  • 2020-11-28 03:57

    According to this support question https://intellij-support.jetbrains.com/hc/en-us/community/posts/206165789-JUnit-default-heap-size-overridden-

    the -Xmx argument for an IntelliJ junit test run will come from the maven-surefire-plugin, if it's set.

    This pom.xml snippet

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>-Xmx1024m</argLine>
                </configuration>
            </plugin>
    

    seems to pass the -Xmx1024 argument to the junit test run, with IntelliJ 2016.2.4.

    0 讨论(0)
  • 2020-11-28 04:05

    If you set it in Java code, you can set it like this

          static {
            System.getProperties().setProperty("env", "test");
            System.getProperties().setProperty("spring.application.name", "spring-example");
        }
    

    reference:

    https://stackoverflow.com/a/60654275/4712855

    https://stackoverflow.com/a/10774432/4712855

    0 讨论(0)
  • 2020-11-28 04:07

    In Maven you can configure the surefire plugin

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.9</version>
        <configuration>
            <argLine>-Xmx256M</argLine>
        </configuration>
    </plugin>
    

    If you use Maven for builds then this configuration will be carried in the source tree and applied when tests are carried out. See the Maven Surefire Plugin documentation.

    0 讨论(0)
  • 2020-11-28 04:09

    An eclipse specific alternative limited to the java.library.path JVM parameter allows to set it for a specific source folder rather than for the whole jdk as proposed in another response:

    1. select the source folder in which the program to start resides (usually source/test/java)
    2. type alt enter to open Properties page for that folder
    3. select native in the left panel
    4. Edit the native path. The path can be absolute or relative to the workspace, the second being more change resilient.

    For those interested on detail on why maven argline tag should be preferred to the systemProperties one, look, for example:

    Pick up native JNI files in Maven test (lwjgl)

    0 讨论(0)
  • 2020-11-28 04:16

    Parameters can be set on the fly also.

    mvn test -DargLine="-Dsystem.test.property=test"
    

    See http://www.cowtowncoder.com/blog/archives/2010/04/entry_385.html

    0 讨论(0)
  • 2020-11-28 04:16

    You can use systemPropertyVariables (java.protocol.handler.pkgs is your JVM argument name):

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <configuration>
            <systemPropertyVariables>
                <java.protocol.handler.pkgs>com.zunix.base</java.protocol.handler.pkgs>
                <log4j.configuration>log4j-core.properties</log4j.configuration>
            </systemPropertyVariables>
        </configuration>
    </plugin>
    

    http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

    0 讨论(0)
提交回复
热议问题