首页 技术 正文
技术 2022年11月10日
0 收藏 953 点赞 3,637 浏览 3541 个字

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'
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290