We use
PowerMock (With the
Mockito Api) in our current project as the Mocking/Stubbing framework. It has worked our really well so far!
Completely in love with the
Mockito syntax and especially the error messages that it gives out when something is wrong.
Love the power that PowerMock adds to Mockito which enables us to
test the un-testable code. The combination i.e. PowerMocks with Mockito, makes it a deadly combination to ignore and I call it
Mocks on Steroids!
But life is not always perfect! is it? Recently I faced a situation, suddenly out of no where I started getting the
infamous
java.lang.OutOfMemoryError: PermGen space Error here is the stack trace.
Man! its always a challenge to fix such issues.
Googled a little, found that its a known issue have a look at this
url
Its caused because of a Memory leak (wow! as if I am telling something new! Its obvious, we get
OutOfMemoryError when we have memory leaks! Everyone knows that!). Yes, and as you guys already know finding the source of memory leak is a challenge in itself.
But looks like, they have narrowed the problem down to the
MockClassLoader class.
The problem:
If your project uses third party frameworks like
log4j or spring, then those classes are also loaded by the
MockClassLoader class. Causing
log4j or spring classes to be loaded multiple times by multiple instances of
MockClassLoader class.
The long term solution:
This problem will be fixed in the PowerMock's 1.4 version for sure!
But what should you do if you can't wait till then?
The short term solution:
Use the
@PowerMockIgnore annotation. This annotation signal PowerMock
not to load third party framework jars such as
log4j and spring using the MockClassLoader. Let the system class loader load them.
The sample test case looks like this
That fixes the problem for our project!
This problem is a perfect example of the fact that, there is always a flip side!
But, if the flip side is not as bad, then as said by
Amir Khan in the movie
3 idiots
All izz well!