我已经将 JUnit 中的 PowerMock 和 PowerRule 与 Mockito 集成在一起。
这是我的依赖项:
<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>
我的测试课是:
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
正在调用我需要模拟的静态方法。当我运行我的测试用例时,我得到javassist.NotFoundException: $Proxy88
.
完整的堆栈跟踪:
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)在 org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:252) 在 java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) 在org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:66) 的 java.lang.Class.forName(Class.java:247) 的 java.lang.Class.forName0(Native Method)。 api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:26) 在 org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:243) 在 org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248) 在 org.powermock.classloading.DeepCloner.performClone(DeepCloner.java) 的 org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128) :128) 在 org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248) 在 org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128) 在 org.powermock.classloading.DeepCloner.cloneFields(DeepCloner. java:248) 在 org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128) 在 org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248) 在 org.powermock.classloading.DeepCloner.performClone(DeepCloner .java:128) 在 org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248) 在 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 org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89) 中 org.powermock.classloading 中的 org.powermock.classloading.DeepCloner.clone(DeepCloner.java:69) 中的 .clone(DeepCloner.java:82)。 ClassloaderExecutor.execute(ClassloaderExecutor.java:78) at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49) atorg.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat. java:72) 在 org.junit 的 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)。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 ) 在 org.springframework 的 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 的 org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)。 test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:236)在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)在 org.eclipse. .jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal .junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner .RemoteTestRunner.main(RemoteTestRunner.java:197) 引起:javassist.NotFoundException: $Proxy88 at javassist.ClassPool.get(ClassPool.java:436) at org.powermock.core.classloader.MockClassLoader。loadUnmockedClass(MockClassLoader.java:180) ... 46 更多
如果我将依赖项更改为
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
然后我得到一个不同的例外。请参阅https://stackoverflow.com/questions/12176049/suggest-work-around-for-com-thoughtworks-xstream-converters-conversionexception(已删除 SO 问题,需要 10k)。
我什至尝试过使用 javassist 15 版,但也有同样的问题。