Powermock方法替换找不到类

时间:2013-09-11 08:22:27

标签: java unit-testing mockito powermock

我正在尝试使用像这样的PowerMock替换方法

@RunWith(PowerMockRunner.class)
@PrepareForTest(HeaderVO.Builder.class)
public void TestClass {
    public Builder fromHeaders(Map<String, String> requestHeadersMap) {
        return null;
    } 

    // snip
    replace(method(HeaderVO.Builder.class, "fromHeaders")).with(
                        method(
                        TestClass.class,
                        "fromHeaders"));

但是,由于HeaderVO的其他方法之一接受了HttpServletRequest类型的参数,我得到了

java.lang.IllegalStateException: Failed to transform class with name path.to.my.test.class.HeaderVO$Builder. Reason: cannot find javax.servlet.http.HttpServletRequest
        at javassist.ClassPool.get(ClassPool.java:450)
        at javassist.bytecode.Descriptor.toCtClass(Descriptor.java:592)
        at javassist.bytecode.Descriptor.getParameterTypes(Descriptor.java:439)
        at javassist.CtBehavior.getParameterTypes(CtBehavior.java:298)
        at javassist.CtBehavior.insertBefore(CtBehavior.java:745)
        at javassist.CtBehavior.insertBefore(CtBehavior.java:730)
        at org.powermock.core.transformers.impl.MainMockTransformer.modifyMethod(MainMockTransformer.java:207)
        at org.powermock.core.transformers.impl.MainMockTransformer.allowMockingOfStaticAndFinalAndNativeMethods(MainMockTransformer.java:143)
        at org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:66)
        at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:215)
        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:356)
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2451)
        at java.lang.Class.privateGetPublicMethods(Class.java:2571)
        at java.lang.Class.getMethods(Class.java:1429)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.getTestMethods(PowerMockJUnit44RunnerDelegateImpl.java:95)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.<init>(PowerMockJUnit44RunnerDelegateImpl.java:71)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit49RunnerDelegateImpl.<init>(PowerMockJUnit49RunnerDelegateImpl.java:29)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:143)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:39)
        at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:217)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:59)
        at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
        at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:33)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
        at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
        at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
        at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
        at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:262)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:159)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:87)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:95)

这应该如何工作,我怎样才能让PowerMock / javassist成为HttpServletRequest?它在类路径中并且工作正常。

1 个答案:

答案 0 :(得分:1)

傻傻的我。我错了,它不在类路径中,将servlet-api添加到测试类路径中修复了...

还注意到由于方法不是静态的,我尝试的东西无法真正完成。使用this等代理结束。