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. String和bytes的编码转换

    import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; /** * @author 作者 E-mai ...

  2. MVC3的一个意外的异常 String was not recognized as a valid Boolean. @using (Html.BeginForm())

    客户的网站放在一个虚拟空间,之间都没有修改过程序.可是网站的后台登录页面报错  String was not recognized as a valid Boolean. ,错误指向@using (H ...

  3. 【UVA10972】RevolC FaeLoN (求边双联通分量)

    题意: 给你一个无向图,要求把所有无向边改成有向边,并且添加最少的有向边,使得新的有向图强联通. 分析: 这题的解法还是很好想的.先用边双联通分量缩点,然后找新图中入度为0和为1的点,入度为0则ans ...

  4. 【HDOJ】3789 奥运排序问题

    写了个函数指针,这题目很水,但是佷烦. #include <iostream> #include <cstring> #include <cstdio> #incl ...

  5. HashMap循环遍历方式及其性能对比

    主要介绍HashMap的四种循环遍历方式,各种方式的性能测试对比,根据HashMap的源码实现分析性能结果,总结结论.   1. Map的四种遍历方式 下面只是简单介绍各种遍历示例(以HashMap为 ...

  6. 【转】Android Listener侦听的N种写法

    原文网址:http://blog.csdn.net/ithomer/article/details/7489274 Android中,View的Listener方法,在是否使用匿名类匿名对象时,有各种 ...

  7. web.xml 详解contextConfigLocation 转

    spring的应用初始化流程一直没有搞明白,刚刚又碰到了相关的问题.决定得好好看看这个流程.我们在开发spring的项目当中基本上都会在web.xml通过: <context-param> ...

  8. HDU-1035 Robot Motion

    http://acm.hdu.edu.cn/showproblem.php?pid=1035 Robot Motion Time Limit: 2000/1000 MS (Java/Others)   ...

  9. VS 2010不显示头文件源文件和所有以前分类的文件夹,*.h 和*.cpp都显示在同一个文件

    打开VS后不显示头文件源文件和所有以前分类的文件夹,*.h 和*.cpp都显示在同一个文件 点击右图红色指示显示所有文件夹按钮,就能恢复.

  10. HDOJ/HDU 2549 壮志难酬(取小数点后几位~)

    Problem Description 话说MCA山上各路豪杰均出山抗敌,去年曾在江湖威名显赫的,江湖人称<万军中取上将首级舍我其谁>的甘露也不甘示弱,"天将降大任于斯人也,必先 ...