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.
Without more code samples I gather that the code is using Spring in the test. So I believe the reason for this error is that related to Spring use, which do seem to have generated JDK proxies (the
$Proxy88
).And the way Powermock is working is by running the JUnit test in a new classloader in order to modify the bytecode of these classes, unfortunately it is only possible to modify the bytecode from a the real file, or at least from a location where it's possible to read the class binary, as java cannot access bytecode already loaded in the JVM. (It may be possible with an agent in a limited way).
As JDK proxies do not exists on disk, they cannot be read, or copied to the specific Powermock classloader.
The test you are writing is not a unit test as it is run with a Spring context. You might want to write a real Unit Test first. Then some Integration Test, in which you won't need mocks.
Also you should avoid the use of statics, as it is a testability nightmare. You should rewrite your production code in a way where static calls don't need to be mocked.
Cheers,
Replace powermock-module-junit4-rule with powermock-module-junit4-rule-agent.
The main difference between the agent based bootstrapper and the classloading based bootstrapper is that you don't run into classloading issues.
Found out the solution myself:
Use the below dependencies (for Power Mock and Power Rule) only
Now I am not getting either of the above exceptions