Replacement for extends PowerMockTestCase i TestNG

983 views
Skip to first unread message

mike

unread,
Oct 3, 2014, 3:39:33 AM10/3/14
to powe...@googlegroups.com
Hi,

I am using PowerMock 1.5.3 with TestNG. We already have a base class that our testclass extends.
So I checked if there is an alternative and found:

https://code.google.com/p/powermock/wiki/TestNG_usage

So I tried to implement this

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


But when I checked the powerock-module-testng-common-1.5.3.jar I found that it only contained this file:

PowerMockTestCase.class

What can I do about this? I am in urgent need of a workaround.

//mike



Johan Haleby

unread,
Oct 3, 2014, 4:01:07 AM10/3/14
to powe...@googlegroups.com
You should (probably) depend on powermock-module-testng instead of testng-common. testng-common is a transitive dependency to powermock-module-testng.

/Johan

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

mike

unread,
Oct 3, 2014, 4:51:25 AM10/3/14
to powe...@googlegroups.com
I changed to latest release and now I am facing a different problem.


My testng testcase looks like this:

@PrepareForTest({ ASingleton.class })
public class TestCase extends ABase {

   
    private Help helpMock;
    private Node nodeMock;

    /**
     * We need a special {@link IObjectFactory}.
     * Cannot use PowerMockTestCase.
     *
     * @return {@link PowerMockObjectFactory}.
     */
    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new PowerMockObjectFactory();
    }

    @BeforeMethod
    public void setup() throws Exception {

        nodeMock = mock(Node.class);
        helpMock = mock(Help.class);
    }
   
    @Test
    public void myTest() throws Exception {
    //mocking a singleton...
   
   
    }


Then I get the following error:

[TestNG] Running:
  /tmp/testng-eclipse--1629291661/testng-customsuite.xml

FAILED CONFIGURATION: @BeforeMethod setup
java.lang.ExceptionInInitializerError
    at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
    at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
    at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:19)
    at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
    at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145)
    at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110)
    at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106)
    at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:59)
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21)
    at org.mockito.internal.configuration.injection.scanner.MockScanner.<init>(MockScanner.java:22)
    at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:96)
    at org.powermock.api.mockito.internal.configuration.PowerMockitoInjectingAnnotationEngine.process(PowerMockitoInjectingAnnotationEngine.java:35)
    at org.powermock.api.extension.listener.AnnotationEnabler.injectSpiesAndInjectToSetters(AnnotationEnabler.java:60)
    at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:663)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:415)
    at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.injectMocksUsingAnnotationEnabler(PowerMockTestNGMethodHandler.java:75)
    at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:47)
    at example.AlarmTestCase_$$_javassist_0.setup(AlarmTestCase_$$_javassist_0.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    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)
Caused by: java.lang.NullPointerException
    at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
    ... 50 more

What am I doing wrong here?

//mike

Johan Haleby

unread,
Oct 3, 2014, 2:30:13 PM10/3/14
to powe...@googlegroups.com
Are you using Maven or Gradle? If not make sure that you're using the right version of Mockito (see docs).

mike

unread,
Oct 6, 2014, 6:13:30 AM10/6/14
to powe...@googlegroups.com
Hi,

I am using maven.

I am using the following versions:


Mockito 1.9.5 and PowerMock 1.5.3

And still the same.

br,

//mike



mike

unread,
Oct 13, 2014, 6:02:09 AM10/13/14
to powe...@googlegroups.com
Hi,

Was this correct versions?

//mike

Johan Haleby

