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