Configuring Robolectric
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 (default
https://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的更多相关文章
- Robolectric 探索之路
layout: post title: Roboletric探索之路,从抗拒到依赖 description: Roboletric Android Unit Testing category: blo ...
- 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 ...
- Error configuring application listener of class。。。NoClassDefFoundError。。某Listener 之启动tomcat报错
当你启动tomcat的时候如果报类似下面的错误: WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to ' ...
- Android 单元测试(junit、mockito、robolectric)
1.运用JUnit4 进行单元测试 首先在工程的 src 文件夹内创建 test 和 test/java 文件夹. 打开工程的 build.gradle(Module:app)文件,添加JUnit4依 ...
- 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 ...
- [转] - 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 ...
- Android studio下gradle Robolectric单元测试配置
android studio下gradle Robolectric单元测试配置 1.Robolectric Robolectric是一个基于junit之上的单元测试框架.它并不依赖于Android提供 ...
- 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 ...
- 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...
随机推荐
- em(倍)与px的区别(转载)
在国内网站中,包括三大门户,以及"引领"中国网站设计潮流的蓝色理想,ChinaUI等都是使用了px作为字体单位.只有百度好歹做了个可调的表率.而 在大洋彼岸,几乎所有的主流站点都使 ...
- Uva_11762 Race to 1
题目链接 题意: 给一个数n, 每次从小于等于n的素数里选一个P, 如果能被n整除, 那么就n就变成n / P. 问: n 变成1的期望. 思路: 设小于等于n的素数有p 个, 其中是n的约数的有g个 ...
- 集群-Session解决方案
在集群中session安全和同步是个最大的问题,下面是收集到的几种session同步的方案,希望能通过分析其各自的优劣找出其适应的场景. 1. 客户端cookie加密 简单,高效.比较好的方法是自己采 ...
- KeilC51使用详解 (三)
C51强大功能及其高效率的重要体现之一在于其丰富的可直接调用的库函数,多使用库函数使程序代码简单,结构清晰,易于调试和维护,下面介绍C51的库函数系统. 第一节 本征库函数(intrinsic rou ...
- BlockingQueue队列学习
今天看了下BlockingQueue的几种实现,记录下以便以后复习. 首先来看一下BlockingQueue的家族成员: BlockingQueue除了先进先出外,还有两个操作:在队列为空时,获取元素 ...
- [性能分析]linux文件描述符(转)
1.什么是文件和文件描述符 Linux中文件可以分为4种:普通文件.目录文件.链接文件和设备文件.1.普通文件是用户日常使用最多的文件,包括文本文件.shell脚本.二进制的可执行和各种类型的数据.l ...
- 算法-KMP模式匹配算法
1朴素算法:逐个比较 2 主要是解决多余比较的麻烦,通过处理比较字符串是否含有重复的字符的问题.
- android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )及屏幕适配注意事
1.1 手机常见分辨率: 4:3VGA 640*480 (Video Graphics Array)QVGA 320*240 (Quarter VGA)HVGA 480*320 (Half ...
- .net framework client profile
.NET Framework Client Profile The .NET Client Profile is a subset of the .NET Framework, which was p ...
- bzoj1236
其实这道题目不难,主要要求我们有一个清晰地思路首先可以按位数讨论,这里我把1~9单独讨论了因为除了1位数,每个位数开头的数的开头数字1前面都是-号然后考虑位数的奇偶性当位数为奇数的时候比较简单举个例子 ...