Using PowerMockito.mockStatic() with Drools throws java.lang.OutOfMemoryError: PermGen space

200 views
Skip to first unread message

pavitra

unread,
May 13, 2016, 8:27:53 PM5/13/16
to Drools Usage
Hello,

I have written Junit tests by mocking some methods using PowerMockito for testing Drools Rules.
The methods that I am mocking are static methods. I am first loading the Drools Rule Engine in 
the setup method and then using 
PowerMockito.mockStatic(StaticClass.class);

After that I am using the following line inside each test case to return different values depending on the
test case:
Mockito.when(StaticClass.StaticMethod(param)).thenReturn(value); 

I have the following annotations set at the beginning of the test class:

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.management.*", "javax.security.*" , "javax.crypto.*", "org.bouncycastle.*"})
@PrepareForTest({StaticClass.class})

I am using drools-6.3.0 and powermock-1.6.1 in my implementation.
The test cases executes properly in Eclipse, however when I try to build it using ant, it throws the following exception:

    [junit] Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
    [junit] at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createCDATASection(CoreDocumentImpl.java:585)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter.formatOutput(XMLJUnitResultFormatter.java:328)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter.setSystemOutput(XMLJUnitResultFormatter.java:122)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.sendOutAndErr(JUnitTestRunner.java:827)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:575)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1182)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:1033)
    [junit] Exception in thread "Thread-0" java.lang.OutOfMemoryError: PermGen space
    [junit] Exception in thread "Thread-2" java.lang.OutOfMemoryError: PermGen space
    [junit] Exception in thread "Thread-4" java.lang.OutOfMemoryError: PermGen space
    [junit] Exception in thread "Thread-3" java.lang.OutOfMemoryError: PermGen space
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec

BUILD FAILED

1) I tried increasing the PermGen space using 
   export ANT_OPTS="-Xmx2g -XX:MaxPermSize=512m" and 
   export JVM_ARGS="-Xmx1024m -XX:MaxPermSize=256m"
by varying the size. But the issue still exists. 

2) I tried adding @PowerMockIgnore("org.drools.*")  annotation but that did not help either.

3) This issue does not occur when using Mockito for non-static methods but as per my requirement,
all my methods have to be static only. Eg:

NonStaticClass obj = Mockito.mock(NonStaticClass.class);


Also this is my first time using PowerMockito with Drools, so is there something that I am missing?

Thanks,
Pavitra

pavitra

unread,
May 18, 2016, 7:29:26 PM5/18/16
to Drools Usage
Hello All,

I fixed the issue by setting the jvmarg parameter in the junit ant task as follows:

<jvmarg value="-XX:MaxPermSize=512m"></jvmarg>


I have also used fork="true" in the junit and batchtest task and forkmode="perTest" in the junit task. Hope this info helps to someone who runs into the same issue.

Best,
Pavitra

Yerrabachu Narayan

unread,
Feb 22, 2018, 5:28:56 AM2/22/18
to Drools Usage

In order to resolve java.lang.OutOfMemoryError: Java heap space in Maven, try to configure below configuration in pom

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${maven-surefire-plugin.version}</version>
     <configuration>
        <verbose>true</verbose>
        <fork>true</fork>
        <argLine>-XX:MaxPermSize=500M</argLine>
    </configuration>
</plugin>
Reply all
Reply to author
Forward
0 new messages