Need some sample mocking with testng + easymock + powermock

517 views
Skip to first unread message

Sumit Kumar

unread,
Feb 2, 2011, 12:34:06 PM2/2/11
to PowerMock
Its kind of frustrating, everywhere i look up theres only sample
examples with junit + powermock or testng+mockito+powermock.
I tried myself , but unless and until its a maven project, testng +
easymock + powermock doesn't workout for me.

I appreciate the efforts in this group, but my work area, i can't make
powermock work.

Heres a link from my prev. query. I have attached a small src code for
anyone interested.

http://groups.google.com/group/powermock/browse_thread/thread/0b6f8ce04b6d9d86?hl=en

Looking forward for some help

Regards,
Sumit

Johan Haleby

unread,
Feb 3, 2011, 2:04:15 AM2/3/11
to powe...@googlegroups.com
Hi,

I've never used PowerMock + TestNG in "production" myself but I've ran the examples in the TestNG test module in PowerMock (checkout the source code in modules/module-test/easymock/testng-test) and this works for me in Eclipse, Intellij and Maven 2 (I haven't tried it with Maven 3 though). I know that there are issues and we do appreciate support. 

/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,
Feb 6, 2011, 5:27:00 AM2/6/11
to powe...@googlegroups.com, johan....@gmail.com
Hi All,

I replaced TestNG 5.14.1+Easymock 3.0+Powermock1.4.7  with JUnit 4.8.9+EasyMock 3.0+Powermock. 1.4.7 The things run very smoothly in my ANT project now. But i need to get it working in Testng :( . I suspect @ObjectFactory public IObjectFactory getObjectFactory()  is not doing its job. Although i see its getting called (i printed something in this function) but its not working. I surfed a lot and  suspect  that Testng is not using the Powermock's classloader.


Could you share someof your experiences in this so that i get things working. I extended by tests with PowerMockTestCase but no help. Any jar issues/workaround?

Regards,
SK

sumit sharma

unread,
Feb 6, 2011, 8:48:49 AM2/6/11
to powe...@googlegroups.com, johan....@gmail.com
Yes theres the problem ,

While printing the classloader in JUnit project i get : - classloader org.powermock.core.classloader.MockClassLoader@1ff5ea7
But while printing inTestng project i get : - classloader sun.misc.Launcher$AppClassLoader@11b86e7

Please help now
The code is exaclty the same in both the places, only change in Testng is use of
@ObjectFactory
public IObjectFactory getObjectFactory() {
System.out.println("got object factory");
   return new org.powermock.modules.testng.PowerMockObjectFactory();
}

in place of @RunWith(PowerMockRunner.class) for JUnit

Johan Haleby

unread,
Feb 6, 2011, 12:13:24 PM2/6/11
to powe...@googlegroups.com
What happens if you extend from PowerMockTestCase? Does that make any difference?

/Johan

sumit sharma

unread,
Feb 6, 2011, 12:22:13 PM2/6/11
to powe...@googlegroups.com
no Johan, not at all, even if i try to use object-factory="org.powermock.modules.testng.PowerMockObjectFactory" in suite file, i always get FAILED CONFIGURATION: @BeforeTest setup
java.lang.IllegalArgumentException: object is not an instance of declaring class.

Johan Haleby

unread,
Feb 6, 2011, 1:12:42 PM2/6/11
to powe...@googlegroups.com
But can you run the examples in the TestNG test module in PowerMock? Have you tried specifying the object factory in XML?
<suite name="dgf" verbose="10" object-factory="org.powermock.modules.testng.PowerMockObjectFactory">
    <test name="dgf">
        <classes>
            <class name="com.mycompany.Test1"/>
            <class name="com.mycompany.Test2"/>
        </classes>
    </test>
</suite>

And if you're using Maven:
<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <suiteXmlFiles>
                <suiteXmlFile>suite.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
</plugin


/Johan

sumit sharma

unread,
Feb 6, 2011, 1:27:22 PM2/6/11
to powe...@googlegroups.com
Hi Johan ,

Thanks for a fast reply. I have tried specifying the object factory in XML. 

Heres what i get. Please see attachments
java.lang.IllegalArgumentException: object is not an instance of declaring class
... Removed 24 stack frames
SKIPPED CONFIGURATION: @AfterMethod afterPowerMockTestMethod
SKIPPED: testMe

Attaching the src code for your reference too
errorLogs.txt
Mock.zip

Johan Haleby

unread,
Feb 6, 2011, 3:41:27 PM2/6/11
to powe...@googlegroups.com
I don't have Eclipse but I imported the project in Intellij 10, put everything from powermock-easymock-testng-1.4.7.zip in the classpath and just ran the testng.xml as a TestNG test case and I get a green bar..

/Johan

sumit sharma

unread,
Feb 7, 2011, 9:56:36 AM2/7/11
to powe...@googlegroups.com
Hi Johan,

It was very heartening to see you going an extra mile to help me. Yes i found out, theres no problem in the code.
I was working on eclipse and using its TestNG plugin to run my testcases. I changed that and started using ANT. All the testcases got executed smoothly. I believe theres a bug in eclipse. Will figure out where to post this bug, and get this issue rectified.
Powermock is really very cool and easy.

Thanks for your help

-Sumit

Johan Haleby

unread,
Feb 7, 2011, 1:21:10 PM2/7/11
to powe...@googlegroups.com
Hi,

No problem :) Please keep in touch if you find out how to solve or get more info on the Eclipse issue. Thanks for your encouragement.

/Johan
Reply all
Reply to author
Forward
0 new messages