Cedric,
While exploring PowerMock, I stumbled into the exact same thing as well:
I did a little bit of debugging and found that the culprit in this case is the @ObjectFactory annotated method [which I guess TestNG calls at the beginning itself] and this seems to provide TestNG with a proxied class instance.
Interestingly the first @Test annotated method seems to work without any issues, but when it comes to DataProvider driven test method, TestNG conks, because it gets a hold of the current test class instance [which unfortunately is not of my test class type, but seems to be proxied class] and then keeps querying this class and its parents for a test method.
I see that TestNG walks up the inheritance tree all the way upto Object, and when it still cannot find my @Test data driven method , I get this exception.
Would appreciate if you could please help me understand as to what could be the alternative here.
Should I have to mandatorily segregate my PowerMock tests from the regular TestNG data driven test methods ?
Here's a sample code that shows this problem [I am using TestNG 6.8 along with PowerMock 1.4.12 and powermock-module-testng 1.4.12
package com.test;
import static org.powermock.api.mockito.PowerMockito.when;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.Assert;
import org.testng.IObjectFactory;
import org.testng.annotations.DataProvider;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;
@PrepareForTest({ Mocks.StaticFactory.class })
public class Mocks {
@ObjectFactory
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
@Test
public void testMe() throws Exception {
Assert.assertEquals(null, StaticFactory.getMethod());
PowerMockito.mockStatic(StaticFactory.class);
when(StaticFactory.getMethod()).thenReturn("DES");
Assert.assertEquals("DES", StaticFactory.getMethod());
}
@Test(dataProvider = "dp", dependsOnMethods = "testMe")
public void foo(StaticFactory name) {
Assert.assertNotNull(name);
}
@DataProvider(name = "dp")
public Object[][] getData() {
return new Object[][] { { new StaticFactory() }, { new StaticFactory() } };
}
public static class StaticFactory {
public static String getMethod() {
return null;
}
}
}