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 ...
随机推荐
- 解决delphi使用sqlite时中文最后一个字是乱码的问题
给部门同事写了个自动生成测试用例执行情况图表的工具,使用的sqlite3,遇到中文分别使用UTF8Encode和UTF8Decode进行处理,可还是出现了某些字段从数据库中读出时,最后一个汉字是乱码的 ...
- Stanford Parser学习入门(1)-Eclipse中配置
Stanford Parser是斯坦福大学研发的用于语法分析的工具,属于stanford nlp系列工具之一.本文主要介绍Standfor Parser的入门用法. 在Stanford官方网站下载最新 ...
- 用Robotium 去实现点击imageview
今天用rototium做自动化遇到imageview无法点击的问题,最终解决如下: 有两种方法: 1.View v = solo.getView(R.id.iv_main_setting); ...
- 大型系统OA--需求
1.面向普通用户 普通用户一般都是公司的小职员,上OA都干什么呢?当然是获取信息咯.还有最好很多事儿,不需要跟公司的其他部门的人儿,打交道,直接在网上解决就好了. 收发邮件: 查看自己发动的流程,也就 ...
- 【转】实现展开列ExpandableListView的三种方式之SimpleExpandableListAdapter实例
原文网址:http://blog.csdn.net/x605940745/article/details/12099709 实现可扩展展开列ExpandableListView的三种方式 欢迎加入QQ ...
- Delphi 6 Web Services初步评估之二(转)
Delphi 6 Web Services初步评估之二(转) ★ 测试环境:CPU:PIII 550内存: 256MBOS: Windows2000 Server + SP2Web Server: ...
- Python IDLE 清屏工具
转载自:http://www.cnblogs.com/maybego/p/3234055.html 1.下载clearwindow.py(右击-目标另存为,直接点击会打开脚本内容). 2.拷贝c ...
- 使用AppDelegate单例,解决子视图无法给父视图发送消息的问题
关于单例模式,我会在实验过后再开一个博客重点讲单例的使用,这里只是介绍我在PhotoForBingyan的照片滤镜的项目中使用AppDelegate单例的情况. 碰到的问题: 由于这个项目是个多视图的 ...
- AQL Subset Compiler:手把手教你如何写一个完整的编译器
项目地址:https://github.com/laiy/Awesome-Complier. 转载请注明出处. 前言 这是学校里编译原理课程的大作业,此Project十分适合编译原理的学习,让基本不听 ...
- 小情人emacs的自动补全
前天打字的时候发现手指疼-..OTL-思考了一下可能是我近几个月以来一直在使用全部手敲代码不使用自动补全的"恶果"(当然我还是建议全部手敲的,至少可以感觉到强烈的屌丝满足感). 先 ...