How to set JVM parameters for Junit Unit Tests?

前端 未结 8 701
青春惊慌失措
青春惊慌失措 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 04:18

    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:

    • Windows Preferences / Java / Installed JREs:
    • Select the proper JRE/JDK (or do it for all of them)
    • Edit
    • Default VM arguments: -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.

    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.

    • 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.
    • 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.
    0 讨论(0)
  • 2020-11-28 04:21

    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)
提交回复
热议问题