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 dreaded 'java.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:

// Test class for MyActivity
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {private Activity activity;private Button pressMeButton;private TextView results;@Beforepublic void setUp() throws Exception {activity = new MyActivity();activity.onCreate(null);pressMeButton = (Button) activity.findViewById(R.id.press_me_button);results = (TextView) activity.findViewById(R.id.results_text_view);}@Testpublic void shouldUpdateResultsWhenButtonIsClicked() throws Exception {pressMeButton.performClick();String resultsText = results.getText().toString();assertThat(resultsText, equalTo("Testing Android Rocks!"));}
}

Robolectric makes this possible by intercepting the loading of the Android classes and rewriting the method bodies. Robolectric re-defines Android methods so they return null (or 0, false, etc.), or if provided Robolectric will forward method calls to shadow Android objects giving the Android SDK behavior. Robolectric provides a large number of shadow objects covering much of what a typical application would need to test-drive the business logic and functionality of your application. Coverage of the SDK is improving every day.

Resource Support

Robolectric handles inflation of views, string resource lookups, etc. Some view attributes (id, visibility enabled, text, checked, and src) are parsed and applied to inflated views. Activity and View #findViewById() methods return Android view objects. Support exists for include and merge tags. These features allow tests access resources and to assert on view state.

Run Tests Outside of the Emulator

Run your tests on your workstation, or on your Continuous Integration environment. Because tests run on your workstation in a JVM and not in the emulator Android runtime, the dexing, packaging, and installation on the emulator steps are not necessary, allowing you to 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 Android Mock to mock out the Android SDK. While this is a valid approach, we have found that without Robolectric, the level of mocking needed to test an Android app quickly 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.

Contributing

We welcome contributions. Please fork and submit pull requests. Don’t forget to include tests!

Sample Project

Look at the sample project to see how fast and easy it can be to test drive the development of Android applications.

Robolectric’s current maintainers are:

  • Christian Williams, Pivotal Labs
  • Tyler Schultz, Pivotal Labs
  • Phil Goodwin, Pivotal Labs
  • Joe Moore, Pivotal Labs

Acknowledgments

  • Robolectric (previously known as “droid-sugar”) was originally developed by Christian Williams at XtremeLabs. Big thanks to XtremeLabs for their support.
  • Considerable contributions have been made by numerous Pivots, including Ian Fisher, Phil Goodwin, Ryan Richard, Joe Moore and Harry Ugol.
  • Thanks to Shane Francis for the inspired name, and to Pivots Ofri Afek and Jessica Miller for our logo!

Support

Google Group: http://groups.google.com/group/robolectric
Group Email: robolectric@googlegroups.com
Samples: http://github.com/pivotal/RobolectricSample

Roboletric_Test-Drive Your Android Code相关推荐

  1. Plugin error: Plugin ‘Android Code Generator‘ is compatible with IntelliJ IDEA only because it does

    目录 Plugin error: Plugin 'Android Code Generator' is compatible with IntelliJ IDEA only because it do ...

  2. android code修改border颜色,Android自定义TextView边框颜色(动态改变边框颜色以及字体颜色)...

    最近写了一个带边框的TextView,因为项目中有许多地方需要用到一个带边框的TextView,而且需要根据不同的状态显示不同的边框颜色,当时的第一反应就是用shape做,但后来转念一想这要需要定义多 ...

  3. Android Code Review 注意点记录(不断更新~)

    一 Review的关键节点指导 架构是否合理:虽然是在Review代码,其实很多时候我们都是在评判架构是否合理. 比如数据的同步机制是否合理等等: 跨网络请求:跨网络请求一般是性能占比最高的地方,是否 ...

  4. 提高 Android 代码质量的4个工具

    在这篇文章中,我将通过不同的自动化工具如CheckStyle,FindBugs,PMD以及Android Lint来介绍(如何)提高你的安卓代码质量.通过自动化的方式检查你的代码非常有用,尤其当你在一 ...

  5. Google Maps Android API v2官网例子使用说明

    2019独角兽企业重金招聘Python工程师标准>>> 1.安装Google Play services SDK Google Maps SDK已经作为Google Play ser ...

  6. 为什么Android教程中的大多数字段(类成员)都以`m`开头?

    我知道骆驼案例规则,但我对这个规则感到困惑. 它代表什么? 我是一名PHP开发人员. "我们"使用变量的第一个字母作为类型的指示,如"b"表示布尔值," ...

  7. wifi信息修改插件ios_这21款Android Studio插件,你不得不装

    作者:王菜花链接:https://juejin.im/entry/5998090ff265da248a7a6bde 最近改用 Android Studio 3.5 preview ,顺便整理一下,常用 ...

  8. android log4,GitHub - oronno/log4android: Log4Android - Simple Logging Wrapper Library for Android

    log4android Log4Android - Simple Logging Wrapper Library for Android Tired writing TAG each time wri ...

  9. Vitamio打造自己的Android万能播放器

    前言 虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,这里与大家一起利用免费的Vitamio来打造属于自己的And ...

最新文章

  1. ExecutorService 的理解与使用
  2. matlab中中图像PSNR和SSIM的计算
  3. log4j2的配置文件log4j2.xml笔记
  4. emmc linux.格式化参数,SD卡, EMMC固化 ,关于bootloader linux
  5. Trie树的常见应用大总结(面试+附代码实现)
  6. weex 安装过程中遇到的坑
  7. 强制类型转换运算符—C++基础篇
  8. 中的实践 中兴_中兴数字化转型:做“极致的云公司” 用5G制造5G
  9. HDU 3350 #define is unsafe
  10. 中文冒号vs英文冒号
  11. python程序文件默认扩展名_Python程序文件的扩展名是:
  12. Win10台式电脑怎么不拔网线断网
  13. 现场总线PROFIBUS-DP在风力发电系统中的应用(内附详细说明)
  14. ecshop模板支持php,让ecshop模板支持php运算
  15. 终于明白#!bin/sh是什么意思了
  16. 跨模态行人重识别:Modality Synergy Complement Learning withCascaded Aggregation for Visible-InfraredPerson 笔记
  17. 基于 KNN 和 人体关键点的动作分类 - Pose classification
  18. Pixel: Multi-signatures for Consensus
  19. Praat将连续录制的声音文件切成小单位文件
  20. python round_Python3 round() 函数

热门文章

  1. PHP设计模式系列 - 外观模式
  2. labview 9点标定 手眼标定
  3. tomcat与地址栏图标之研究(多浏览器)
  4. 计算机开机启动excer,我的电脑只要用excel就会自动重新启动?
  5. JSP include参数的中文乱码问题
  6. linux 映射远程文件夹,Linux远程映射磁盘的方法
  7. Java位运算(面试)
  8. java jcf查看_Java 集合系列之一:JCF集合框架概述
  9. 飞书开放平台Go-Sdk使用指南
  10. 发给美女的搞笑祝福短信