Using @Factory annotation + PowerMock's @ObjectFactory

147 views
Skip to first unread message

aiman86

unread,
Sep 24, 2015, 8:15:58 PM9/24/15
to testng-users
Hello,

I'm trying to use @Factory annotation to build dynamic test instances, but I would like to also use PowerMock in my test. It seems that this is currently undoable  - or I may be doing something wrong. Can you please take a look ? Below is a simplified version of the code that exhibit the same issue:

@PrepareForTest({ FileSystem.class })

@PowerMockIgnore("javax.management.*")

@SuppressWarnings("all")

public class SimpleTestCase extends PowerMockTestCase {


   @Factory(dataProvider="testData")

   public Object[] testInstanceFactory(Object param) {

       return new Object[]{ new SimpleTestCase() };

   }


    @Test

   public void testProcessEvents() throws Exception {

       Assert.assertTrue(true);

   }


    @DataProvider(name = "testData")

   public Object[][] testData() {


        return new Object[][] { { "param" } };

   }


    @ObjectFactory

   public IObjectFactory getObjectFactory() {

       return new PowerMockObjectFactory();

   }

}


Mismatch between instance/method classes:class com.company.SimpleTestCase_$$_jvst74d_0 class com.company.SimpleTestCase
    at org.testng.internal.FactoryMethod.<init>(FactoryMethod.java:44)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:138)
    at org.testng.TestRunner.initMethods(TestRunner.java:413)
    at org.testng.TestRunner.init(TestRunner.java:239)
    at org.testng.TestRunner.init(TestRunner.java:209)
    at org.testng.TestRunner.<init>(TestRunner.java:164)
    at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:142)
    at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:584)
    at org.testng.SuiteRunner.init(SuiteRunner.java:159)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:113)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1274)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1261)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1115)
    at org.testng.TestNG.run(TestNG.java:1032)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

To get the above to run, I can do one of the following:
  • Remove the the @ObjectFactory method and the inheritance from PowerMockTestCase
  • Or remove the PrepareForTest annotation 
However, naturally this will disable PowerMock features which I critically need. So, I'm not sure how to use both PowerMock and TestNG's @Factory to create dynamic test instances. Are these two incompatible currently or is there another way to accomplish this?

Thank you


Mrunal Gosar

unread,
Sep 26, 2015, 9:18:42 AM9/26/15
to testng-users
TestNG's Factory should be sufficient to create dynamic tests, why would u need other supporting library?
Reply all
Reply to author
Forward
0 new messages