Android單元測試Mockito+Robolectric

前言

為什么需要寫單元測試什么的我就不多說了。
我也是第一次接觸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 ...


image.png

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


image.png

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

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


image.png

總結(jié)

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一.基本介紹 背景: 目前處于高速迭代開發(fā)中的Android項目往往需要除黑盒測試外更加可靠的質(zhì)量保障,這正是單元...
    anmi7閱讀 2,182評論 0 6
  • Android單元測試介紹 處于高速迭代開發(fā)中的Android項目往往需要除黑盒測試外更加可靠的質(zhì)量保障,這正是單...
    東經(jīng)315度閱讀 3,424評論 6 37
  • 前面花了很大篇幅來介紹JUnit4,JUnit4是整個單元測試的基礎(chǔ),其他的測試框架都是跑在JUnit4上的。接下...
    云飛揚1閱讀 6,182評論 2 51
  • 什么是單元測試 在計算機編程中,單元測試(Unit Testing)又稱為模塊測試, 是針對程序模塊(軟件設(shè)計的最...
    HelloCsl閱讀 11,107評論 1 46
  • @Author:彭海波 前言 單元測試(又稱為模塊測試, Unit Testing)是針對程序模塊(軟件設(shè)計的最小...
    海波筆記閱讀 5,129評論 0 52

友情鏈接更多精彩內(nèi)容