Exception while casting Binder to LocalBinder in JUnit testing with Robolectric.How to write unit test for it?

38
January 12, 2019, at 7:20 PM

Versions I am using-
compile 'org.robolectric:robolectric:2.4' Android version 2.3.3

Test case from test file.

    @Test
    public void onStartTest() throws Exception{    
        IMMMDialogPopup  immmDialogPopup = mock(IMMMDialogPopup.class);
        objMIPDialogActivity.setDialogPopup(immmDialogPopup);
        ComponentName expectedComponentName = new ComponentName(objMMMDialogActivity.getPackageName(),MIPService.class.getName());
        Binder expectedBinder = new Binder();
        ShadowApplication shadowApplication = Robolectric.getShadowApplication();
        shadowApplication.setComponentNameAndServiceForBindService(expectedComponentName,expectedBinder);
        objMMMDialogActivity.onStart();     
      }

Actual Implementation to verify. mConnection is object of ServiceConnection.Call bindService() then it jumps to onServiceConnected() of ServiceConnection.

 @Override
    protected void onStart() {
        super.onStart();
bindService(new Intent(this, MIPService.class), mConnection, Context.BIND_AUTO_CREATE); 
    }

Call onServiceConnected() method of ServiceConnection from where it throws exception while casting Binder to local binder. It is giving exception at line "MIPService.LocalBinder binder = (MIPService.LocalBinder) service;"

  private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {
            if(dialogPopup != null) {
                MIPService.LocalBinder binder = (MIPService.LocalBinder) service;
                mipService = binder.getService();
                mipService.registerListener(MMMDialogActivity.this);
                showPopup(MMMDialogActivity.this.dialogPopup.getDialog(), false, centerInParent);
                mBound = true;
            }
        }

Exception -

It jumps to Scheduler.class and throws exception and hit in  this.isExecutingRunnable = false; 
  private void runOrQueueRunnable(Runnable runnable, long scheduledTime) {
        if(this.isExecutingRunnable) {
            this.queueRunnableAndSort(runnable, scheduledTime);
        } else {
            this.isExecutingRunnable = true;
            try {
                runnable.run();
            } finally {
                this.isExecutingRunnable = false;
            }
        }
READ ALSO
i want to make a seekbar with this different background drawbles

i want to make a seekbar with this different background drawbles

see image over here I have already completed adding flags but with different backgrounds seems quite difficult I want to make an seekbar like this any suggestion thanks in advance

25
Cannot resolve adapter method when using DifUtil

Cannot resolve adapter method when using DifUtil

I want to update my RecyclerView using DifUtil to prevent duplicatesThis is how my User class look like:

47
Sorting of ArrayList shows first item twice

Sorting of ArrayList shows first item twice

i try to Sort my arrayList depending on the distance between user and the locationTo do this i created the following two methods:

33
How to customize SwipeToReferesh layout top ProgressBar or Loader?

How to customize SwipeToReferesh layout top ProgressBar or Loader?

I would like to know how can I customize SwipeToRefresh layoutIn specific, I am trying to hide that loader

19