|
TestNG_usage
Using PowerMock with TestNGSince version 1.3.5 PowerMock has basic support for TestNG. Supported versions are:
Using PowerMock 1.4.9 and 1.4.10 without MavenThere 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 testsJust 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 factoryUsing suite.xmlIn 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> ProgrammaticallyAdd 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. | ||||||||||||
hi, is it possible to use the @ObjectFactory? testng annotation ?
Yes, just added documentation for it
Hi Tried the version with @ObjectFactory?, but I keep getting:
java.lang.reflect.InvocationTargetException?
Caused by: java.lang.IllegalArgumentException?: Not a mock: java.lang.ClassIs this a known thing?
Ahh,accidentally called EasyMock's replay instead of PowerMocks? (so much for static imports)
sorry for the noise.
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.
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() {
}Does anybody know how to make this work?
Please use the mailing list for asking questions.