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

739 views
Skip to first unread message

Sumit Kumar

unread,
Jan 9, 2011, 5:43:29 AM1/9/11
to PowerMock
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);

} }

`here

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

Johan Haleby

unread,
Jan 10, 2011, 2:16:49 AM1/10/11
to powe...@googlegroups.com
Try letting your test class extend from PowerMockTestCase and see if that works better. 

/Johan


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


sumit sharma

unread,
Jan 10, 2011, 10:44:32 AM1/10/11
to powe...@googlegroups.com
Hi Johan,

I have tried that but no success. I suspect, the @PrepareForTest is not doing its job because while mocking and expecting at the first place, its making a call to the real static method.

-Sumit

Sumit Kumar

unread,
Jan 11, 2011, 12:48:21 AM1/11/11
to PowerMock
I got an excellent workaround here
http://blogs.bytecode.com.au/glen/2006/10/12/doing-bytecode-kungfu-with-javassist.html

JAVASSIST rocks!!!!!!!!!!!!
But PowerMock still need to figure out this problem...its still open

On Jan 10, 8:44 pm, sumit sharma <sksum...@gmail.com> wrote:
> Hi Johan,
>
> I have tried that but no success. I suspect, the @PrepareForTest is not
> doing its job because while mocking and expecting at the first place, its
> making a call to the real static method.
>
> -Sumit
>
> On Mon, Jan 10, 2011 at 12:46 PM, Johan Haleby <johan.hal...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Try letting your test class extend from PowerMockTestCase and see if that
> > works better.
>
> > /Johan
>
> >> powermock+...@googlegroups.com<powermock%2Bunsubscribe@googlegroups .com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/powermock?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "PowerMock" group.
> > To post to this group, send email to powe...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > powermock+...@googlegroups.com<powermock%2Bunsubscribe@googlegroups .com>
> > .

Sumit Kumar

unread,
Jan 11, 2011, 2:32:37 AM1/11/11
to PowerMock
I got an excellent workaround here
http://blogs.bytecode.com.au/glen/2006/10/12/doing-bytecode-kungfu-wi...
JAVASSIST rocks!!!!!!!!!!!!
But PowerMock still need to figure out this problem...its still open

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)

On Jan 11, 10:48 am, Sumit Kumar <sksum...@gmail.com> wrote:
> I got an excellent workaround herehttp://blogs.bytecode.com.au/glen/2006/10/12/doing-bytecode-kungfu-wi...

Johan Haleby

unread,
Jan 11, 2011, 2:36:27 AM1/11/11
to powe...@googlegroups.com
Perhaps your classpath ordering is different in Eclipse and Ant? This is a common error.

/Johan

To unsubscribe from this group, send email to powermock+...@googlegroups.com.

sumit sharma

unread,
Jan 13, 2011, 11:31:55 PM1/13/11
to powe...@googlegroups.com
Hi Johan,

Thanks to you for your great work on Powermock.
It has helped me in my work a lot.
But could you look into the issue why powermock's mocking of a non-maven project of testng-easymock-powermock is not working?
Is it a jar version related issue. (I have upgraded/degraded jars with the same results.)
I am attaching my sample project thats not working. (look into staticPkg)

Thanks,
Sumit
Mock.zip

sachin grover

unread,
Jun 28, 2013, 2:49:20 AM6/28/13
to powe...@googlegroups.com, sksu...@gmail.com
Hi,,  
       i have the same issue.while mocking static method getting same exception.
       why powermock's mocking static method of a non-maven project of junit-easymock-powermock is not working??
     
      Did you get any solution???

sachin

Johan Haleby

unread,
Jul 1, 2013, 4:14:31 AM7/1/13
to powe...@googlegroups.com
How do I run the project you've attached?
Reply all
Reply to author
Forward
0 new messages