天天看點

二.Android單元測試 Mockito的更多用法

一.Android單元測試 Mockito的簡單用法

三.Android單元測試 PowerMock給私有變量指派

四.Android單元測試 PowerMock mock靜态方法

 相對于上篇文章,這篇介紹了更多mockito的用法,對照着MeetingPresenterImp.java和MeetingPresenterImpTest.java大家可以看看Android單元測試用例怎麼寫.

public class MeetingPresenterImp implements MeetingContract.MeetingPresenter {

    private MeetingController meetingController;

    private MeetingAudioManager audioManager;

    private MeetingContract.MeetingView meetingView;

    private MeetingParticipant localParticipant;

    public MeetingPresenterImp(MeetingContract.MeetingView meetingView, MeetingController meetingController, MeetingAudioManager audioManager) {
        this.meetingController = meetingController;
        this.audioManager = audioManager;
        this.meetingView = meetingView;
    }

    //開關攝像頭
    @Override
    public void toggleCamera() {
        meetingController.toggleCamera();
    }

    //開關揚聲器
    @Override
    public void toggleSpeaker() {
        if (audioManager.isSpeakerOn()) {
            audioManager.turnOffSpeaker();
        } else {
            audioManager.turnOnSpeaker();
        }
    }

    //設定Orientation
    public void setOrientation(int orientation) {
        meetingController.setOrientation(orientation);
    }

    //更新MainView的可見度
    public void updateMainViewVisibility(boolean isShowAllButtons) {
        meetingView.updateButtonsVisibility(isShowAllButtons);
        meetingView.updateSelfSurfaceVisibility(isShowAllButtons);
        meetingView.updateSelfIconVisibility(isShowAllButtons);
    }

    //更新會議時間
    public void updateMeetingTime(long time) {
        Date d = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        meetingView.updateTime(sdf.format(d));
    }

    //觀看某位與會人的視訊源
    synchronized void startWatchVideoSource(MeetingParticipant participant, long mainSourcePtr, long minSourcePtr) {
        if (participant == null || participant.isBeingWatched()) {
            return;
        }

        if (participant.equals(localParticipant)) {
            meetingController.setSelfPreviewView(mainSourcePtr, minSourcePtr);
        } else {
            meetingController.startWatchVideoSource(participant.getUri(), mainSourcePtr, , , , , );
        }

        participant.setBeingWatched(true);
    }

}
           
@RunWith(MockitoJUnitRunner.class)
public class MeetingPresenterImpTest {

    @Mock
    MeetingContract.MeetingView meetingView;

    @Mock
    MeetingController meetingController;

    @Mock
    MeetingAudioManager meetingAudioManager;

    MeetingPresenterImp meetingPresenterImp;

    private Random random = new Random();

    @Before
    public void setUp() throws Exception {
        meetingPresenterImp = new MeetingPresenterImp(meetingView, meetingController, meetingAudioManager);
    }

    //簡單的驗證方法是否執行
    @Test
    public void toggleCamera() throws Exception {
        meetingPresenterImp.toggleCamera();

        Mockito.verify(meetingController).toggleCamera();
    }

    //使用when().thenReturn控制mock對象的方法的傳回值
    @Test
    public void should_turn_off_speaker() throws Exception {
        Mockito.when(meetingAudioManager.isSpeakerOn()).thenReturn(true);

        meetingPresenterImp.toggleSpeaker();

        Mockito.verify(meetingAudioManager).turnOffSpeaker();
    }

    //同上,但是測試的是另外一個分支,這裡isSpeakerOn()傳回false
    @Test
    public void should_turn_on_speaker() throws Exception {
        Mockito.when(meetingAudioManager.isSpeakerOn()).thenReturn(false);

        meetingPresenterImp.toggleSpeaker();

        Mockito.verify(meetingAudioManager).turnOnSpeaker();
    }

    //測試方法執行還不夠,測試傳入的參數是否一緻
    @Test
    public void should_set_orientation() throws Exception {
        meetingPresenterImp.setOrientation();
        verify(meetingController).setOrientation();
    }

    //使用inOrder()測試方法是否按特定順序執行
    @Test
    public void should_update_main_view_visibility() throws Exception {
        boolean isShowAllButtons = random.nextBoolean();

        meetingPresenterImp.updateMainViewVisibility(isShowAllButtons);
        InOrder inOrder = inOrder(meetingView);
        inOrder.verify(meetingView).updateButtonsVisibility(isShowAllButtons);
        inOrder.verify(meetingView).updateSelfSurfaceVisibility(isShowAllButtons);
        inOrder.verify(meetingView).updateSelfIconVisibility(isShowAllButtons);
    }

    //測試一個String被format後的值是否和想要的一緻,情況一
    @Test
    public void should_update_meeting_time_30_seconds() throws Exception {
        meetingPresenterImp.updateMeetingTime( * );
        verify(meetingView).updateTime("00:00:30");
    }

    //測試一個String被format後的值是否和想要的一緻,情況二
    @Test
    public void should_update_meeting_time_61_seconds() throws Exception {
        meetingPresenterImp.updateMeetingTime( * );
        verify(meetingView).updateTime("00:01:01");
    }

    //使用never測試一個方法一次都沒有被執行
    @Test
    public void should_start_watch_video_source_when_participant_is_null() throws Exception {
        meetingPresenterImp.startWatchVideoSource(null, , );

        verify(meetingController, never()).setSelfPreviewView(anyLong(), anyLong());
        verify(meetingController, never()).startWatchVideoSource(anyString(), anyLong(), anyInt(), anyInt(), anyInt(), anyInt(), anyInt());
    }

}