前言
為什么需要寫單元測試什么的我就不多說了。
我也是第一次接觸Android的單元測試,Android的單元測試框架也不少,為啥我選擇了Robolectric?
因為我看了這兩篇文章:
http://www.10tiao.com/html/169/201611/2650821538/1.html
https://tech.meituan.com/Android_unit_test.html
相信大神的選擇不會錯。
我項目使用的框架 MVPArms
JUnit4
首先我們要先了解JUnit4的幾個注解:
| 注解 | 說明 | 翻譯 |
|---|---|---|
| @RunWith | When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit. | 當(dāng)一個類被@RunWith注釋或擴展@RunWith注釋的類時,JUnit將調(diào)用它引用的類來運行該類中的測試,而不是內(nèi)置在JUnit中的運行器。 |
| @Rule | Annotates fields that reference rules or methods that return a rule. | 注釋引用規(guī)則或返回規(guī)則的方法的字段。 |
| @Test | The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. | Test注釋告訴JUnit,它所注釋的public void方法可以作為測試用例運行。 |
| @Before | When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before the Test method. | 使用@Before注解的public void 的方法 會在編寫的測試用例之前執(zhí)行,所以可以用來做一些初始化的操作 |
| @After | If you allocate external resources in a Before method you need to release them after the test runs. Annotating a public void method with @After causes that method to be run after the Test method. | 使用@After注解的public void 的方法會在運行完測試用例之后執(zhí)行,可以在這個方法論釋放資源 |
需要了解更多可以查看 JUnit4的API手冊
Mockito
這里主要提示一下mock,spy的區(qū)別
使用mock生成的類,所有方法都不是真實的方法,而且返回值都是NULL。
使用spy生成的類,所有方法都是真實方法,返回值都是和真實方法一樣的。
Robolectric
不多少了,直接看代碼吧
配置
//robolectric 單元測試
testImplementation 'org.robolectric:robolectric:4.1'
testImplementation 'org.robolectric:shadows-support-v4:latest.release'
// Mockito
testImplementation "org.mockito:mockito-core:2.8.9"
不要忘記這個
android {
...
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
創(chuàng)建一個ActivityTest
1.在需要測試的Activity上:鼠標右鍵 -> Go To -> Test -> Create New Test ...

2.就會出現(xiàn) Create Test 彈窗 -> 點擊OK

3.選擇test的那個目錄,不是androidTest !!!(說三遍)
點擊OK就完成了

Demo
下面是一個登錄頁面的Test
testLogin()模擬的是:點擊登錄按鈕 --> mock登錄請求 --> 登錄成功 --> 跳轉(zhuǎn)到主頁
testToRegister() 模擬的是:點擊注冊按鈕 --> 跳轉(zhuǎn)到注冊頁面
先上代碼:
//import ...省略
@RunWith(RobolectricTestRunner.class)
public class LoginActivityTest {
private final String LOGIN_JSON = "{\"chapterTops\":[],\"collectIds\":[7656,1905,7666,7679,7688,7556,3242,7654,7700,7697,7410,7661]," +
"\"email\":\"\",\"icon\":\"\",\"id\":14540,\"password\":\"\",\"token\":\"\",\"type\":0,\"username\":\"12341234\"}";
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
private static boolean hasInited = false;
private LoginActivity loginActivity;
@Mock
LoginModel loginModel;
private Gson gson;
@Before
public void setUp() throws Exception {
//將rxjava 的異步操作轉(zhuǎn)化為同步
RxJavaPlugins.setIoSchedulerHandler(scheduler -> Schedulers.trampoline());
RxAndroidPlugins.setMainThreadSchedulerHandler(scheduler -> Schedulers.trampoline());
//初始化Mockito
MockitoAnnotations.initMocks(this);
//獲取測試的activity
loginActivity = Robolectric.setupActivity(LoginActivity.class);
gson = new Gson();
// 因為我們不需要它LoginModel中的方法返回真正的值,只是需要mock它的方法返回我們想要的值,所以用@Mock
// 而LoginModule我們是需要它提供真是的view所以我們用spy
LoginModule loginModule = Mockito.spy(new LoginModule(loginActivity));
Mockito.when(loginModule.provideLoginModel(Mockito.any(LoginModel.class)))
.thenReturn(loginModel);
// 我們mock出的module需要注入到測試的activity中
DaggerLoginComponent
.builder()
.appComponent(ArmsUtils.obtainAppComponentFromContext(loginActivity))
.loginModule(loginModule)
.build()
.inject(loginActivity);
}
/**
* 將登錄的請求直接mock成我們想要的結(jié)果
*/
@Test
public void testLogin(){
System.out.println("*********** testLogin 開始 *********");
BaseResponse<Login> response = new BaseResponse<>();
response.setCode("0");
response.setData(gson.fromJson(LOGIN_JSON,Login.class));
//將去服務(wù)器請求login數(shù)據(jù)的方法直接mock成我們想得到數(shù)據(jù)
Mockito.when(loginModel.login(Mockito.anyMap()))
.thenReturn(Observable.just(response));
EditText userName = loginActivity.findViewById(R.id.account_edt);
EditText password = loginActivity.findViewById(R.id.password_edt);
userName.setText("12341234");
password.setText("123456");
//模擬點擊 登錄按鈕
loginActivity.findViewById(R.id.login_btn).performClick();
//檢查是否登錄成功跳轉(zhuǎn)到了UserActivity
Intent intent = new Intent(loginActivity, UserActivity.class);
Assert.assertEquals(intent.getComponent(),Shadows.shadowOf(loginActivity).getNextStartedActivity().getComponent());
System.out.println("*********** testLogin 完成 *********");
}
@Test
public void testToRegister(){
System.out.println("*********** testToRegister 開始 *********");
loginActivity.findViewById(R.id.register_tv).performClick();
Intent intent = new Intent(loginActivity,RegisterActivity.class);
Assert.assertEquals(intent.getComponent(),Shadows.shadowOf(loginActivity).getNextStartedActivity().getComponent());
System.out.println("*********** testToRegister 完成 *********");
}
}
如果測試中有異步一定要加上
//將rxjava 的異步操作轉(zhuǎn)化為同步
RxJavaPlugins.setIoSchedulerHandler(scheduler -> Schedulers.trampoline());
RxAndroidPlugins.setMainThreadSchedulerHandler(scheduler -> Schedulers.trampoline());
如果你用的是Rxjava1.+
if (!hasInited){
hasInited = true;
RxJavaPlugins.getInstance().registerSchedulersHook(new RxJavaSchedulersHook() {
@Override
public Scheduler getIOScheduler() {
return Schedulers.immediate();
}
});
RxAndroidPlugins.getInstance().registerSchedulersHook(new RxAndroidSchedulersHook() {
@Override
public Scheduler getMainThreadScheduler() {
return Schedulers.immediate();
}
});
}
因為我們不需要它LoginModel中的方法返回真正的值,只是需要mock它的方法返回我們想要的值,所以用@Mock
而LoginModule我們是需要它提供真是的view所以我們用spy
這是我的 Debug Configurations

總結(jié)
這里只是最簡單的使用,有時間還會繼續(xù)研究。
Robolectric看上去好像很好用,==但是用起來感覺好多坑。
就到這里吧,有問題歡迎一起討論。
拜拜了您嘞?。。?/p>