久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

帶有 Mockito 間諜的 Robolectric buildActivity()?

Robolectric buildActivity() with Mockito spy?(帶有 Mockito 間諜的 Robolectric buildActivity()?)
本文介紹了帶有 Mockito 間諜的 Robolectric buildActivity()?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在我看來,使用 Robolectric 的生命周期實(shí)用程序(從 Robolectric.buildActivity() 開始)構(gòu)建 Activity 單元測試和使用 Mockito 間諜監(jiān)視同一個(gè) Activity 是相互排斥的.

It seems to me that building an Activity unit test with Robolectric's lifecycle utilities (starting with Robolectric.buildActivity()) and spying on the same Activity with a Mockito spy are mutually exclusive.

因?yàn)?code>buildActivity()控制了Activity對象的構(gòu)建,所以唯一給Activity添加spy的地方就是調(diào)用buildActivity()之后.但是,在事后添加間諜時(shí),間諜無法正常工作.

Because buildActivity() controls the construction of the Activity object, the only place to add a spy for the Activity is after calling buildActivity(). However, the spy doesn't function properly when it's added after the fact.

在監(jiān)視 ActivityController 生命周期方法的副作用時(shí)尤其如此,例如 create()start()恢復(fù)().我認(rèn)為這是因?yàn)?ActivityController 持有對真實(shí)"Activity 對象的引用,而不是后來添加的間諜.

This is especially true when spying for side effects of ActivityController lifecycle methods such as create(), start() and resume(). I assume this is because the ActivityController holds a reference to the "real" Activity object and not the spy that was added later.

那么有什么方法可以監(jiān)視正在使用 Robolectric 進(jìn)行單元測試的 Activity,以便在通過 Robolectric 的 ActivityController 調(diào)用生命周期方法時(shí),間諜可以正常工作?

So is there any way to spy an Activity that's being unit tested with Robolectric, such that the spy works properly when calling the lifecycle methods via Robolectric's ActivityController?

推薦答案

答案是用反射替換ActivityController中真實(shí)的"Activity對象.p>

The answer is using the reflection to replace the "real" Activity object in ActivityController.

@Test
public void someTestMethod() throws NoSuchFieldException, IllegalAccessException {
    ActivityController<LoginActivity> ac = Robolectric.buildActivity(LoginActivity.class);
    LoginActivity spiedActivity = spy(ac.get());

    replaceComponentInActivityController(ac, spiedActivity);

    ac.create();

    // do your work
 }

public static void replaceComponentInActivityController(ActivityController<?> activityController, Activity activity)
        throws NoSuchFieldException, IllegalAccessException {
    Field componentField = ComponentController.class.getDeclaredField("component");
    componentField.setAccessible(true);
    componentField.set(activityController, activity);
}

我用Robolectric 3.1測試過,沒問題.

I test it by Robolectric 3.1, and it's ok.

這篇關(guān)于帶有 Mockito 間諜的 Robolectric buildActivity()?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出)
Multiline EditText with Done SoftInput Action Label on 2.3(2.3 上帶有完成 SoftInput 操作標(biāo)簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測向左或向右滑動?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉(zhuǎn)對話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點(diǎn)擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設(shè)置為僅接受 Android 中的數(shù)值?)
主站蜘蛛池模板: 欧美性久久 | 欧美日韩在线视频一区 | 久久免费高清视频 | 欧美一二三四成人免费视频 | 日本不卡一区 | 国产在线视频在线观看 | 亚洲精品国产a久久久久久 午夜影院网站 | 中文一区| 亚洲精品无人区 | 99re视频在线观看 | 亚洲精品乱码久久久久久蜜桃91 | 一区二区三区四区视频 | 亚洲免费av一区 | 亚洲一区二区三区四区五区午夜 | 国产一区二区三区四区hd | 亚洲精品视频免费 | 99精品久久久| 羞羞色影院 | 精品亚洲一区二区三区 | 亚洲国产成人av好男人在线观看 | 成人国产精品久久久 | 午夜视频在线 | 99re国产 | 欧美精品区| 国产剧情一区 | 亚洲一区二区av | 国产粉嫩尤物极品99综合精品 | 亚洲永久字幕 | 日本中文字幕在线观看 | 国产精品久久久久久婷婷天堂 | 午夜在线| 污书屋| 亚洲天堂免费 | 一区二区三区精品 | 国产成人精品免高潮在线观看 | 国产精品性做久久久久久 | 黄色网址在线免费观看 | 亚洲午夜av久久乱码 | 亚洲成人免费视频 | 欧洲成人 | 精品国产乱码久久久久久丨区2区 |