PowerMockito on Android (Eclipse)

128 views
Skip to first unread message

Akshat Jain

unread,
Nov 25, 2014, 9:30:06 PM11/25/14
to powe...@googlegroups.com
Hello all powermock users,

I am trying to write some Android testcases using Powermockito. I am using eclipse as my IDE and run the tests as "Android JUnit tests"

When I just Mockito v1.9.5 (along with the Dexmaker-1.1.jar & Dexmaker-mockito-1.1.jar) all my test pass. Some of the test have mocks and some don't.

When I add the powermock-mockito-1.5.6-full.jar to my libs folder and change my imports to 

 

   import static org.powermock.api.mockito.PowerMockito.*;


Things start to break. (  I am just using mock() and when() calls )

Below is my testcase class:





@RunWith(PowerMockRunner.class)

@PrepareForTest({AuthSettings.class})

public class NetworkServicesImplTest extends ActivityTestCase{


 
@Override

 
protected void setUp() throws Exception  {

     
super.setUp();

     
System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());

     
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
     obj
= new MyObj();

 
}

       
private MyObject obj;

        public void testSimpleTest() {

               Response response = mock(Response.class);

               Data data = mock(Data.class);


                when(response.getString()).thenReturn("dummy");

                obj.setString(response);

               String str1 = obj.getString(dsContext, data);

               assertEquals("dummy",str1);

       }

       public void testGetDSClientExecutionContextURL() throws MalformedURLException {

               DSHttpResponse response;

               // Mock our class
       
       HttpRequestParameters mockHttpRequest = mock(HttpRequestParameters.class);

               final URL url = new URL("https://");

               try{
                     
// PowerMock API                        
                       whenNew(HttpRequestParameters.class).withNoArguments().thenReturn(mockHttpRequest);

                       response = network.get(dsContext, url);

               }catch(Exception ex){

                       response = null;

                       fail("should not have got an exception" + ex);

               }

               assertNotNull(response);

       }





This is the Exception on the first testcase:


java
.lang.VerifyError: org/mockito/cglib/core/ReflectUtils

at org
.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167)

at org
.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)

at org
.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)

at org
.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)

at org
.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)

at org
.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)

at org
.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)

at org
.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70)

at org
.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:85)

at org
.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62)

at org
.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111)

at org
.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60)

at org
.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143)

at com
.citrix.work.network.impl.test.NetworkServicesImplTest.testGetDSClientExecutionContextURL(NetworkServicesImplTest.java:91)

at java
.lang.reflect.Method.invokeNative(Native Method)

at android
.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)

at android
.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)

at android
.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)

at android
.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)

at android
.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)

at android
.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729)




and the subsequest testcase throws 



java.lang.NoClassDefFoundError: org/mockito/internal/creation/jmock/ClassImposterizer$3
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:85)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60)
at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143)
at com.citrix.work.network.impl.test.NetworkServicesImplTest.testGetDSClientExecutionContextURLHttpRequestParameters(NetworkServicesImplTest.java:107)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729)



 






Does anyone here have successfully done Unit testing on an Android Project using PowerMockito ?

Any help would be highly appreciated.


Thanks,

Akshat


Johan Haleby

unread,
Nov 26, 2014, 1:13:57 AM11/26/14
to powe...@googlegroups.com
You must make sure to use the right combination of PowerMock and Mockito. See the compatibility matrix here.

--
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.

Akshat Jain

unread,
Nov 26, 2014, 2:41:18 AM11/26/14
to powe...@googlegroups.com
Hello Johan,

I already did that.
Here are the jar files that I have in my test project's  libs/ folder

powermock-mockito-1.5.6-full.jar
mockito-all-1.9.5.jar
dexmaker-1.1.jar
dexmaker-mockito-1.1.jar

Thanks,
Akshat

Johan Haleby

unread,
Nov 26, 2014, 2:53:08 AM11/26/14
to powe...@googlegroups.com
Are you trying to run the tests on the device or from your IDE?

Akshat Jain

unread,
Nov 26, 2014, 4:25:00 PM11/26/14
to powe...@googlegroups.com
So I run the tests on my device using Eclipse : Run as -> Android JUnit Tests


But when I ran them from Command line setting the classpath, the tests ran.
I ran the test using JUnit4 via the 


java -cp . org.junit.runner.JUnitCore <testclass>


 

Johan Haleby

unread,
Nov 27, 2014, 12:48:17 AM11/27/14
to powe...@googlegroups.com
Ok then it's possible that there's some quirks with the classpath setup in Eclipse.
Reply all
Reply to author
Forward
0 new messages