首先告诉你,我已经尝试过,现在我需要一些帮助
这是我的代码
package staticPkg;
public class Static {
public static final String staticMethod() {
System.out.println("Static method called");
return "Static called";
}
}
package staticPkg;
public class TargetClass {
Static staticClass;
public String callHere() {
return Static.staticMethod();
}
}
package staticPkg;
import org.easymock.EasyMock;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.IObjectFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;
@PrepareForTest({Static.class})
public class TestClass {
Static staticClass = null;
@ObjectFactory
public IObjectFactory getObjectFactory() {
System.out.println("got object factory");
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
@BeforeMethod
public void setup() {
System.out.println("print me");
PowerMock.mockStatic(Static.class);
staticClass = PowerMock.createMock(Static.class);
}
@Test
public void testMe() {
EasyMock.expect(Static.staticMethod()).andReturn("Mock called").anyTimes();
PowerMock.replay(Static.class,staticClass);
TargetClass tc = new TargetClass();
String output = tc.callHere();
PowerMock.verify(Static.class,staticClass);
System.out.println(output);
}
}
And heres the log
[解析器] 运行:C:\MockWorkspace\Mock\temp-testng-customsuite.xml
得到对象工厂 打印我 静态方法调用 失败:测试我 java.lang.IllegalStateException:没有可用的模拟最后一次调用 在 org.easymock.EasyMock.getControlForLastCall(EasyMock.java:521) 在 org.easymock.EasyMock.expect(EasyMock.java:499) 在 staticPkg.TestClass.testMe(TestClass.java:46) ... 删除了 22 个堆栈帧 ================================================ staticPkg.TestClass 测试运行:1,失败:1,跳过:0 ================================================ ================================================ 嘲笑 总测试运行:1,失败:1,跳过:0 ================================================
请帮助,我尝试了各种解决方案,无法完成。请任何人都可以尝试此代码并更正它以获得成功吗?我在 EasyMock.expect 中遇到错误......
在http://blogs.bytecode.com.au/glen/2006/10/12/doing-bytecode-kungfu-with-javassist.html得到了解决, 它可以工作但是等等...... .我又被卡住了我的测试用例在单独运行时工作正常,但是当与 Ant 一起运行时,它会出现问题。可能是不同文件的其他测试用例在干扰。当我的个人测试用例使用 @PrepareTest & easymock/powermock 时,我遇到了同样的错误
[测试] =====================静态调用========================= == [testng] javassist.CannotCompileException:由 java.lang.LinkageError:加载程序(sun/misc/Launcher$AppClass Loader 的实例):尝试重复的类定义名称:“com/symantec/mobius/aggregator/submission/SubmissionFactory” [testng] 在 javassist.ClassPool.toClass(ClassPool.java:1085) [testng] 在 javassist.ClassPool.toClass(ClassPool.java:1028) [testng] 在 javassist.ClassPool.toClass(ClassPool.java:986) [testng ] 在 javassist.CtClass.toClass(CtClass.java:1110)