unread,
Oct 13, 2014, 6:26:52 AM10/13/14
to powe...@googlegroups.com
Yes I should be the correct version. You could try upgrading PowerMock to 1.5.6 to see if it makes any difference (but I don't think so). You can also try to bootstrap PowerMock from the suite.xml (see powermock-testng documentation) to see if that helps.

mike

unread,
Oct 16, 2014, 4:40:24 AM10/16/14
to powe...@googlegroups.com
Hi,

I tried that but managed to find a workaround so that I could extend PowerMockTestCase.

The DemoTestBase extends PowerMockTestCase ( and has an interface for the other methods that it would need).

Here is the new problem that I am facing.

org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, you naughty developer!

This refers to the following line in my test ( see below):

before = methods().fault().retrieve();

For some reason it seems that this code is interpreted as a when().thenReturn ( powermock code).

Any ideas why?

//mike

@PrepareForTest({ OneClazz.class, SecondClazz.class })
public class MyTestCase extends DemoTestBase {



    private Object before;
    private MyHelper myHelperMock;

   

    @BeforeMethod
    public void setup() throws Exception {
        //Some mocking here
       
        myHelperMock = mock(MyHelper .class);
    }



    @Test
    public void testAlarmsOnCppNode() throws Exception {

        //Some when.thenReturn()
   
        //get alarms before upgrade
        before = methods().fault().retrieve();

Johan Haleby

unread,
Oct 16, 2014, 5:22:25 AM10/16/14
to powe...@googlegroups.com
Could you show the mock setup where you now have "//Some when.thenReturn()" or is all you have that comment?

mike

unread,
Oct 16, 2014, 6:51:35 AM10/16/14
to powe...@googlegroups.com
I cannot send it in public, company policy, but can I send it to you directly and then we can provide an answer ( more general ) here?

//mike

Johan Haleby

unread,
Oct 16, 2014, 8:57:08 AM10/16/14
to powe...@googlegroups.com
Would it be possible to come up with an example that demonstrates the issue that doesn't use the code you have in production?

mike

unread,
Oct 16, 2014, 9:27:05 AM10/16/14
to powe...@googlegroups.com
Not really but if I comment out the line I mentioned I get no complaints. Then when I put it back I get the complaint. But if I understand you correctly you suspect my mock setup to be the cause, right?

//mike

mike

unread,
Oct 17, 2014, 9:00:55 AM10/17/14
to powe...@googlegroups.com
Hi,

I finally managed to factor out the problem with the code:


I have a static final void (phew!) method in LoggingApi that makes it trickier. I have tried 2 ways in my testcase ( method annotated with @Test)  to get around this but I have not managed.
I am extending PowerMockTestCase and @PrepareForTest(LoggingApi.class)

These are the alternatives:

1. PowerMockito.doNothing().when(LoggingApi.class, "setTestEnd");

Message:

java.lang.NullPointerException
    at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.addAnswersForStubbing(PowerMockitoStubberImpl.java:67)
    at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:42)
    at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:105)
    at example.MyTestCase.myTestCase(MyTestCase.java:42)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)


2.     PowerMockito.mockStatic(LoggingApi.class);
        final LoggingApi logginApi = mock(LoggingApi.class);
       
        PowerMockito.doNothing().when(logginApi).setTestEnd();


Message:


org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, you naughty developer!

    at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:260)
    at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192)
    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:123)
    at org.powermock.core.MockGateway.methodCall(MockGateway.java:62)
    at <company package>LoggingApi.setTestEnd(tLoggingApi.java)
    at example.MyTestCase.myTestCase(MyTestCase.java:47)



I guess I am doing something syntactically wrong!

Any hints?

//mike

mike

unread,
Oct 17, 2014, 9:24:15 AM10/17/14
to powe...@googlegroups.com
Just a minor note. Found out that LoggingApi class is also final.

Can't really understand why you would have final on both method and class...

//

mike

unread,
Oct 20, 2014, 1:06:02 AM10/20/14
to powe...@googlegroups.com
Any ideas?

//mike

Johan Haleby

unread,
Oct 20, 2014, 8:51:16 AM10/20/14
to powe...@googlegroups.com
Since all you want to do if I interpret you correctly is to do nothing when that method is called I would suggest you to try the stubbing API instead and see if that works better. I.e. 

suppress(method(LoggingApi.class, "setTestEnd"));


/Johan

mike

unread,
Oct 22, 2014, 7:06:47 AM10/22/14
to powe...@googlegroups.com
Thanks that was sufficient!

//mike
Reply all
Reply to author
Forward
0 new messages