Android类

时间:2017-09-19 16:06:30

标签: java android unit-testing mockito

我正在尝试对使用SharedPrefrences的片段和活动以及其他Android相关类进行一些单元测试。但是,当我尝试模拟以下内容时,我似乎总是得到ClassNotFound异常:

ClassNotFoundException: android.content.Context

以下是一个例子:

我的'TimerFragment'包含'MyNotificationManager'类。

@RunWith(MockitoJUnitRunner.class)
public class TimerFragmentUnitTest {

    @Mock
    TimerFragmentView view;
    @Mock
    MyNotificationManger notificationManger;


    private TimerFragmentPresenter presenter;

    @Before
    public void setUp(){
        presenter = new TimerFragmentPresenter(notificationManger);
        presenter.bindView(view);
    }

    @Test
    public void shouldSendNotification() throws Exception{
        //Given
        doNothing().when(view).setStopEnabled(false);
        doNothing().when(view).setStopEnabled(true);
        //When
        presenter.setMinutesAndSeconds(2000);
        TimeUnit.MILLISECONDS.sleep(3000);
        //Then
        verify(notificationManger,times(1)).sendTimerFinishNotification();


    }

}

在此示例中,我收到ClassNotFoundException: android.content.Context错误,因为MyNotificationManager中有Context个对象。

任何建议都将受到赞赏。

感谢。

以下是MyNotificationTimer

的实现
    public class MyNotificationManger {

        private static final String EXTRA_NOTIFICATION_TIMER = "notification_timer";
        private static final int TIMER_NOTIFICATION_ID = 1;
        private final Context _context;

        public MyNotificationManger(Context context) {
            _context = context;
        }

        public void sendTimerFinishNotification() {
            int icon = Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? R.mipmap.ic_launcher : R.drawable.methodegg;

            Notification.Builder builder =
                    new Notification.Builder(_context)
                            .setSmallIcon(icon)
                            .setContentTitle(_context.getString(R.string.timer))
                            .setContentText(_context.getString(R.string.ready))
                            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM))
                            .setAutoCancel(true);

            Intent resultIntent = new Intent(_context, NavigationActivity.class);
            resultIntent.putExtra(EXTRA_NOTIFICATION_TIMER, true);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(
                    _context,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(resultPendingIntent);


            NotificationManager notificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(TIMER_NOTIFICATION_ID, builder.getNotification());

        }
    }

1 个答案:

答案 0 :(得分:2)

管理找到答案,虽然Android文档似乎认为这个解决方案应该只用作最后的手段&#39;。我把

android {
    testOptions {
        unitTests.returnDefaultValues = true
    }
}

位于顶级build.gradle

根据文档,我应该使用'If you run a test that calls an API from the Android SDK that you do not mock',我觉得这很奇怪,因为我没有收到'Method ... not mocked'错误,我收到'ClassNotFoundException'