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. Elasticsearch客户端大全 http://www.searchtech.pro/elasticsearch-clients

    Elasticsearch有各种语言的客户端,下面一一列出来: Perl ElasticSearch.pm: Perl客户端 Python pyes: Python客户端pyelasticsearch ...

  2. hello,world不使用ARC

    main.m // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. ...

  3. 转:etcd:从应用场景到实现原理的全方位解读

    原文来自于:http://www.infoq.com/cn/articles/etcd-interpretation-application-scenario-implement-principle ...

  4. http server v0.1_mime.c

    #include <string.h> #include "mime.h" static STR_MIME_MAP mime_map[]= { MIME_MAP(MIM ...

  5. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 动态树

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3119  Solved: 1399[Submit] ...

  6. BZOJ 2226 LCMSum

    Description Given \(n\), calculate the sum \(LCM(1,n) + LCM(2,n) + \cdots + LCM(n,n)\), where \(LCM( ...

  7. [BZOJ 1053] [HAOI 2007] 反素数ant

    题目链接:BZOJ 1053 想一想就会发现,题目让求的 1 到 n 中最大的反素数,其实就是 1 到 n 中因数个数最多的数.(当有多于一个的数的因数个数都为最大值时,取最小的一个) 考虑:对于一个 ...

  8. Milk Patterns

    poj3261:http://poj.org/problem?id=3261 题意:给定一个字符串,求至少出现k 次的最长重复子串,这k 个子串可以重叠. 题解:还是用后缀数组,求H和后缀数组,然后二 ...

  9. 李洪强漫谈iOS开发[C语言-033]-三元运算符的应用

  10. astyle代码格式化

    Artistic Style 1.24 A Free, Fast and Small Automatic Formatterfor C, C++, C#, and Java Source Code 项 ...