JUnit 4 PermGen size overflow when running tests in Eclipse and Maven2

BlueLettuce16 picture BlueLettuce16 · Jul 27, 2012 · Viewed 18.6k times · Source

I'm doing some unit tests with JUnit, PowerMock and Mockito. I have a lot of test classes annotated with @RunWith(PowerMockRunner.class) and @PrepareForTest(SomeClassesNames) to mock final classes and more than 200 test cases.

Recently I've run into a problem of a PermGen space overflow when I run my entire test suite in Eclipse or Maven2. When I run my test one by one then each of them succeeds.

I did some research about that, however none of the advice helped me (I have increased PermGenSize and MaxPermSize). Recently I've found out that there is one class that contains only static methods and each method returns object mocked with PowerMockito. I'm wondering whether it is a good practice and maybe this is the origin of the problem because static variables are being shared between unit tests?

Generally speaking is it a good practice to have a static class with a lot of static methods which returns static mocked objects?

Answer

Jay Meyer picture Jay Meyer · Sep 13, 2013

I am getting PermGen errors from Junit in Eclipse too. But I am not using any mocking libs like Mockito nor EasyMock. However, my code base is large and my Junit tests are using Spring-Test (and are intense and complex test cases). For this, I need to truly increase the PermGen for all of my Junit tests.

Eclipse applies the Installed JRE settings to the Junit runs - not the eclipse.ini settings. So to change those:

  • Window > Preferences > Java > Installed JRE's
  • select the default JRE, Edit... button
  • add to Default VM Arguments: -XX:MaxPermSize=196m

This setting will allow Junit tests to run the more intense TestCases in Eclipse, and avoid the OutOfMemoryError: PermGen. This should also be low risk because most simple Junit tests will not allocate all of that memory.