首先要告诉您,我已经尝试了一次,现在又需要一些帮助
To tell you first, i have tried and tried it again and now i need some help
此处提供了我的代码
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: WorkMockWorkspace\Mock\temp-testng-customsuite.xml
[Parser] Running: C:\MockWorkspace\Mock\temp-testng-customsuite.xml
got object factory print me Static method called FAILED: testMe java.lang.IllegalStateException: no last call on a mock available at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:521) at org.easymock.EasyMock.expect(EasyMock.java:499) at staticPkg.TestClass.testMe(TestClass.java:46) ... Removed 22 stack frames =============================================== staticPkg.TestClass Tests run: 1, Failures: 1, Skips: 0 =============================================== =============================================== Mock Total tests run: 1, Failures: 1, Skips: 0 ===============================================请帮助,我尝试了多种解决方案,无法完成。 请任何人都可以尝试此代码并更正成功吗? 我在EasyMock.expect中遇到错误......
Help please, i have tried a variety of solutions, can't get it done. Please can anyone try this code and correct it for success? I get error in EasyMock.expect ...............
在 blogs.bytecode.au /glen/2006/10/12/doing-bytecode-kungfu-with-javassist.html 可以运行但请稍候.......... 单独运行时,我的测试用例可以正常工作,但是与Ant一起运行时,它会带来问题。可能是不同文件的其他测试用例正在干扰。 ,当我的单个测试用例使用@PrepareTest& easymock / powermock
Got a work around at blogs.bytecode.au/glen/2006/10/12/doing-bytecode-kungfu-with-javassist.html And it works But wait..........I am stuck again My testcase works fine when runs alone, but when run with Ant, it gives problem. Might be other test cases of different files are interfering. I got the same error, when my individual test case was using @PrepareTest & easymock/powermock
[testng] ====================静态调用======= =================== [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] at javassist.ClassPool.toClass(ClassPool.java:1028) [testng] at javassist.ClassPool.toClass(ClassPool.java:986) javassist.CtClass.toClass( CtClass.java:1110)
[testng] ====================STATIC CALLED=========================== [testng] javassist.CannotCompileException: by java.lang.LinkageError: loader (instance of sun/misc/Launcher$AppClass Loader): attempted duplicate class definition for name: "com/symantec/mobius/aggregator/submission/SubmissionFactory" [testng] at javassist.ClassPool.toClass(ClassPool.java:1085) [testng] at javassist.ClassPool.toClass(ClassPool.java:1028) [testng] at javassist.ClassPool.toClass(ClassPool.java:986) [testng] at javassist.CtClass.toClass(CtClass.java:1110)
推荐答案在继续模拟静态方法之前,请模拟静态类中的所有静态方法。尝试以下操作:
Mock all the static methods in static class before proceeding to mock the static method. Try with this:
@Test public void testMe() { PowerMock.mockStatic(Static.class); 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); }更多推荐
无法使用powermock
发布评论