目的
充分的单元测试就是提高代码质量最有效的手段之一,而单元测试严重依赖代码的可测试性,本文主要通过一个简单的DEMO演示如何对Android原生应用进行单元测试,同时示例代码采用MVP模式以提高代码的可读性和可测试性
简介
在Android原生应用开发中,存在两种单元测试:本地JVM测试和Instrumentation测试。本文仅介绍本地JVM测试
本地jvm的单元测试
这种方式运行速度快,对运行环境没有特殊要求,可以很方便的做自动化测试,是单元测试首选的方法Instrumentation测试
Instrumentation测试需要运行在Android环境下,可以是模拟器或者手机等真实设备。这种方式运行速度慢,且严重依赖Android运行环境,更适合用来做集成测试
准备
我准备了一个简单的APP,模拟一个耗时的网络请求获得一段数据并显示在界面上,针对这个APP编写单元测试用例并进行本地单元测试。
依赖库
依赖库 | 作用 |
---|---|
JUnit-4.12 | 基础得单元测试框架 |
Robolectric-3.8 | Android SDK测试框架 |
PowerMock-1.6.6 | 模拟被测对象依赖的静态方法 |
Mockito-1.10.19 | 模拟被测对象依赖的对象 |
配置build.gradle
- 增加编译选项,在测试中包含资源文件
testOptions {
unitTests {
includeAndroidResources true
}
}
- 添加测试依赖库
testImplementation 'junit:junit:4.12'
testImplementation 'org.robolectric:robolectric:3.8'
testImplementation 'org.robolectric:shadows-supportv4:3.8'
testImplementation 'org.powermock:powermock-module-junit4:1.6.6'
testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.6'
testImplementation 'org.powermock:powermock-api-mockito:1.6.6'
testImplementation 'org.powermock:powermock-classloading-xstream:1.6.6'
testImplementation 'org.mockito:mockito-all:1.10.19'
测试Activity
测试Activity主要是测试它各个生命周期的状态变化、对外界输入的响应是否符合预期,Activity测试完全依赖Android SDK,需要用Robolectric。
Robolectric是一个开源的单元测试框架,能够完全模拟Android SDK并在JVM中运行。
UI依赖于Persenter,在Activity中通过静态工厂方法创建依赖的Presenter实例,需要使用PowerMock来模拟创建Presenter过程,完成Presenter模拟对象的注入
配置
- 通过@RunWith指定使用RobolectricTestRunner
- 通过@Config配置Robolectric的运行环境
- 通过@PrepareForTest配置PowerMock需要模拟的静态类型
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 21, constants = BuildConfig.class)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@PrepareForTest({PresenterFactory.class})
@Before
public void setUp() {
appContext = RuntimeEnvironment.application.getApplicationContext();
PowerMockito.mockStatic(PresenterFactory.class);
}
onCreate用例
通过Robolectric的ActivityController来构建并管理activity的生命周期,运行至onCreate阶段,然后验证这个阶段text1是否正确初始化
@Test
public void onCreate_text1() {
MainActivity activity = Robolectric.buildActivity(MainActivity.class).create().get();
String expect = appContext.getString(R.string.hell_world);
assertEquals(expect, ((TextView)activity.findViewById(R.id.lbl_text1)).getText());
}
Click Button1用例
Activity完全显示以后,验证button1的click操作是否显示toast消息
@Test
public void btn1_click() {
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
activity.findViewById(R.id.btn_1).performClick();
String expect = appContext.getString(R.string.hell_world);
assertEquals(expect, ShadowToast.getTextOfLatestToast());
}
Click Button2用例
Activity完全显示以后,验证button2的click操作是否调用了presenter的fetch方法
@Test
public void btn2_click() {
MainContract.Presenter presenter = Mockito.mock(MainContract.Presenter.class);
PowerMockito.when(PresenterFactory.create(Mockito.any(MainContract.View.class), Mockito.any(AppExecutors.class)))
.thenReturn(presenter);
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
activity.findViewById(R.id.btn_2).performClick();
Mockito.verify(presenter, Mockito.times(1))
.fetch();
}
测试Presenter
Presenter的测试一般可以不用依赖Android SDK了,Presenter依赖于底层的领域服务,也依赖上层View,demo中对领域服务的依赖没有通过构造函数的方式注入,而是通过静态工厂方法构建,还是需要用到PowerMock
配置
- 通过@RunWith指定使用PowerMockRunner
- 通过@PrepareForTest配置PowerMock需要模拟的静态类型
@RunWith(PowerMockRunner.class)
@PrepareForTest({ServiceFactory.class})
@Before
public void setUp() {
PowerMockito.mockStatic(ServiceFactory.class);
}
成功路径用例
验证View的方法是否成功调用且调用参数是否一致
@Test
public void fetch_success() {
String expected = "hello world";
SlowService service = Mockito.mock(SlowService.class);
Mockito.when(service.fetch()).thenReturn(expected);
PowerMockito.when(ServiceFactory.create())
.thenReturn(service);
MainContract.View view = Mockito.mock(MainContract.View.class);
MainPresenter presenter = new MainPresenter(view, executors);
presenter.fetch();
Mockito.verify(service, Mockito.times(1)).fetch();
Mockito.verify(view, Mockito.times(1)).onFetchStarted();
Mockito.verify(view, Mockito.times(1)).onFetchCompleted();
Mockito.verify(view, Mockito.times(0)).onFetchFailed(Mockito.anyObject());
ArgumentCaptor captor = ArgumentCaptor.forClass(String.class);
Mockito.verify(view, Mockito.times(1)).onFetchSuccess(captor.capture());
assertEquals(expected, captor.getValue());
}
失败路径用例
@Test
public void fetch_failed() {
RuntimeException exception = new RuntimeException("fetch failed");
SlowService service = Mockito.mock(SlowService.class);
Mockito.when(service.fetch()).thenThrow(exception);
PowerMockito.when(ServiceFactory.create())
.thenReturn(service);
MainContract.View view = Mockito.mock(MainContract.View.class);
MainPresenter presenter = new MainPresenter(view, executors);
presenter.fetch();
Mockito.verify(service, Mockito.times(1)).fetch();
Mockito.verify(view, Mockito.times(1)).onFetchStarted();
Mockito.verify(view, Mockito.times(1)).onFetchCompleted();
ArgumentCaptor captor = ArgumentCaptor.forClass(Throwable.class);
Mockito.verify(view, Mockito.times(1)).onFetchFailed(captor.capture());
assertEquals(exception, captor.getValue());
Mockito.verify(view, Mockito.times(0)).onFetchSuccess(Mockito.anyString());
}
测试Service
Service不会对上层有依赖,可以直接使用JUnit测试
public class SlowServiceImplTest {
@Test
public void fetch_data() {
SlowServiceImpl impl = new SlowServiceImpl();
String data = impl.fetch();
assertEquals("from slow service", data);
}
}
自动化测试
自动化测试一般是在持续集成环境中使用命令来执行单元测试
gradlew :app:testDebugUnitTest
总结
写完这个demo,总觉得给Android APP做单元测试还是非常简单的,作为一个优秀的程序员,怎么能够不关注自己的代码质量呢,还是自己动手试试吧
源码下载
https://github.com/hziee514/android-testing
参考资料
Robolectric
Using PowerMock
Mockito
PowerMock