I have integrated PowerMock and PowerRule in JUnit with Mockito.
Here are my dependencies:
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.0.GA</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.powermoc</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-objenesis</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
My test class is:
public class TestClass extends AbstractShiroTest{
@Rule
public PowerMockRule rule = new PowerMockRule();
@Autowired
SomeService someService;
@Before
public void setUp(){
Map<String, Object> newMap = new HashMap<String, Object>();
newMap.put("userTimeZone", "Asia/Calcutta");
Subject subjectUnderTest = mock(Subject.class);
when(subjectUnderTest.getPrincipal()).thenReturn(LMPTestConstants.USER_NAME);
Session session = mock(Session.class);
when(session.getAttribute(LMPCoreConstants.USER_DETAILS_MAP)).thenReturn(newMap);
when(subjectUnderTest.getSession(false)).thenReturn(session);
setSubject(subjectUnderTest);
PowerMockito.mockStatic(CasSessionUtil.class);
when(CasSessionUtil.getCarrierId()).thenReturn(1L);
}
@Test
public void myTestMethod() {
someService.doSomething();
}
}
doSomething
is calling a static method that I need to mock.
When I run my test case, I get javassist.NotFoundException: $Proxy88
.
Full stack trace:
java.lang.RuntimeException: javassist.NotFoundException: $Proxy88 at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:187) at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:147) at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:247) at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:66) at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:26) at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:243) at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128) at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248) at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128) at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248) at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128) at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248) at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128) at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248) at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128) at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248) at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128) at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248) at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128) at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:82) at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:69) at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89) at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78) at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: javassist.NotFoundException: $Proxy88 at javassist.ClassPool.get(ClassPool.java:436) at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:180) ... 46 more
If I change the dependency to
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
then I get a different exception. Please see https://stackoverflow.com/questions/12176049/suggest-work-around-for-com-thoughtworks-xstream-converters-conversionexception (deleted SO question, requires 10k).
I have even tried with javassist version 15, but that has the same problem.
Found out the solution myself:
Use the below dependencies (for Power Mock and Power Rule) only
<!-- Required for PowerMock -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
<!-- Required for PowerMockRule -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
Now I am not getting either of the above exceptions
Replace powermock-module-junit4-rule with powermock-module-junit4-rule-agent.
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<scope>test</scope>
</dependency>
The main difference between the agent based bootstrapper and the classloading based bootstrapper is that you don't run into classloading issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With