My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
TestNG_usage  

Usage
Updated Jan 5, 2012 by johan.ha...@gmail.com

Using PowerMock with TestNG

Since version 1.3.5 PowerMock has basic support for TestNG. Supported versions are:

TestNGPowerMock
5.13.1+ 1.4.5+
5.12.1 1.3.8 - 1.4
5.11 1.3.6 & 1.3.7

Using PowerMock 1.4.9 and 1.4.10 without Maven

There was a bug in the release process which excluded the PowerMockTestCase from the full release build. Please download the missing dependency here (sources, javadoc).

How to write tests

Just as with the JUnit runners you need to prepare the classes that are normally not mockable by using the @PrepareForTest annotation. A full example using the Mockito API extension:

Class under test:

public void methodToTest() {
   ..
   final long id = IdGenerator.generateNewId();
   ..
}

The test with TestNG:

@PrepareForTest(IdGenerator.class)
public class MyTestClass {
    @Test
    public void demoStaticMethodMocking() throws Exception {
	mockStatic(IdGenerator.class);
     
	when(IdGenerator.generateNewId()).thenReturn(2L);		
 
	new ClassUnderTest().methodToTest();
 
	// Optionally verify that the static method was actually called
	verifyStatic();
	IdGenerator.generateNewId();
    }
}

For this to work you need to tell TestNG to use the PowerMock object factory as seen below:

Configure TestNG to use the PowerMock object factory

Using suite.xml

In your suite.xml add the following in the suite tag:

object-factory="org.powermock.modules.testng.PowerMockObjectFactory"

e.g.

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

If you're using Maven you may need to point out the file to Surefire:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<configuration>
	    <suiteXmlFiles>
		<suiteXmlFile>suite.xml</suiteXmlFile>
	    </suiteXmlFiles>
	</configuration>
</plugin>

Programmatically

Add a method like this to your test class:

@ObjectFactory
public IObjectFactory getObjectFactory() {
    return new org.powermock.modules.testng.PowerMockObjectFactory();
}

or to be on the safe side you can also extend from the PowerMockTestCase:

@PrepareForTest(IdGenerator.class)
public class MyTestClass extends PowerMockTestCase {
   ...
}

For an example of this check out the example made by Adrian Moerchen.

Comment by nicola.s...@gmail.com, Feb 25, 2010

hi, is it possible to use the @ObjectFactory? testng annotation ?

Comment by project member johan.ha...@gmail.com, Feb 28, 2010

Yes, just added documentation for it

Comment by soren.hi...@gmail.com, Mar 22, 2010

Hi Tried the version with @ObjectFactory?, but I keep getting:

java.lang.reflect.InvocationTargetException?

at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:48) at zzz$$javassist_0.testget(zzz$$javassist_0.java)
Caused by: java.lang.IllegalArgumentException?: Not a mock: java.lang.Class
at org.easymock.classextension.internal.ClassExtensionHelper?.getControl(ClassExtensionHelper?.java:62) at org.easymock.classextension.EasyMock.replay(EasyMock.java:181) at dk.amplex.aas.ws.rest.TestIOUnitWS.testget(TestIOUnitWS.java:63)

Is this a known thing?

Comment by soren.hi...@gmail.com, Mar 22, 2010

Ahh,accidentally called EasyMock's replay instead of PowerMocks? (so much for static imports)

sorry for the noise.

Comment by adam.spi...@gmail.com, Sep 8, 2010

I saw some really odd behaviour with PowerMock? 1.4.5 and TestNG 5.13.1 which I have detailed here:

http://code.google.com/p/powermock/issues/detail?id=54#c9

The workaround seems to be to subclass PowerMockTestCase? but I am not 100% sure this is right.

Incidentally that issue #54 offers information about TestNG support which conflicts with this page.

Comment by etux....@gmail.com, Feb 21, 2011

I am trying to mock org.jboss.soa.esb.message.format.MessageFactory? which has an static method called getInstance() returning an instance of MessageFactory? class.

Whenever I do a when(MessageFactory?.getInstance).thenReturn(mockedMessageFactory); I get a org.mockito.exceptions.misusing.MissingMethodInvocationException?.

I believe I have followed the instructions step by step, this means:

@PrepareForTest?(MessageFactory?.class)

..

PowerMockito?.mockStatic(MessageFactory?.class) MessageFactory? mockedMessageFactory = Mockito.mock(MessageFactory?.class);

..

@ObjectFactory? public IObjectFactory getObjectFactory() {

return new org.powermock.modules.testng.PowerMockObjectFactory?();
}

Does anybody know how to make this work?

Comment by project member johan.ha...@gmail.com, Feb 22, 2011

Please use the mailing list for asking questions.


Sign in to add a comment
Powered by Google Project Hosting