There are numerous ways to customize how Robolectric behaves at runtime.

Config Annotation

The primary way to customize Robolectric is done via the @Config annotation. The annotation can be applied to classes and methods, with the values specified at the method level overriding the values specified at the class level.

//定制robolectric的主要方法是通过@Config注解,这个注解可以被使用在类上和方法上。方法上的value会重写类层的value

Base classes are also searched for annotations, so if you find yourself specifying the same values on a large number of tests, you can create a base class and move your @Config annotation to that class.

//基类也会被注解搜索,所以如果你发现你自己定义了很多同样的value在大量的测试类中,你可以创建一个基类。然后让@Config注解指向这个基类

The following examples show how to handle common setup tasks:

Configure SDK Level

Robolectric will run your code against the targetSdkVersion specified in your manifest. If you want to test how specific pieces of code behave under a different SDK level, you can change the sdk version by setting:

//robolectric运行代码是基于你声明的targetsdkversion版本。如果你想测试特定的代码片段在不懂sdk level的效果。你可以如下面方法修改sdk version:

@Config(sdk = Build.VERSION_CODES.JELLY_BEAN)
public class SandwichTest { @Config(sdk = Build.VERSION_CODES.KITKAT)
public void getSandwich_shouldReturnHamSandwich() {
}
}

Configure Application Class

Robolectric will attempt to create an instance of your Application class as specified in the manifest. If you want to provide a custom implementation, you can specify it by setting:

//robolectric尝试创建一个在manifest中指定的application实例。如果你想提供一个自定义的实现,使用下面的方法实现:

@Config(application = CustomApplication.class)
public class SandwichTest { @Config(application = CustomApplicationOverride.class)
public void getSandwich_shouldReturnHamSandwich() {
}
}

Configure Resource Paths

Robolectric provides defaults for Gradle and Maven, but allows you to customize the path to your manifest, resource directory, and assets directory. This can be useful if you have a custom build system. You can specify these values by setting:

//rebolectric支持自定义manifest,resource目录,assets目录等默认路径

@Config(manifest = "some/build/path/AndroidManifest.xml")
public class SandwichTest { @Config(manifest = "other/build/path/AndroidManifest.xml")
public void getSandwich_shouldReturnHamSandwich() {
}
}

By default, Robolectric will assume that your resources and assets are located in directories named res and assets, respectively. These paths are assumed to be relative to the directory where the manifest is located. You can change these values by adding theresourceDir and assetDir options to the @Config annotaton.

//使用resourceDir and assetDir选项来配置目录

Config Properties

Any option that can be specified in a @Config annotation can also be specified globally in a properties file. Create a file namedrobolectric.properties and make sure it can be found on the classpath. Below is an example:

//可以创建一个robolectric.properties文件来指定全局配置的变量,如下例子:

sdk=18
manifest=some/build/path/AndroidManifest.xml
shadows=my.package.ShadowFoo,my.package.ShadowBar

System Properties

