美文网首页文艺的安卓君移动开发狂热者(299402133)IT圈
Android单元测试框架Robolectric3.0介绍(一)

Android单元测试框架Robolectric3.0介绍(一)

作者: geniusmart | 来源:发表于2016-01-21 00:37 被阅读25677次

一、关于Robolectric3.0

文章中的所有代码在此:https://github.com/geniusmart/LoveUT ,由于 Robolectric 3.0 和 3.1 版本(包括后续3.x版本)差异不小,该工程中包含这两个版本对应的测试用例 Demo 。

作为一个软件开发攻城狮,无论你多不屑多排斥单元测试,它都是一种非常好的开发方式,且不谈TDD,为自己写的代码负责,测试自己写的代码,在自己力所能及的范围内提高产品的质量,本是理所当然的事情。

那么如何测试自己写的代码?点点界面,测测功能固然是一种方式,但是如果能留下一段一劳永逸的测试代码,让代码测试代码,岂不两全其美?所以,写好单元测试,爱惜自己的代码,爱惜颜值高的QA妹纸,爱惜有价值的产品(没价值的、政治性的、屁股决定脑袋的产品滚粗),人人有责!

对于Android app来说,写起单元测试来瞻前顾后,一方面单元测试需要运行在模拟器上或者真机上,麻烦而且缓慢,另一方面,一些依赖Android SDK的对象(如Activity,TextView等)的测试非常头疼,Robolectric可以解决此类问题,它的设计思路便是通过实现一套JVM能运行的Android代码,从而做到脱离Android环境进行测试。本文对Robolectric3.0做了简单介绍,并列举了如何对Android的组件和常见功能进行测试的示例。

二、环境搭建

Gradle配置

在build.gradle中配置如下依赖关系:

testCompile "org.robolectric:robolectric:3.0"

通过注解配置TestRunner

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class SampleActivityTest {

}

Android Studio的配置

  1. 在Build Variants面板中,将Test Artifact切换成Unit Tests模式(注:新版本的as已经不需要做这项配置),如下图:


    配置Test Artifact
  2. working directory 设置为$MODULE_DIR$

如果在测试过程遇见如下问题,解决的方式就是设置working directory的值:

java.io.FileNotFoundException: build\intermediates\bundles\debug\AndroidManifest.xml (系统找不到指定的路径。)

设置方法如下图所示:

Edit Configurations Working directory的配置

更多环境配置可以参考官方网站

三、Activity的测试

  1. 创建Activity实例
@Test
public void testActivity() {
        SampleActivity sampleActivity = Robolectric.setupActivity(SampleActivity.class);
        assertNotNull(sampleActivity);
        assertEquals(sampleActivity.getTitle(), "SimpleActivity");
    }
  1. 生命周期
@Test
public void testLifecycle() {
        ActivityController<SampleActivity> activityController = Robolectric.buildActivity(SampleActivity.class).create().start();
        Activity activity = activityController.get();
        TextView textview = (TextView) activity.findViewById(R.id.tv_lifecycle_value);
        assertEquals("onCreate",textview.getText().toString());
        activityController.resume();
        assertEquals("onResume", textview.getText().toString());
        activityController.destroy();
        assertEquals("onDestroy", textview.getText().toString());
    }
  1. 跳转
@Test
public void testStartActivity() {
        //按钮点击后跳转到下一个Activity
        forwardBtn.performClick();
        Intent expectedIntent = new Intent(sampleActivity, LoginActivity.class);
        Intent actualIntent = ShadowApplication.getInstance().getNextStartedActivity();
        assertEquals(expectedIntent, actualIntent);
    }

注:Robolectric 3.1 之后,不建议用 Intent.equals() 的方式来比对两个 Intent ,因此以上代码将无法正常执行。目前建议用类似代码来断言:

assertEquals(expectedIntent.getComponent(), actualIntent.getComponent());

当然,Intent 有很多属性,如果需要分别断言的话比较麻烦,因此可以用一些第三方库,比如 assertj-android 的工具类 IntentAssert

  1. UI组件状态
@Test
public void testViewState(){
        CheckBox checkBox = (CheckBox) sampleActivity.findViewById(R.id.checkbox);
        Button inverseBtn = (Button) sampleActivity.findViewById(R.id.btn_inverse);
        assertTrue(inverseBtn.isEnabled());

        checkBox.setChecked(true);
        //点击按钮,CheckBox反选
        inverseBtn.performClick();
        assertTrue(!checkBox.isChecked());
        inverseBtn.performClick();
        assertTrue(checkBox.isChecked());
    }
  1. Dialog
@Test
public void testDialog(){
        //点击按钮,出现对话框
        dialogBtn.performClick();
        AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog();
        assertNotNull(latestAlertDialog);
    }
  1. Toast
@Test
public void testToast(){
        //点击按钮,出现吐司
        toastBtn.performClick();
        assertEquals(ShadowToast.getTextOfLatestToast(),"we love UT");
    }
  1. Fragment的测试
    如果使用support的Fragment,需添加以下依赖
testCompile "org.robolectric:shadows-support-v4:3.0"

shadow-support包提供了将Fragment主动添加到Activity中的方法:SupportFragmentTestUtil.startFragment(),简易的测试代码如下

@Test
public void testFragment(){
    SampleFragment sampleFragment = new SampleFragment();
    //此api可以主动添加Fragment到Activity中,因此会触发Fragment的onCreateView()
    SupportFragmentTestUtil.startFragment(sampleFragment);
    assertNotNull(sampleFragment.getView());
}
  1. 访问资源文件
@Test
public void testResources() {
        Application application = RuntimeEnvironment.application;
        String appName = application.getString(R.string.app_name);
        String activityTitle = application.getString(R.string.title_activity_simple);
        assertEquals("LoveUT", appName);
        assertEquals("SimpleActivity",activityTitle);
    }

四、BroadcastReceiver的测试

首先看下广播接收者的代码

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences.Editor editor = context.getSharedPreferences(
                "account", Context.MODE_PRIVATE).edit();
        String name = intent.getStringExtra("EXTRA_USERNAME");
        editor.putString("USERNAME", name);
        editor.apply();
    }
}

广播的测试点可以包含两个方面,一是应用程序是否注册了该广播,二是广播接受者的处理逻辑是否正确,关于逻辑是否正确,可以直接人为的触发onReceive()方法,验证执行后所影响到的数据。

@Test
public void testBoradcast(){
        ShadowApplication shadowApplication = ShadowApplication.getInstance();

        String action = "com.geniusmart.loveut.login";
        Intent intent = new Intent(action);
        intent.putExtra("EXTRA_USERNAME", "geniusmart");

        //测试是否注册广播接收者
        assertTrue(shadowApplication.hasReceiverForIntent(intent));

        //以下测试广播接受者的处理逻辑是否正确
        MyReceiver myReceiver = new MyReceiver();
        myReceiver.onReceive(RuntimeEnvironment.application,intent);
        SharedPreferences preferences = shadowApplication.getSharedPreferences("account", Context.MODE_PRIVATE);
        assertEquals( "geniusmart",preferences.getString("USERNAME", ""));
    }

五、Service的测试

Service的测试类似于BroadcastReceiver,以IntentService为例,可以直接触发onHandleIntent()方法,用来验证Service启动后的逻辑是否正确。

public class SampleIntentService extends IntentService {
    public SampleIntentService() {
        super("SampleIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(
                "example", Context.MODE_PRIVATE).edit();
        editor.putString("SAMPLE_DATA", "sample data");
        editor.apply();
    }
}

以上代码的单元测试用例:

@Test
public void addsDataToSharedPreference() {
        Application application = RuntimeEnvironment.application;
        RoboSharedPreferences preferences = (RoboSharedPreferences) application
                .getSharedPreferences("example", Context.MODE_PRIVATE);

        SampleIntentService registrationService = new SampleIntentService();
        registrationService.onHandleIntent(new Intent());

        assertEquals(preferences.getString("SAMPLE_DATA", ""), "sample data");
    }

六、Shadow的使用

Shadow是Robolectric的立足之本,如其名,作为影子,一定是变幻莫测,时有时无,且依存于本尊。因此,框架针对Android SDK中的对象,提供了很多影子对象(如Activity和ShadowActivity、TextView和ShadowTextView等),这些影子对象,丰富了本尊的行为,能更方便的对Android相关的对象进行测试。

1.使用框架提供的Shadow对象

@Test
public void testDefaultShadow(){

    MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);

    //通过Shadows.shadowOf()可以获取很多Android对象的Shadow对象
    ShadowActivity shadowActivity = Shadows.shadowOf(mainActivity);
    ShadowApplication shadowApplication = Shadows.shadowOf(RuntimeEnvironment.application);

    Bitmap bitmap = BitmapFactory.decodeFile("Path");
    ShadowBitmap shadowBitmap = Shadows.shadowOf(bitmap);

    //Shadow对象提供方便我们用于模拟业务场景进行测试的api
    assertNull(shadowActivity.getNextStartedActivity());
    assertNull(shadowApplication.getNextStartedActivity());
    assertNotNull(shadowBitmap);

}   

2.如何自定义Shadow对象

首先,创建原始对象Person

public class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

其次,创建Person的Shadow对象

@Implements(Person.class)
public class ShadowPerson {

    @Implementation
    public String getName() {
        return "geniusmart";
    }
}

接下来,需自定义TestRunner,添加Person对象为要进行Shadow的对象(注:Robolectric 3.1 起可以省略此步骤)。

public class CustomShadowTestRunner extends RobolectricGradleTestRunner {

    public CustomShadowTestRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    public InstrumentationConfiguration createClassLoaderConfig() {
        InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
        /**
         * 添加要进行Shadow的对象
         */
        builder.addInstrumentedPackage(Person.class.getPackage().getName());
        builder.addInstrumentedClass(Person.class.getName());
        return builder.build();
    }
}

最后,在测试用例中,ShadowPerson对象将自动代替原始对象,调用Shadow对象的数据和行为

@RunWith(CustomShadowTestRunner.class)
@Config(constants = BuildConfig.class,shadows = {ShadowPerson.class})
public class ShadowTest {

    /**
     * 测试自定义的Shadow
     */
    @Test
    public void testCustomShadow(){
        Person person = new Person("genius");
        //getName()实际上调用的是ShadowPerson的方法
        assertEquals("geniusmart", person.getName());

        //获取Person对象对应的Shadow对象
        ShadowPerson shadowPerson = (ShadowPerson) ShadowExtractor.extract(person);
        assertEquals("geniusmart", shadowPerson.getName());
    }
}

七、关于代码

文章中的所有代码在此:https://github.com/geniusmart/LoveUT
另外,除了文中所示的代码之外,该工程还包含了Robolectric官方的测试例子,一个简单的登录功能的测试,可以作为入门使用,界面如下图。

官方的登录测试DEMO

八、参考文章

http://robolectric.org
https://github.com/robolectric/robolectric
http://tech.meituan.com/Android_unit_test.html

关于代码中的日志如何输出、网络请求、数据库操作如何测试,请移步第二篇文章Android单元测试框架Robolectric3.0介绍(二)

相关文章

网友评论

  • 李云龙_:哎呀呀,大块人心啊,楼主,每次都是调试,蛋疼的一笔,Android 原生的单元测试写的又是蛋疼的很,这会终于找到组织了,我是怀着激动的心情看完的,
  • 8470a652c28e:java.lang.NoClassDefFoundError: android/content/pm/PackageManager$NameNotFoundException 每次都是都是这个错 楼主你的可以跑?????????????????????????????
  • 304251ccd3fb: @Before
    public void setUp(){
    LoginActivity loginActivity = Robolectric.setupActivity(LoginActivity.class);
    btLogin = (Button) loginActivity.findViewById(R.id.btn_login_login);
    etAccount = (EditText) loginActivity.findViewById(R.id.edit_login_account);
    etPassWord = (EditText) loginActivity.findViewById(R.id.edit_login_password);
    }
    启动测试方法的时候,这个里面报错 java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details.,怎么弄?
    304251ccd3fb:LoginActivity报空指针
  • 扬州慢_:想请问下,一些第三方库要如何在 UT 中使用呢?一些第三方库需要在 application 中初始化, UI 里面运行直接初始化就无法通过。
  • 陈德湾:楼主你还在吗我用你的3.0 demo跑是报java.lang.NoClassDefFoundError: com/android/internal/os/BackgroundThread
    我自己的项目引入
    报Exception in thread "main" java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.robolectric.annotation.Config.application()
    刚刚入门,尝试了网上别人的方法都没解决掉。。
  • cwjbest:很详细,对初学者很有用,多谢楼主
  • 20ac0170353d:有个问题需要用到context.getAssets().open("XXX")加载asset目录下的文件时,要是遇到以下错误:

    java.io.FileNotFoundException: build/intermediates/bundles/debug/assets/https.cer (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    这种问题该怎么解决,只能更换assert文件的读取方式么
  • 逆袭的产品旺:代码展示的方式,用户体验不好,
  • 适当车:你好 测试service那个例子 intentService.onHandleIntent(new Intent()); 报空指针,请问属于什么情况
    适当车:谢谢:smile:
    geniusmart:3.1 中的 api 变了,service 的实例化不要直接用 new,使用 Robolectric.buildService(MyService.class)。

    详见这个:issue
    https://github.com/robolectric/robolectric/issues/2437
  • kanghb:我们项目有.so库,貌似没法搞...:sob:
  • 08862ce1dbc6:测试值得学习
  • 希腊明天:赞一个
  • Mark_Liu:@geniusmart 最近在学习android 单元测试,因为开发使用的SDK都是比较新的,故尝试Robolectric 3.1.4版本,SDK版本 23,测试报 java.lang.NoClassDefFoundError: javax/microedition/khronos/opengles/GL,估测是javax包没加载,暂不知如何处理,如有方案,麻烦转告,谢谢
    linkeo:加上这个就可以了

    testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
  • 2292504ec1e9:博主你好,很喜欢你的文章,最近在研究Robolectric,碰到一个问题,不知道你是否遇到过,能否帮忙看下:
    如果对自定义的类,如Person类,写其对应ShadowPerson类,在测试时,可以正常走ShadowPerson类中定义的方法。但是如果是对java存在的类,如File类,写其对应的ShadowFile类,则在测试时,是不能走ShadowFile类中定义的方法的。 Robolectric3.1.4和Robolectric3.0我都试过了,结果是一样的。
  • 少年阿洁:你好,这里给出完整的错误:
    Unable to resolve artifact: Missing:
    ----------
    1) com.ximpleware:vtd-xml:jar:2.11

    Try downloading the file manually from the project website.

    Then, install it using the command:
    mvn install:install-file -DgroupId=com.ximpleware -DartifactId=vtd-xml -Dversion=2.11 -Dpackaging=jar -Dfile=/path/to/file

    Alternatively, if you host your own repository you can deploy the file there:
    mvn deploy:deploy-file -DgroupId=com.ximpleware -DartifactId=vtd-xml -Dversion=2.11 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

    Path to dependency:
    1) org.apache.maven:super-pom:pom:2.0
    2) org.robolectric:shadows-core:jar:21:3.0
    3) org.robolectric:robolectric-resources:jar:3.0
    4) com.ximpleware:vtd-xml:jar:2.11

    。。。。

    ----------
    7 required artifacts are missing.

    for artifact:
    org.apache.maven:super-pom:pom:2.0

    from the specified remote repositories:
    sonatype (https://oss.sonatype.org/content/groups/public/),
    central (http://repo1.maven.org/maven2)


    at org.apache.maven.artifact.resolver.DefaultArtifactResolver.resolveTransitively(DefaultArtifactResolver.java:360)
    at org.apache.maven.artifact.ant.DependenciesTask.doExecuteResolution(DependenciesTask.java:263)
    ... 23 more


    Process finished with exit code -1
  • 少年阿洁:你好,我想问下,为啥我环境弄好,准备执行测试代码的时候,在输出框中,总是提示:
    Downloading: org/robolectric/android-all/5.0.0_r2-robolectric-1/android-all-5.0.0_r2-robolectric-1.jar from repository sonatype at https://oss.sonatype.org/content/groups/public/
    [INFO] Unable to find resource 'org.json:json:jar:20080701' in repository sonatype (https://oss.sonatype.org/content/groups/public/)
    。。。类似的语句,然后就是失败
    5ea4beb37efa:同学你好 我是小米工程师 最近也在学习 单元测试 可以加微信好友不 shaoxy1992 我的微信 注明简书单元测试。
    少年阿洁:@geniusmart 谢谢你的回复,我这边试了很多次,翻墙也没成功,希望加联系方式详聊。
    geniusmart:@太子阿洁 第一次跑UT,要下载相关的依赖,需要科学上网,多试几次就OK了
  • e113140718ad:代码中有访问sd卡和sqlite数据库,怎么进行单元测试啊?
    geniusmart:@myfeifei 第二篇文章里有数据库的测试。sd卡,你可以google一下 robolectric sd card
  • 红颜疯子:我下了demo,运行测试的时候,一直有个无法访问AndroidHttpClient类文件,不知道为什么,错误定位到MainActivityTest的shadowOf方法
    geniusmart:@红颜疯子 都21以上,这问题很好解决,我上文不是说得很清楚了,在test文件夹下加上AndroidHttpClient空类,一点也不侵入你的业务代码,就可以解决此问题
    红颜疯子:@红颜疯子 额,这个好蛋疼啊,难道你们用的都是21以下的版本?
    geniusmart:@红颜疯子
    你应该是用了21以上的sdk版本,这个问题可以在test目录下,新增包和对应的类:android.net.http.AndroidHttpClient.java,如下
    package android.net.http;

    public class AndroidHttpClient {
    }


    可以详细看看这个issue:
    https://github.com/robolectric/robolectric/issues/1862
  • c1455ddef2d5:刚入坑
  • f65d71618558:请问一下楼主运行那个测试时报错java.lang.NoClassDefFoundError: com/android/internal/os/BackgroundThread,可是程序可以正常运行如何解决。
    陈德湾:到底怎么解决啊,我也是这个问题
    houtengzhi:同样有这个问题,应该是SDK版本比较低
  • 开悟2020:初次研究robolectric,楼主写的用例很详细,对我帮助很大。谢谢
  • c1b405d8ae82:这个TestRunner是个什么东西
  • richy_:学习了
  • 吴晨:憋说话,吻我
  • 吴晨:为什么我在test下的绿色java文件夹上面点右键 选择run 'All Tests'就会在shadowTest的testCustomShadow里面就会报错,但是单独测试这个方法就不会出问题
    geniusmart:@吴晨wchen 嗯,断点时可以看到,run all tests时调用的是Person,而非ShadowPerson,我再研究研究。
    吴晨: @geniusmart 好的 谢谢,我发现run all tests的时候,ShadowPerson类的构造函数没有调用
    geniusmart:@吴晨wchen 这个问题我也百思不得其解,给官方github提issue了,还没回复我,有回复或者我研究出来了,第一时间回复你
  • 接地气的二呆:网络请求的结果怎么模拟
    接地气的二呆:@geniusmart 使用的是 volley 没有找到合适的方法,异步的怎么模拟
    geniusmart:@接地气的二呆 如果是使用retrofit可参考下面这篇文章,后续会写一篇这方面的文章
    http://stackoverflow.com/questions/17544751/square-retrofit-server-mock-for-testing
  • __Berial___:Build Variants面板中没有Test Artifact。。。 :joy:
    明镜本清净anany:@__Berial___ 憋说话,吻鹏神
    __Berial___:@__Berial___ 好吧,2.0版本之后不需要设置Test Artifact就可以直接用了,但是api最高支持到21好伤
  • U3:有一个问题,如果com.android.tools.build:gradle的版本过旧,例如,使用了,1.2.3版本,那么就会出现异常无法运行,这时候就需要更新到1.3.1或者其他版本,希望大家别猜踩坑
  • 680f5e4aaa83:不错的东西
  • lovexiaov:赞一个,开篇就说出了国内现状。其实测试是开发流程中的一个必需环节。
    小鄧子:@lovexiaov 求带
    lovexiaov: @geniusmart 互粉,哈哈。希望以后多多交流,最近也在研究ANDROID测试
    geniusmart:@lovexiaov 赞,粉了

本文标题:Android单元测试框架Robolectric3.0介绍(一)

本文链接:https://www.haomeiwen.com/subject/efzhkttx.html