There are several situations where you want to customize Robolectric's test runner to perform some operation before all tests are run, or even before each test method is run. One good example is initializing a dependency injection framework with a different set of dependencies for your test. Fortunately, Robolectric has a way to hook into the test lifecycle. If you define an Application class in your AndroidManifest.xml, Robolectric will automatically try and load a test version of your application class first. For example:

Let's say you've defined a FooApplication in your manifest:

<application android:name=".FooApplication">

If you're using RoboGuice, you would initialize the injector in your Application class:

public class FooApplication extends Application {
@Override
public void onCreate() {
super.onCreate(); ApplicationModule module = new ApplicationModule();
setBaseApplicationInjector(this, DEFAULT_STAGE, newDefaultRoboModule(this), module);
}
}

You can define a test version of the application named TestFooApplication:

public class TestFooApplication extends FooApplication implements TestLifecycleApplication {
@Override
public void onCreate() {
super.onCreate(); TestApplicationModule module = new TestApplicationModule();
setBaseApplicationInjector(this, DEFAULT_STAGE, newDefaultRoboModule(this), module);
} @Override
public void beforeTest(Method method) {
} @Override
public void prepareTest(Object test) {
getInjector(this).injectMembers(test);
} @Override
public void afterTest(Method method) {
}
}

Robolectric will load the test version of the application which you can use to load a different set of bindings during tests.

Customizing the Test Runner的更多相关文章

  1. svn: E200007: Runner for 'org.tmatesoft.svn.core.wc2.SvnMerge' command have not been found; probably not yet implement in this API.

    myeclipse分支合并主干(分支->team->合并->选择主干)的时候出现这个错误: svn: E200007: Runner for 'org.tmatesoft.svn.c ...

  2. 将 xunit.runner.dnx 的 xml 输出转换为 Nunit 格式

    由于目前 DNX 缺乏 XSLT 的转换能力,因此只能使用变通方法.具体参考这个链接 主要内容复制过来是: From @eriklarko on July 14, 2015 7:38 As a wor ...

  3. postman使用之五:Runner的使用

    1.首先在postman新建要批量运行的接口文件夹,新建一个接口,并设置好全局变量. 2.然后在Test里面设置好要断言的方法 如: tests["Status code is 200&qu ...

  4. 利用docker compose启动gitlab及runner

    添加docker compose配置文件 新建文件docker-compose.yml,输入如下内容: gitlab: image: 'gitlab/gitlab-ce:latest' contain ...

  5. Task Runner Explorer for vs2015找不到啊

    https://visualstudiogallery.msdn.microsoft.com/8e1b4368-4afb-467a-bc13-9650572db708/view/ 编译typescri ...

  6. karma作为jQuery单元测试Runner

    karma作为angular测试runner出现,如果你使用过karma一定感受到这很不错的javascript测试runner.简单干净的配置文件karma.config.js,以及karma in ...

  7. exception throw in progress runner thread_VS2015中SVN源代码无说明提交异常

    1-问题描述:通过SVN将代码变更提交服务器时,你可能会遇到以下异常:  exception throw in progress runner thread 2-解决办法: SVN代码源代码管理和TF ...

  8. SaltStack之Job管理和Runner(八)

    SaltStack之Job管理和Runner 配置文件/etc/salt/master cachedir: /var/cache/salt/master # cache路径 keep_jobs: 24 ...

  9. Customizing Navigation Bar and Status Bar

    Like many of you, I have been very busy upgrading my apps to make them fit for iOS 7. The latest ver ...

随机推荐

  1. iOS 仪表式数字跳动动画-b

    前几天搞了 双曲线波浪动画(http://www.jianshu.com/p/7db295fd38eb)和环形倒计时动画(http://www.jianshu.com/p/d1d16dff33c9)而 ...

  2. Uva_11427 Expect the Expected

    题目链接 题意: 你玩纸牌, 如果当天晚上你赢的局数比例 大于 p, 就去睡觉, 第二天继续. 如果小于等于p, 就去睡觉, 并且以后都不玩了. 每晚最多玩n局, 每局赢的概率为p , 求玩的天数的期 ...

  3. python获取本地ip地址的方法

    #_*_coding:utf8_*_ #以下两种方法可以在ubuntu下或者windows下获得本地的IP地址 import socket # 方法一 localIP = socket.gethost ...

  4. javascript中神奇的(+)加操作符

    javascript是一门神奇的语言,这没神奇的语言中有一个神奇的加操作符. 常用的加操作符我们可以用来做: 加法运算,例如:alert(1+2); ==>3 字符串连接,例如:alert(“a ...

  5. android 通过eclipse混淆代码 打包 + proguard 总结

    android应用程序的混淆打包 1 . 在工程文件project.properties中加入下proguard.config=proguard.cfg , 如下所示: target=android- ...

  6. -_-#【HTML】同一个标签页打开

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. redis3.0集群搭建

    生产环境中准备使用redis3.0集群了,花了一天时间研究了一下,下面记录一下集群搭建的过程. 服务器规划: 192.168.116.129    7000,7003 192.168.116.130 ...

  8. apple ID的重要性

    当手机丢失时,您可以将对应的产品码提供给运营商,提高找回丢失手机的可能性.         2.去App store下载“查找我的iPhone(Find My iPhone)”,立刻安上,用你的app ...

  9. Linux 查看磁盘分区、文件系统、使用情况的命令和相关工具介绍

    磁盘分区表.文件系统的查看.统计的工具很多,有些工具是多功能的,不仅仅是查看磁盘的分区表,而且也能进行磁盘分区的操作:但在本文,我们只讲磁盘分区的查看,以及分区的使用情况的查看:本文只是给新手上路之用 ...

  10. Unity3d shader之次表面散射(Subsurface Scattering)

    次表面散射是一种非常常用的效果,可以用在很多材质上如皮肤,牛奶,奶油奶酪,番茄酱,土豆等等  初衷是想做一个牛奶shader的,但后来就干脆研究了sss这是在vray上的次表面散射效果 这是本文在un ...