Some options can be configured globally by setting these system properties:

  • robolectric.offline - Set to true to disable runtime fetching of jars.
  • robolectric.dependency.dir - When in offline mode, specifies a folder containing runtime dependencies.
  • robolectric.dependency.repo.id - Set the ID of the Maven repository to use for the runtime dependencies (default sonatype).
  • robolectric.dependency.repo.url - Set the URL of the Maven repository to use for the runtime dependencies (defaulthttps://oss.sonatype.org/content/groups/public/).
  • robolectric.logging.enabled - Set to true to enable debug logging.

When using Gradle, you can configure the System Properties for unit tests with the all block (see here). For example, to override the Maven repository URL and ID to download the runtime dependencies from a repository other than Sonatype:

android {
testOptions {
unitTests.all {
systemProperty 'robolectric.dependency.repo.url', 'https://local-mirror/repo'
systemProperty 'robolectric.dependency.repo.id', 'local'
}
}
}

Configuring Robolectric的更多相关文章

  1. Robolectric 探索之路

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

  2. Configuring Autofac to work with the ASP.NET Identity Framework in MVC 5

    https://developingsoftware.com/configuring-autofac-to-work-with-the-aspnet-identity-framework-in-mvc ...

  3. Error configuring application listener of class。。。NoClassDefFoundError。。某Listener 之启动tomcat报错

    当你启动tomcat的时候如果报类似下面的错误: WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to ' ...

  4. Android 单元测试(junit、mockito、robolectric)

    1.运用JUnit4 进行单元测试 首先在工程的 src 文件夹内创建 test 和 test/java 文件夹. 打开工程的 build.gradle(Module:app)文件,添加JUnit4依 ...

  5. Configuring Network in CentOS 6.3 Virtual Box + Screenshots

    Configuring Network in CentOS 6.3 Virtual Box + Screenshots Posted: May 23, 2013 in Uncategorized Ta ...

  6. [转] - Configuring Android Studio: IDE & VM Options, JDK, etc

    Configuring Android Studio: IDE & VM Options, JDK, etc You should not edit any files in the IDE ...

  7. Android studio下gradle Robolectric单元测试配置

    android studio下gradle Robolectric单元测试配置 1.Robolectric Robolectric是一个基于junit之上的单元测试框架.它并不依赖于Android提供 ...

  8. hive脚本出现Error: java.lang.RuntimeException: Error in configuring object和Caused by: java.lang.IndexOutOfBoundsException: Index: 9, Size: 9

    是在reduce阶段报的错误,详细错误信息是 朱传豪 19:04:48 Diagnostic Messages for this Task: Error: java.lang.RuntimeExcep ...

  9. 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener

    严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...

随机推荐

  1. maven web项目不能创建src/main/java等文件夹的问题

    eclipse创建maevn web项目,在选择maven_archetype_webapp原型后,默认只有src/main/resources这个Source Floder.  按照maven目录结 ...

  2. Android Learning:多线程与异步消息处理机制

    在最近学习Android项目源码的过程中,遇到了很多多线程以及异步消息处理的机制.由于之前对这块的知识只是浅尝辄止,并没有系统的理解.但是工程中反复出现让我意识到这个知识的重要性.所以我整理出这篇博客 ...

  3. iPhone手机VPN设置

    如果iPhone,iPad游戏或软件服务器在国外不能用,就需要设置VPN了. 如果是为了解除公司上网策略限制,或者为了上Google,Facebook,都可以通过设置VPN实现. 要使用VPN需要到V ...

  4. 【POJ1470】Closest Common Ancestors

    Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For e ...

  5. 重载VerifyRenderingInServerForm

    重载VerifyRenderingInServerForm 今天在做一个AjaxPro的应用时,VerifyRenderingInServerForm给我带来了麻烦,在网上找了找,发现大多数人的解决方 ...

  6. 在XP系统中自带的 msvcrt.dll 和 Vista 中的 msvcrt.dll 版本不同,导致抛出的异常不同

    然而,在XP系统中,系统自带的 msvcrt.dll 和 Vista 中的 msvcrt.dll 版本不同, 并没有这个 _except_handler4_common ,结果就出现了启动程序时,遇到 ...

  7. 关于下拉刷新你是否真的非常理解还是只会搬砖?附 Android 实例子源代码文件下载地址380个合集

    1,推荐几篇非常有用的博文 原创写的真的非常好 主要讲解原理,整体布局三部分组成以及设置padding等等作用, 下拉的具体实现 滑动到底部具体加载以及判断手势事件,再次推荐作者的 详细讲解 建议先看 ...

  8. Unity-layermask的问题

    using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { priva ...

  9. 折腾iPhone的生活——iOS设备重刷固件

    iOS设备升级系统总共有这么几种方法: 1.OTA升级,也就是我们最常碰到的,在设备上,连上Wifi,在设置里面的软件更新就可以直接通过Wifi安装新的系统(已越狱设备不要这样升级) 2.通过iTun ...

  10. Installing scikit-learn

    Installing scikit-learn http://scikit-learn.org/stable/install.html Installing scikit-learn There ar ...