I have the following powermock test:
@RunWith(PowerMockRunner.class)
@PrepareForTest({DaoCaseTypeDefinition.class, QDataContext.class})
public class PowermockTest {
private static QDataContext m_dc;
private static DaoCaseTypeDefinition m_daoCaseTypeDefinition;
@Test
public void test() throws Exception {
m_dc = mock(QDataContext.class);
m_daoCaseTypeDefinition = mock(DaoCaseTypeDefinition.class);
when(m_daoCaseTypeDefinition.findAll(m_dc))
.thenReturn(Collections.EMPTY_LIST);
DaoCaseTypeDefinition daoCaseTypeDefinition = new DaoCaseTypeDefinition();
List<CaseTypeDefinition> all = daoCaseTypeDefinition.findAll(m_dc);
System.out.println("sleep");
Thread.sleep(2000);
}
@AfterClass
public static void after() throws InterruptedException {
m_daoCaseTypeDefinition = null;
m_dc = null;
System.out.println("done");
Thread.sleep(2000);
}
}
In this test I am using PowerMockito 1.7.1 on jdk1.8 (also used PowerMockito 2 btw). When I let Intellij run this test over and over again and attaching VisualVM to it, it becomes clear that I have a memory leak.
I noticed that for every testclass annotated with PowerMockRunner.class a MockClassloader instance is created that is holding the references to, in this case a DaoCaseTypeDefinition. Unfortunately the mockclassloader instances are never recovered.
Is there something in my test that I'm doing wrong? Why is the garbage collection not clearing the mockclassloader instances?