教程網(wǎng)上比較多嗜愈,但是因?yàn)樽钚掳媸?.0了旧蛾,所以用上面的官網(wǎng)robolectric資料還是比較好莽龟,配置比較簡(jiǎn)單,基本是在gradle里加上依賴(lài)蚜点,再在build variants里把a(bǔ)rtifact改為junit就可以開(kāi)始寫(xiě)擦了轧房。
testCompile 'junit:junit:4.12'
testCompile "org.robolectric:robolectric:3.0"
本來(lái)打算要做項(xiàng)目中使用,中間了遇到了幾個(gè)問(wèn)題绍绘,當(dāng)然如果你只是用官方的demo去實(shí)驗(yàn)奶镶,好像一切都很完美。
試過(guò)demo陪拘,我也是覺(jué)得這個(gè)框架很強(qiáng)大厂镇,運(yùn)行很快,不需要連接手機(jī)左刽,但是我接入之前寫(xiě)的一個(gè)app時(shí)就遇到問(wèn)題了捺信,首先遇到一個(gè)warn加exception
WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only.
To remove this warning, annotate your test class with @Config(manifest=Config.NONE).
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f03001b
at org.robolectric.shadows.ShadowResources.checkResName(ShadowResources.java:343)
at org.robolectric.shadows.ShadowResources.resolveResName(ShadowResources.java:338)
at org.robolectric.shadows.ShadowResources.loadXmlResourceParser(ShadowResources.java:429)
at android.content.res.Resources.loadXmlResourceParser(Resources.java)
這個(gè)可以通過(guò)在Config里配置manifest路徑解決。
然后是初始化時(shí)自定義的application里異常欠痴,因?yàn)槲以赼pplication里進(jìn)行了一些初始化迄靠,比如sd卡目錄什么的,導(dǎo)致直接運(yùn)行失敗喇辽,后來(lái)用框架提供的shadow掌挚,然后在Config里配置application為自定義的就可以了。
@Implements(AssistApplication.class)
public class ShadowApplication extends Application{
@Implementation
public static void initDownloader(){}
}
之后想測(cè)試一下toast測(cè)試
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class,
manifest = "src/main/AndroidManifest.xml",
application = ShadowApplication.class,
shadows = {ShadowApplication.class},
sdk = 19)
public class AssistActivityTest {
@Before
public void setUp(){
}
@Test
public void testAssist(){
ProxyActivity activity = Robolectric.setupActivity(ProxyActivity.class);
View view = activity.findViewById(R.id.id_layout_proxy_switch);
view.performClick();
ShadowLooper.idleMainLooper();
assertEquals(ShadowToast.getLatestToast(),"請(qǐng)?jiān)O(shè)置ip地址");
}
}
遇到了如下異常菩咨,去github上看了[issues](https://github.com/robolectric/robolectric/labels/defect?page=2&q=is%3Aopen+label%3Adefect)還是有很多問(wèn)題沒(méi)解決吠式,所以暫時(shí)還是打算不在項(xiàng)目中使用了。
java.lang.NullPointerException
at org.robolectric.shadows.ShadowAssetManager$OverlayedStyle.equals(ShadowAssetManager.java:318)
at org.robolectric.shadows.ShadowAssetManager.applyThemeStyle(ShadowAssetManager.java:291)
at org.robolectric.shadows.ShadowResources$ShadowTheme.applyStyle(ShadowResources.java:461)
at android.content.res.Resources$Theme.applyStyle(Resources.java)
at android.view.ContextThemeWrapper.onApplyThemeResource(ContextThemeWrapper.java:132)
at android.app.Activity.onApplyThemeResource(Activity.java:3360)
at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:144)
at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:89)
at org.robolectric.shadows.ShadowActivity.setThemeFromManifest(ShadowActivity.java:85)
at org.robolectric.shadows.CoreShadowsAdapter$1.setThemeFromManifest(CoreShadowsAdapter.java:35)
at org.robolectric.util.ActivityController.attach(ActivityController.java:58)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:121)