java - UnsupportedOperationException with Mockito and Dexmaker -


recently i've been struggling mockito. after valiant efforts, made compile without errors except 1 in particular case :

when i'm mocking package-private class mockito, test in same package, following error :

java.lang.unsupportedoperationexception: cannot proxy inaccessible class class [...].androidcalendargenerator.manageeventsui.calendarmonitorserviceconnection     @ com.android.dx.stock.proxybuilder.buildproxyclass(proxybuilder.java:269)     @ com.android.dx.mockito.dexmakermockmaker.createmock(dexmakermockmaker.java:56)     @ org.mockito.internal.util.mockutil.createmock(mockutil.java:33)     @ org.mockito.internal.mockitocore.mock(mockitocore.java:59)     @ org.mockito.mockito.mock(mockito.java:1285)     @ org.mockito.mockito.mock(mockito.java:1163)     [...] 

here class :

package [...].androidcalendargenerator.manageeventsui;  import org.junit.test; import org.junit.runner.runwith; import org.mockito.runners.mockitojunitrunner;  import static org.mockito.mockito.mock;  @runwith(mockitojunitrunner.class) public class calendarmonitorserviceconnectiontest {      @test     public void testone() {         calendarmonitorserviceconnection c1 = new calendarmonitorserviceconnection(null);         calendarmonitorserviceconnection c = mock(calendarmonitorserviceconnection.class);     } } 

i think first line of test compiling without errors means test in same folder calendarmonitorserviceconnection class try mock.

finally, have imports in build.gradle :

androidtestcompile 'junit:junit:4.12' androidtestcompile 'org.mockito:mockito-core:1.10.19' androidtestcompile "com.crittercism.dexmaker:dexmaker:1.4" androidtestcompile "com.crittercism.dexmaker:dexmaker-mockito:1.4" androidtestcompile "com.crittercism.dexmaker:dexmaker-dx:1.4" 

what missing ?

thanks lot answers


edit

here's code of class try mock :

package [...].androidcalendargenerator.manageeventsui;  import android.content.componentname; import android.content.serviceconnection; import android.os.ibinder;  import java.util.arraylist; import java.util.calendar; import java.util.date;  import [...].androidcalendargenerator.calendareventmonitor; import [...].androidcalendargenerator.event; import [...].androidcalendargenerator.eventchangelistener;  class calendarmonitorserviceconnection implements serviceconnection {      private calendareventmonitor mmonitor;     private eventlistviewadapter meventlistviewadapter;      calendarmonitorserviceconnection(eventlistviewadapter eventlistviewadapter) {         meventlistviewadapter = eventlistviewadapter;     }      /**      * custom event change listener defines in case of changes in calendar      */     private class customeventchangelistener implements eventchangelistener {         @override         public void oneventactivated(event event) {             //[...]         }          @override         public void oneventremoved(event event) {             //[...]         }          @override         public void oneventlistchanged() {             //[...]         }     }      /**      * pulls data next 30 days , send adapter      */     private void sendnextthirtydayseventstoadapter() {         //[...]     }      @override     public void onserviceconnected(componentname classname,                                    ibinder service){         //[...]     }      @override     public void onservicedisconnected(componentname arg0){         //[...]     } } 

after research, seems me :

  • mockito + dexmaker cannot mock package-private class, unit tests being in same package.
  • powermock can mock package-private class, if unit tests in same package.

therefore solution, want keep class package-private, use powermock.

main sources :


i'll update post try powemock. or if can agree or correct answer.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -