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

为自己写的代码负责,测试自己写的代码,在自己力所能及的范围内提高产品的质量,本是理所当然的事情。

Robolectric

一、关于Robolectric3.0

作为一个软件开发攻城狮,无论你多不屑多排斥单元测试,它都是一种非常好的开发方式,且不谈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模式,如下图:

    配置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");
     }
  2. 生命周期
    @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());
     }
  3. 跳转
    @Test
    public void testStartActivity() {
         //按钮点击后跳转到下一个Activity
         forwardBtn.performClick();
         Intent expectedIntent = new Intent(sampleActivity, LoginActivity.class);
         Intent actualIntent = ShadowApplication.getInstance().getNextStartedActivity();
         assertEquals(expectedIntent, actualIntent);
     }
  4. 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());
     }
  5. Dialog
    @Test
    public void testDialog(){
         //点击按钮,出现对话框
         dialogBtn.performClick();
         AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog();
         assertNotNull(latestAlertDialog);
     }
  6. Toast
    @Test
    public void testToast(){
         //点击按钮,出现吐司
         toastBtn.performClick();
         assertEquals(ShadowToast.getTextOfLatestToast(),"we love UT");
     }
  7. 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());
    }
  8. 访问资源文件
    @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的对象

public class CustomShadowTestRunner extends RobolectricGradleTestRunner {

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

    @Override
    public InstrumentationConfiguration createClassLoaderConfig() {
        InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
        /**
         * 添加要进行Shadow的对象
         */
        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());
    }
}

【声明:本文经授权转自简书,作者:geniusmart未经许可,禁止转载。】

本文原地址:http://www.jianshu.com/p/9d988a2f8ff7

最新文章
1客户案例研究:专家安全扫描,守护金融银行小程序安全和私密性 WeTest私有化部署的定制扫描平台让金融银行客户能无成本接入扫描系统并迅速上手使用。客户能方便快捷地根据定制手册进行自助扫描,根据生成的扫描报告,详细洞察漏洞,快速识别并准确定位问题根源。
2客户案例研究:专家渗透测试,洞察电子商务小程序重大交易漏洞 通过WeTest渗透测试服务,某知名零售公司旗下的在线购物类小程序中发现了8处安全风险,我们的安全专家为客户提供了详细的漏洞报告,提供了较为清晰完整的安全加固方案。在回归测试中,中危以上风险均被解决。
3自查小程序4大安全隐患!文末免费赠送小程序安全扫描专业版! 腾讯WeTest现面向小程序开发者开放免费申请使用小程序安全扫描专业版,助您提前发现全面的安全漏洞。扫描文中问卷二维码或点击问卷链接,即可报名参与免费领取活动。
4浅谈渗透测试服务在泛互行业带来的价值 在泛互联网行业中,渗透测试服务对于保障企业的网络安全至关重要。
5云手机卡顿/无特定设备/商店登录受限怎么办?WeTest专有云帮您解决! 公有云满足了大量小微企业、个人的测试需求;随着客户深入使用,也遇到了一系列新问题。本篇将对几个常见问题予以解答
购买
客服
反馈