Cannot get past ClassNotPreparedException using Powermock 1.7.0 Mockito and TestNG

5,209 views
Skip to first unread message

stuart....@bt.com

unread,
Jun 28, 2017, 5:47:26 AM6/28/17
to PowerMock

Could someone please help:

 

I have been trying to get this to work for about 5 days with different versions of Powermock, Mockito, Junit and TestNG.
I an trying to mock a static method without much luck. 
I have tried 'whenNew' on other classes in the chain but it either does nothing (No logs, no errors just calls the original code) or I get 'classNotPrepared' or other exceptions.

 

I actually would prefer to use 'whenNew' on a different class but I have tried many variations and many classes without success.

 

There does not seem to be a logging mechanism that would inform me when things work or not so I feel I am working in the dark.

I also do not want to down the Spring configuration route as managing the Spring config files in this legacy project is a nightmare.

 

Also Tried - powermock-mockito2-testng-1.7.0 but got:

java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/MethodInterceptor.

Gave up after some time with Google and went back to  powermock-mockito-testng-1.7.0.

 

Current Software environment:

Powermock: powermock-mockito-testng-1.7.0.

Java: 1.7

Eclipse: 4.6.3

Windows 10 Running in VirtualBox.

 

I am trying to Mock a method in a library class. This class is an import from another project and may have been compiled using Java 1.6 (possibly even 1.5).

 

The Class is a Legacy class that implements the Singleton pattern. It wraps SpringContext with a private constructor and a getInstance() method. It also has the following static method (Not sure why its static!):

public static Object getBean(String beanName)

 

My test code (Very much redacted due to company policies):

@RunWith(PowerMockRunner.class)

@PrepareForTest(SpringWrapper.class)

public class TestSW extends UnitTestBase {

@Test

public void testMock() {

try {

PowerMockito.mockStatic(SpringWrapper.class);   // This is line 94! Does not get past here...

PowerMockito.when(SpringWrapper.getBean(Mockito.anyString())).thenReturn(null);

} catch (Exception e) {

e.printStackTrace();

}

}

}

 
Stack Trace:

at org.powermock.api.mockito.expectation.reporter.MockitoPowerMockReporter.classNotPrepared(MockitoPowerMockReporter.java:32)

at org.powermock.api.mockito.internal.mockcreation.MockTypeValidatorFactory$DefaultMockTypeValidator.validate(MockTypeValidatorFactory.java:38)

at org.powermock.api.mockito.internal.mockcreation.AbstractMockCreator.validateType(AbstractMockCreator.java:10)

at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:56)

at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:46)

at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:71)

at com.bt.*****************************************************.unit.TestSW.testMock(TestSW.java:94)

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:606)

at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)

at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)

at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)

at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)

at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)

at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)

at org.testng.TestRunner.privateRun(TestRunner.java:782)

at org.testng.TestRunner.run(TestRunner.java:632)

at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)

at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)

at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)

at org.testng.SuiteRunner.run(SuiteRunner.java:268)

at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)

at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)

at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)

at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)

at org.testng.TestNG.run(TestNG.java:1064)

at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)

at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)

at org.testng.remote.RemoteTestNG.main(RemoteTestN

Zagretdinov Arthur

unread,
Jun 28, 2017, 4:25:55 PM6/28/17
to powe...@googlegroups.com
Hi,

Current Software environment:
Powermock: powermock-mockito-testng-1.7.0.
Java: 1.7
Eclipse: 4.6.3
Windows 10 Running in VirtualBox.

I’m a bit confused. You pointed that you use powermock-mockito-testng-1.7.0. Does it mean that you download zip file and unpack it? 

Also Tried - powermock-mockito2-testng-1.7.0 but got:
java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/MethodInterceptor.

Could you provide full stack trace? 

@RunWith(PowerMockRunner.class)
@PrepareForTest(SpringWrapper.class)
public class TestSW extends UnitTestBase {
@Test
public void testMock() {
try {
PowerMockito.mockStatic(SpringWrapper.class);   // This is line 94! Does not get past here...
PowerMockito.when(SpringWrapper.getBean(Mockito.anyString())).thenReturn(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}

@RunWith - it’s JUnit annotation. But As I see from stack trace you run test with TestNG. 

Please, read Wiki about how to configure TestNG to run with PowerMock 



Best regrads,
Arthur Zagretdinov



--
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 https://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages