当我在运行ANT测试任务后尝试运行特定的测试套件时,在日志中出现初始化错误。完整堆栈跟踪如下:
Failed to transform class with name [CLASS NAME]. Reason: javax.servlet.ServletContext
java.lang.IllegalStateException: Failed to transform class with name [CLASS NAME]. Reason: javax.servlet.ServletContext
at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:266)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:180)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:68)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.lang.ClassLoader.defineClass(ClassLoader.java:635)
at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:269)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:180)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:68)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at java.lang.Class.initAnnotationsIfNecessary(Class.java:3168)
at java.lang.Class.getAnnotation(Class.java:3127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.validate(PowerMockJUnit44RunnerDelegateImpl.java:108)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.<init>(PowerMockJUnit44RunnerDelegateImpl.java:70)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl.<init>(PowerMockJUnit47RunnerDelegateImpl.java:42)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit49RunnerDelegateImpl.<init>(PowerMockJUnit49RunnerDelegateImpl.java:25)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:156)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:40)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:244)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:61)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
Caused by: javassist.NotFoundException: javax.servlet.ServletContext
at javassist.ClassPool.get(ClassPool.java:450)
at javassist.bytecode.Descriptor.toCtClass(Descriptor.java:592)
at javassist.bytecode.Descriptor.getReturnType(Descriptor.java:489)
at javassist.CtBehavior.getReturnType0(CtBehavior.java:306)
at javassist.CtMethod.getReturnType(CtMethod.java:217)
at org.powermock.core.transformers.impl.MainMockTransformer.modifyMethod(MainMockTransformer.java:163)
at org.powermock.core.transformers.impl.MainMockTransformer.allowMockingOfStaticAndFinalAndNativeMethods(MainMockTransformer.java:133)
at org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:65)
at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:251)
当我将以下内容添加到测试套件的开头时,问题就出现了:
@RunWith(PowerMockRunner.class)
@PrepareForTest({problemClass.class, classBeingTested.class})
目标是当被测试的类试图实例化problemClass时,它只会创建一个模拟对象而不是实例。
classBeingTested扩展了堆栈交易中提到的类。即classBeingTested扩展[CLASS NAME]。
我也在监视classBeingTested的实现,因此在调用而不是被调用时,另一个方法什么都不做。
我不确定是否存在某种干扰,在这种情况下,为测试准备课程并在其中插入以及间谍的模拟构造函数是不正确的? / p>
注意:我可以使用run / debug在IntelliJ中运行测试。在这两种情况下,测试都完成并显示为通过。所以只有在ANT构建发生时它才会失败。