Cant mock static functions with powermock-easymock-testng (non-maven project)

匿名 (未验证) 提交于 2019-12-03 01:41:02

问题:

To tell you first, i have tried and tried it again and now i need some help

Heres my code

 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 

[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 ===============================================  

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 ...............

Got a work around at http://blogs.bytecode.com.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] ====================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)

回答1:

Try extending from PowerMockTestCase. The TestNG support will also be updated in next version of PowerMock (1.4.9).



回答2:

I faced this same issue, and struggled a lot. Finally, found the following solution:

Another alternative is to set the object-factory to org.powermock.modules.testng.PowerMockObjectFactory in the TestNG suite.xml. Here is a sample suite file:

<suite name="dgf" verbose="10" object-factory="org.powermock.modules.testng.PowerMockObjectFactory">     <test name="dgf">         <classes>             <class name="com.example.ClientTest"/>         </classes>     </test> </suite> 

Of course, you can also extend your test case from PowerMockTestCase as told by Johan.



回答3:

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);  } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!