在使用Robolectric进行JUnit测试中将Binder转换为LocalBinder时发生异常。如何编写单元测试?

时间:2019-01-12 11:41:38

标签: android unit-testing junit robolectric

我正在使用的版本-
 编译'org.robolectric:robolectric:2.4'         Android版本2.3.3

测试文件中的测试用例。

    @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();     
      }

要验证的实际实施。     mConnection是ServiceConnection的对象。调用bindService()然后跳到ServiceConnection的onServiceConnected()。

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

调用ServiceConnection的onServiceConnected()方法,从该方法在将Binder强制转换为本地绑定程序时引发异常。   它在“ MIPService.LocalBinder活页夹=(MIPService.LocalBinder)服务;”行中给出了例外情况

  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;
            }
        }

例外-

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;
            }

        }

0 个答案:

没有答案