java junit 启动参数,如何为Junit单元测试设置JVM参数?

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

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.

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.

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

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用IntelliJ IDEA进行单元测试时,可以通过设置运行配置来指定JVM的内存参数。你可以按照以下步骤进行设置: 1. 打开项目并导航到要进行单元测试的类或方法。 2. 右键单击该类或方法,然后选择"Run"或"Debug"。 3. 在弹出的菜单中,选择"Edit Configurations"。 4. 在配置窗口中,找到"VM options"字段,并在该字段中输入你想要设置JVM参数。例如,如果你想要设置最大堆内存为512MB,可以输入"-Xmx512m"。 5. 点击"Apply"或"OK"保存配置。 现在,当你运行或调试该单元测试时,IntelliJ IDEA将使用你设置JVM参数来运行测试代码。这样,你就可以灵活地调整内存参数以满足你的需求了。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [2021新版intelliJ idea 单元测试设置jvm参数的地方](https://blog.csdn.net/qq_27093465/article/details/121381356)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [自动化测试之idea+gradle+JUnit的使用(参数化测试与非参数化测试)](https://download.csdn.net/download/weixin_38502428/14852672)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值