Running tests on an Android emulator or device is slow! Building, deploying, and launching the app often takes a minute or more. That's no way to do TDD. There must be a better way.

//原生的单元测试的运行时测试形式很慢,构建部署启动也会费很长时间,使用这个框架就是个比较好的办法

Wouldn't it be nice to run your Android tests directly from inside your IDE? Perhaps you've tried, and been thwarted by the dreadedjava.lang.RuntimeException: Stub!?

Robolectric is a unit test framework that de-fangs the Android SDK jar so you can test-drive the development of your Android app. Tests run inside the JVM on your workstation in seconds. With Robolectric you can write tests like this:

//robolectric是一个单元测试框架,测试运行在JVM中只需要几秒

  1. @RunWith(RobolectricTestRunner.class)
  2. public class MyActivityTest {
  3.  
  4. @Test
  5. public void clickingButton_shouldChangeResultsViewText() throws Exception {
  6. MyActivity activity = Robolectric.setupActivity(MyActivity.class);
  7.  
  8. Button button = (Button) activity.findViewById(R.id.button);
  9. TextView results = (TextView) activity.findViewById(R.id.results);
  10.  
  11. button.performClick();
  12. assertThat(results.getText().toString()).isEqualTo("Robolectric Rocks!");
  13. }
  14. }

Robolectric makes this possible by rewriting Android SDK classes as they're being loaded and making it possible for them to run on a regular JVM.

//robolectric让重写Android sdk类变得可能,因为这些类已经被加载然后在正常的JVM运行也变得可能

SDK, Resources, & Native Method Emulation

Robolectric handles inflation of views, resource loading, and lots of other stuff that's implemented in native C code on Android devices. This allows tests to do most things you could do on a real device. It's easy to provide our own implementation for specific SDK methods too, so you could simulate error conditions or real-world sensor behavior, for example.

Run Tests Outside of the Emulator

Robolectric lets you run your tests on your workstation, or on your Continuous Integration environment in a regular JVM, without an emulator. Because of this, the dexing, packaging, and installing-on-the emulator steps aren't necessary, reducing test cycles from minutes to seconds so you can iterate quickly and refactor your code with confidence.

No Mocking Frameworks Required

An alternate approach to Robolectric is to use mock frameworks such as Mockito or to mock out the Android SDK. While this is a valid approach, it often yields tests that are essentially reverse implementations of the application code.

Robolectric allows a test style that is closer to black box testing, making the tests more effective for refactoring and allowing the tests to focus on the behavior of the application instead of the implementation of Android. You can still use a mocking framework along with Robolectric if you like.

Robolectric Test-Drive Your Android Code的更多相关文章

  1. android code 和js的交互

    小弟现在需要android code 和js的交互.出现了问题,求大家带一带啊. 我的页面:<!DOCTYPE html><html lang="en">& ...

  2. Android code wiki

    Android code wiki Tip1: 类的全局静态变量的使用,这样可以静态变量只分配一次内存,可以不通过类的对象也就是可以通过类名直接使用该变量.(使用场景:Request_Code ,Re ...

  3. 安卓自动生成代码插件-Android code Generator(转)

    编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! 介绍 A ...

  4. android code bbs for developer

    http://bbs.aiyingli.com/forum.php http://www.eoeandroid.com/ http://www.javaapk.com/demo http://www. ...

  5. Android Migrate Android Code

    近期在更改程序架构的时候,用引用Library的方式实现程序的共享化过程中发现R.id.xx的文件不能够在  switch cases  中使用 看代码提示,如下信息: As of ADT 14, r ...

  6. Android开发的插件Code Generator与LayoutCreator的安装与使用,提升你的开发效率

    前言 大家好,给大家带来Android开发的插件Code Generator与LayoutCreator的安装与使用,提升你的开发效率的概述,希望你们喜欢 学习目标 掌握两个插件的安装和使用,能够实现 ...

  7. Robolectric 探索之路

    layout: post title: Roboletric探索之路,从抗拒到依赖 description: Roboletric Android Unit Testing category: blo ...

  8. Android application testing with the Android test framework

    目录(?)[-] Android automated testing 1 How to test Android applications Tip 2 Unit tests vs functional ...

  9. React Native Android入门实战及深入源代码分析系列(2)——React Native源代码编译

    本文为老曾原创.转载需注明出处:viewmode=contents">http://blog.csdn.net/minimicall?viewmode=contents 在上一节中,我 ...

随机推荐

  1. Mac os 10.9下面配置JAVA_HOME

    刚入手的的MBP,就开始配置java环境,搜了一下网上的都是10.9以前的配置方法.jdk7在10.9的安装目录变化了. 首先到Oracle官网下载最新版本的java,直接默认安装 cd /etc s ...

  2. Contest 20140928 密碼破譯 字符串hash

    題意:詢問字符串指定區間循環節個數. 解法:有循環節長度a的字符串s[x,y]的性質:s[x,y-a]==s[x+a,y]由此寫一個雙hash就行了. #include<iostream> ...

  3. <jsp:forward>、requestDispatcher和sendRedirect()的区别

    1.会话信息保存在服务器内存上,可以断续访问,和cookie相比,其保存在服务器上. 2.男人就像蓝牙:只有在你接近时,他才会找上你.当你离开后,他便又去找其他的"设备"了.女人就 ...

  4. Codeforces Round #204 (Div. 2): B

    很简单的一个题: 只需要将他们排一下序,然后判断一下就可以了! 代码: #include<cstdio> #include<algorithm> #define maxn 10 ...

  5. 如何用 React Native 创建一个iOS APP?(三)

    前两部分,<如何用 React Native 创建一个iOS APP?>,<如何用 React Native 创建一个iOS APP (二)?>中,我们分别讲了用 React ...

  6. SQL servere 范式、事务

    一.数据库范式: 1.构造数据库必须遵循一定的规则.在关系数据库中,这种规则就是范式. 范式是符合某一种级别的关系模式的集合.数据库中的关系必须满足一定的要求,即满足不同的范式. 满足最低要求的范式是 ...

  7. oracle的存储过程语法(转)

    1.ORA-00942: table or view does not exist 指的你要操作的表尚未存在,需要先create出来先. 2.ORA-00922: missing or invalid ...

  8. Java多线程(四)之ConcurrentSkipListMap深入分析

    一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下,ConcurrentHashMap 存取速度是ConcurrentSki ...

  9. 【HDOJ】3584 Cube

    三位树状数组. /* 3584 */ #include <iostream> #include <string> #include <map> #include & ...

  10. poj3709

    首先我们发现将一段数变为同一个数比间隔着搞肯定优,因为数列是升序的,然后不难得到方程式f[i]=min(f[j]+sum[i]-sum[j]-(i-j)*a[j+1]) (i-j>=m)简单的斜 ...