如何为Junit单元测试设置JVM参数?

时间:2023-02-05 10:30:59

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 sufficient heap-space, and will be run internally on Win 7 64-bit machines - so redesigning the tests isn't a practical suggestion.)

我有一些Junit单元测试,需要大量的堆空间来运行,即1G。(他们测试的是一个webstart应用程序的内存密集型功能,该应用程序只运行足够的heap-space,并在内部运行Win 7 64位机器——所以重新设计测试并不是一个实际的建议。)

I am developing in Intellij IDEA, so I know I can set the JVM parameters (e.g. -Xmx1024M) for the test class. However, this is only for running the whole test class - if I want to run an individual test, I have to recreate the run congfigurations for that test method.

我正在开发Intellij IDEA,所以我知道我可以为测试类设置JVM参数(例如-Xmx1024M)。但是,这只是为了运行整个测试类——如果我想要运行一个单独的测试,我必须重新创建这个测试方法的运行丛集。

Also, those are IDE and box specific - so if I switch boxes (I develop on multiple machines) or one of my colleagues tries to run the tests, those settings are not transferred. (Also, other IDEs like Eclipse and NetBeans are used by my colleagues.) FWIW, we're using mercurial for source code control.

另外,那些是IDE和特定的盒子——所以如果我把盒子(我在多台机器上开发)或者我的一个同事试图运行测试,这些设置不会被转移。(另外,我的同事还使用了Eclipse和NetBeans等其他ide。)FWIW,我们使用mercurial进行源代码控制。

For the build cycle, we're using Maven, so I know how to specify the JVM parameters for that.

对于构建周期,我们使用Maven,因此我知道如何为其指定JVM参数。

So: - I'm looking for a way of specifying the JVM parameters that will apply for the whole test class and the individual test methods; and - I'd like to share those specification across IDEs on any machine (having picked up the code from the repository).

所以:-我正在寻找一种方法来指定将应用于整个测试类和单个测试方法的JVM参数;而且,我希望在任何机器上(从存储库中提取代码)共享这些规范。

7 个解决方案

#1


43  

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.

在IntelliJ中,您可以为每个运行配置指定默认设置。在运行/调试配置对话框中(用于配置堆每次测试),单击缺省值和JUnit。这些设置将自动应用于每个新的JUnit测试配置。我想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.

然而,没有一个简单的选项可以将这样的设置(至少在IntelliJ中)传输到不同的环境中。您可以将IntelliJ项目文件提交到您的存储库:它可能有用,但我不推荐它。

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

你知道如何将这些设置为maven-surefire-plugin。好。这是最便捷的方式(参见Ptomli的例子)。

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.

对于rest——您必须记住,JUnit测试用例只是一堆Java类,而不是一个独立的程序。它取决于runner(让它成为一个独立的JUnit运行器,您的IDE, maven-surefire-plugin来设置这些选项)。这就是说,没有“可移植”的方法来设置它们,所以不管运行者如何使用内存设置。

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.

举个例子:在开发一个servlet时,您不能定义Xmx参数—它取决于容器来定义它。您不能说:“这个servlet应该始终以Xmx=1G运行。

#2


56  

In Maven you can configure the surefire plugin

在Maven中,您可以配置surefire插件。

<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.

如果您使用Maven进行构建,那么这个配置将在源树中进行,并在进行测试时应用。查看Maven Surefire插件文档。

#3


15  

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

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

#4


13  

I agree with the others who said that there is no simple way to distribute these settings.

我同意其他人的观点,他们说没有简单的方法来分配这些设置。

For Eclipse: ask your colleagues to set the following:

对于Eclipse:请您的同事设置如下:

  • Windows Preferences / Java / Installed JREs:
  • Windows首选项/ Java /安装的JREs:
  • Select the proper JRE/JDK (or do it for all of them)
  • 选择合适的JRE/JDK(或者对所有的JDK都这样做)
  • Edit
  • 编辑
  • Default VM arguments: -Xmx1024m
  • 默认的VM参数:-Xmx1024m
  • Finish, OK.
  • 完成。

After that all test will run with -Xmx1024m but unfortunately you have set it in every Eclipse installation. Maybe you could create a custom Eclipse package which contains this setting and give it to you co-workers.

在此之后,所有的测试都将使用-Xmx1024m运行,但不幸的是,您已经在每个Eclipse安装中设置了它。也许您可以创建一个自定义的Eclipse包,其中包含这个设置,并将其提供给您的同事。

The following working process also could help: If the IDE cannot run a test the developer should check that Maven could run this test or not.

下面的工作流程也可以帮助:如果IDE不能运行测试,开发人员应该检查Maven是否可以运行这个测试。

  • If Maven could run it the cause of the failure usually is the settings of the developer's IDE. The developer should check these settings.
  • 如果Maven能够运行它,那么失败的原因通常是开发人员IDE的设置。开发人员应该检查这些设置。
  • If Maven also could not run the test the developer knows that the cause of the failure is not the IDE, so he/she could use the IDE to debug the test.
  • 如果Maven也不能运行测试,那么开发人员就知道失败的原因不是IDE,所以他/她可以使用IDE来调试测试。

#5


6  

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

根据这一支持问题,https://intellij-support.jetbrains.com/hc/en-us/community/posts/206165789- junitdefault -heap-size-overridden-。

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

如果是设置的话,对于一个IntelliJ junit测试运行的-Xmx参数将来自maven-surefire-plugin。

This pom.xml snippet

砰的一声。xml片段

        <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.

似乎将-Xmx1024参数传递到junit测试运行,使用IntelliJ 2016.2.4。

#6


1  

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:

