Custom shadows are a Robolectric feature that allows you to make targeted changes in the way Android functions under test. This could be anything from capturing simply that a method was called, to inserting code that interacts with test objects, to doing nothing at all.

Custom shadows allow you to include shadow functionality in only some of your test code, as opposed to adding or modifying a Shadow in Robolectric source. They also allow your shadow to refer to domain specific context, like domain objects in your test classes.

Writing a Custom Shadow

Custom shadows are structured much the same as normal shadow classes. They must include the@Implements(AndroidClassName.class) annotation on the class definition. You can use the normal shadow implementation options, such as shadowing instance methods using @Implementation or shadowing constructors usingpublic void __constructor__(...).

@Implements(Bitmap.class)
public class MyShadowBitmap {
@RealObject private Bitmap realBitmap;
private int bitmapQuality = -1; @Implementation
public boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) {
bitmapQuality = quality;
return realBitmap.compress(format, quality, stream);
} public int getQuality() {
return bitmapQuality;
}
}
}

Using a Custom Shadow

Custom Shadows get hooked up to Robolectric using the @Config annotation on the test class or test method, using the shadowsarray attribute. To use the MyShadowBitmap class mentioned in the previous section, you would annotate the test in question with@Config(shadows={MyShadowBitmap.class}), and to include multiple custom shadows:@Config(shadows={MyShadowBitmap.class, MyOtherCustomShadow.class}). This causes Robolectric to recognize and use your custom shadow when executing code against the class you shadowed.

However, the Robolectric.shadowOf() method will not work with custom shadows, as it has to be implemented in Robolectric for each shadow class. You can instead use Robolectric.shadowOf_() and cast the return value to the custom Shadow class you implemented.

Also, if you choose to shadow an Android class that already is shadowed in Robolectric, you will replace the Robolectric shadow. You could try inheriting from the Robolectric shadow if you still need the base shadow functionality.

Creating Custom Shadows ——创建自定义shadow的更多相关文章

  1. 使用 custom element 创建自定义元素

    很早我们就可以在 HTML 文档中写 <custome-element></custom-element> 这样的自定义名称标签.但是浏览器对于不认识的标签一律当成一个普通的行 ...

  2. Collection View Programming Guide for iOS---(六)---Creating Custom Layouts

    Creating Custom Layouts 创建自定义布局 Before you start building custom layouts, consider whether doing so ...

  3. GHOST CMS - 创建自定义主页 Creating a custom home page

    创建自定义主页 Creating a custom home page 为你的网站创建一个自定义的主页是一个让你从人群中脱颖而出的好方法,并把你自己独特的印记存放在你的网上.本教程向您展示了如何在Gh ...

  4. ArcGIS Engine环境下创建自定义的ArcToolbox Geoprocessing工具

    在上一篇日志中介绍了自己通过几何的方法合并断开的线要素的ArcGIS插件式的应用程序.但是后来考虑到插件式的程序的配置和使用比较繁琐,也没有比较好的错误处理机制,于是我就把之前的程序封装成一个类似于A ...

  5. 【IOS笔记】Creating Custom Content View Controllers

    Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...

  6. 【翻译】在Ext JS和Sencha Touch中创建自定义布局

    原文:Creating Custom Layouts in Ext JS and Sencha Touch 布局系统是Sencha框架中最强大和最独特的一部分.布局会处理应用程序中每个组件的大小和位置 ...

  7. 【UiPath 中文教程】02 - 创建自定义 Activity

    在 UiPath Studio 中,活动 (Activity) 是流程自动化的基石,是构成自动化程序的最小模块.它们被包含在一个个 NuGet 包中. UiPath Studio 中有 3 类包: 官 ...

  8. View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers

    Creating Custom Content View Controllers 创建自定义内容视图控制器 Custom content view controllers are the heart ...

  9. 在html中创建自定义标签

    创建并使用自定义标签 Web Components 标准非常重要的一个特性是,它使开发者能够将HTML页面的功能封装为 custom elements(自定义标签),本篇介绍使用 CustomElem ...

随机推荐

  1. 《深入剖析Tomcat》阅读(一)

    第一章 一个简单的Web服务器 该应用程序仅接受位于指定目录的静态资源的请求,如HTML文件和图像文件.它也可以将传入的HTTP请求字节流显示到控制台上.但是,它并不发送任何头信息到浏览器,如日期或者 ...

  2. 一个消除if语句的例子

    // 一个按钮点击事件,判断点击按钮是那一个显示出他的信息 - (IBAction)buttonPressed:(id)sender { if (sender == self.leftButton) ...

  3. sleep与wait的区别,详细解答(通过代码验证)

    package com.ysq.test; /** * sleep与wait的区别: * @author ysq * */ public class SleepAndWait { public sta ...

  4. Spring MVC和Struts2的区别

    1. 机制:spring mvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同. 2. 性能:spring会稍微比struts快.spring mvc是基于方法的设 ...

  5. Spring MVC 解读——@RequestMapping (1)(转)

    转自:http://my.oschina.net/HeliosFly/blog/212329 Spring MVC 解读——@RequestMapping 为了降低文章篇幅,使得文章更目标化,简洁化, ...

  6. Android ServiceConnection类的onServiceDisconnected(ComponentName name)在什么时候执行

    ServiceConnection类中的两个方法非别在服务连接成功时.不成功时调用.其中onServiceDisconnected()方法在连接正常关闭的情况下是不会被调用的, 该方法只在Servic ...

  7. Linux Shell编程(13)——数字常量

    除非一个数字有特别的前缀或符号,否则shell脚本把它当成十进制的数.一个前缀为0的数字是八进制数.一个前缀为0x的数字是十六进制数.一个数用内嵌的#来求值则看成BASE#NUMBER(有范围和符号限 ...

  8. 【转】Xcode 7 真机调试详细步骤

    原文网址:http://www.jianshu.com/p/fa5f90b61ad6 文/ldjhust(简书作者)原文链接:http://www.jianshu.com/p/fa5f90b61ad6 ...

  9. apply和call详解

    1.        apply和call的区别在哪里 2.        什么情况下用apply,什么情况下用call 3.        apply的其他巧妙用法(一般在什么情况下可以使用apply ...

  10. 公有云开启全面竞争时代——青云QingCloud

    网界网本期[封面报道]公有云开启全面竞争时代 [CNW.com.cn 专稿] 险滩急流之后,公有云[注]服务市场的开拓者们终于看到了一片丰饶之海. 2013年,中国云计算[注]产业起步的第五年,公有云 ...