Using DataProviders with Powermock+Mockito+TestNG

2,856 views
Skip to first unread message

bsorek

unread,
Apr 6, 2011, 11:01:14 AM4/6/11
to testng-users
Hi,
We are trying to use PowerMock to mock a singleton. The test has a
dataprovider defined which seems to cause problems when running the
test.
How are we meant to pass parameters to a test using testng's
dataproviders?

We get the following error:

FAILED: demoSingletonMocking(com.sandvine.tests.regex.Person@fbb7cb,
"Ben")
java.lang.RuntimeException: Can't invoke method public void
com.sandvine.tests.SingletoneInitTest.demoSingletonMocking(com.sandvine.tests.regex.Person,java.lang.String),
probably due to classloader mismatch

Here is the code:

@PrepareForTest(MySingleton.class)
public class SingletoneInitTest extends PowerMockTestCase {


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

@Test(dataProvider="mynames" )
public void demoSingletonMocking(Person person, String
expected)
{
//
PowerMockito.suppress(PowerMockito.constructor(MySingleton.class));
PowerMockito.mockStatic(MySingleton.class);
MySingleton mockSingleton =
Mockito.mock(MySingleton.class);


Mockito.when(MySingleton.getInstance()).thenReturn(mockSingleton);
Mockito.when(MySingleton.getName()).thenReturn("you");
Mockito.when(mockSingleton.getId()).thenReturn("3333");
System.out.println(MySingleton.getName());
System.out.println(MySingleton.getInstance().getId());
Assert.assertEquals(person.getName(), expected);
PowerMockito.verifyStatic();
MySingleton.getName();
}

@DataProvider(name = "mynames" )
public Object[][] getNames()
{
return new Object[][] {
{new Person("Shalom"), "Shalom"},
{new Person("Ben"), "Ben"}
};
}

I get the following stack trace:

FAILED: demoSingletonMocking(com.sandvine.tests.regex.Person@159054d,
"Ben")
java.lang.RuntimeException: Can't invoke method public void
com.sandvine.tests.SingletoneInitTest.demoSingletonMocking(com.sandvine.tests.regex.Person,java.lang.String),
probably due to classloader mismatch
at
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:
60)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:673)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:846)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1170)
at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:
125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:
109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1125)
at org.testng.TestRunner.privateRun(TestRunner.java:749)
at org.testng.TestRunner.run(TestRunner.java:600)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:317)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274)
at org.testng.SuiteRunner.run(SuiteRunner.java:223)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1007)
at org.testng.TestNG.runSuitesLocally(TestNG.java:932)
at org.testng.TestNG.run(TestNG.java:868)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:110)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174)


Thanks

Krishnan

unread,
Nov 30, 2012, 8:25:35 AM11/30/12
to testng...@googlegroups.com
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;
        }
    }
}

Krishnan Mahadevan

unread,
Dec 4, 2012, 2:32:37 AM12/4/12
to testng...@googlegroups.com
Cedric,
Can you please help advice on this ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/l0kikerppEkJ.

To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Hitesh Sajnani

unread,
Feb 25, 2016, 9:55:46 AM2/25/16
to testng-users

If you are using any PowerMock:PrepareForTest, you should not use the custom class as test case input.

You can always put it like public void forAllWeekdaysAllClassroomsShouldBeAvailableInitially(Object p) {
customClass = (CustomClass) p;
assertTrue(true);
}


I got this error, and the afore-mentioned solution works.

Ref: https://github.com/jayway/powermock/issues/484
Reply all
Reply to author
Forward
0 new messages