一个eclipse专用的替代方法,仅限于java.library。path JVM参数允许将其设置为特定的源文件夹,而不是在另一个响应中建议的整个jdk:

  1. select the source folder in which the program to start resides (usually source/test/java)
  2. 选择要启动程序的源文件夹(通常是源/测试/java)
  3. type alt enter to open Properties page for that folder
  4. 键入alt键进入该文件夹的打开属性页。
  5. select native in the left panel
  6. 在左侧面板中选择本机。
  7. Edit the native path. The path can be absolute or relative to the workspace, the second being more change resilient.
  8. 编辑本机路径。路径可以是绝对的,也可以是相对于工作空间的,第二个是更具弹性的。

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

对于那些有兴趣了解为什么maven argline标签应该更倾向于系统属性的人来说,请看下面的例子:

Pick up native JNI files in Maven test (lwjgl)

在Maven测试中获取本地JNI文件(lwjgl)

#7


1  

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

可以使用systemPropertyVariables (java.protocol.handler. handler)。pkgs是您的JVM参数名称):

<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

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

#1


43  

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.

在IntelliJ中,您可以为每个运行配置指定默认设置。在运行/调试配置对话框中(用于配置堆每次测试),单击缺省值和JUnit。这些设置将自动应用于每个新的JUnit测试配置。我想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.

然而,没有一个简单的选项可以将这样的设置(至少在IntelliJ中)传输到不同的环境中。您可以将IntelliJ项目文件提交到您的存储库:它可能有用,但我不推荐它。

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

你知道如何将这些设置为maven-surefire-plugin。好。这是最便捷的方式(参见Ptomli的例子)。

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.

对于rest——您必须记住,JUnit测试用例只是一堆Java类,而不是一个独立的程序。它取决于runner(让它成为一个独立的JUnit运行器,您的IDE, maven-surefire-plugin来设置这些选项)。这就是说,没有“可移植”的方法来设置它们,所以不管运行者如何使用内存设置。

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.

举个例子:在开发一个servlet时,您不能定义Xmx参数—它取决于容器来定义它。您不能说:“这个servlet应该始终以Xmx=1G运行。

#2


56  

In Maven you can configure the surefire plugin

在Maven中,您可以配置surefire插件。

<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.

如果您使用Maven进行构建,那么这个配置将在源树中进行,并在进行测试时应用。查看Maven Surefire插件文档。

#3


15  

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

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

#4


13  

I agree with the others who said that there is no simple way to distribute these settings.

我同意其他人的观点,他们说没有简单的方法来分配这些设置。

For Eclipse: ask your colleagues to set the following:

对于Eclipse:请您的同事设置如下:

  • Windows Preferences / Java / Installed JREs:
  • Windows首选项/ Java /安装的JREs:
  • Select the proper JRE/JDK (or do it for all of them)
  • 选择合适的JRE/JDK(或者对所有的JDK都这样做)
  • Edit
  • 编辑
  • Default VM arguments: -Xmx1024m
  • 默认的VM参数:-Xmx1024m
  • Finish, OK.
  • 完成。

After that all test will run with -Xmx1024m but unfortunately you have set it in every Eclipse installation. Maybe you could create a custom Eclipse package which contains this setting and give it to you co-workers.

在此之后,所有的测试都将使用-Xmx1024m运行,但不幸的是,您已经在每个Eclipse安装中设置了它。也许您可以创建一个自定义的Eclipse包,其中包含这个设置,并将其提供给您的同事。

The following working process also could help: If the IDE cannot run a test the developer should check that Maven could run this test or not.

下面的工作流程也可以帮助:如果IDE不能运行测试,开发人员应该检查Maven是否可以运行这个测试。

  • If Maven could run it the cause of the failure usually is the settings of the developer's IDE. The developer should check these settings.
  • 如果Maven能够运行它,那么失败的原因通常是开发人员IDE的设置。开发人员应该检查这些设置。
  • If Maven also could not run the test the developer knows that the cause of the failure is not the IDE, so he/she could use the IDE to debug the test.
  • 如果Maven也不能运行测试,那么开发人员就知道失败的原因不是IDE,所以他/她可以使用IDE来调试测试。

#5


6  

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

根据这一支持问题,https://intellij-support.jetbrains.com/hc/en-us/community/posts/206165789- junitdefault -heap-size-overridden-。

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

如果是设置的话,对于一个IntelliJ junit测试运行的-Xmx参数将来自maven-surefire-plugin。

This pom.xml snippet

砰的一声。xml片段

        <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.

似乎将-Xmx1024参数传递到junit测试运行,使用IntelliJ 2016.2.4。

#6


1  

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:

一个eclipse专用的替代方法,仅限于java.library。path JVM参数允许将其设置为特定的源文件夹,而不是在另一个响应中建议的整个jdk:

  1. select the source folder in which the program to start resides (usually source/test/java)
  2. 选择要启动程序的源文件夹(通常是源/测试/java)
  3. type alt enter to open Properties page for that folder
  4. 键入alt键进入该文件夹的打开属性页。
  5. select native in the left panel
  6. 在左侧面板中选择本机。
  7. Edit the native path. The path can be absolute or relative to the workspace, the second being more change resilient.
  8. 编辑本机路径。路径可以是绝对的,也可以是相对于工作空间的,第二个是更具弹性的。

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

对于那些有兴趣了解为什么maven argline标签应该更倾向于系统属性的人来说,请看下面的例子:

Pick up native JNI files in Maven test (lwjgl)

在Maven测试中获取本地JNI文件(lwjgl)

#7


1  

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

可以使用systemPropertyVariables (java.protocol.handler. handler)。pkgs是您的JVM参数名称):

<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

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