上一篇我们简单的介绍了一下RoboGuice的使用(【九】注入框架RoboGuice使用:(Your
First Injected Service and BroadcastReceiver)
),今天我们来看下測试用例(TestCase)的注入

RoboGuice使得我们更加easy实现可測试的Android应用程序,本文章就来具体讲解下:当我们測试的时候,怎样编写測试用例,已经从RoboGuice中获益。本文章使用Android Robolectric,适合大部分用Android标准測试的情况。

我们用Mockito来模拟关系依赖,EasyMock使用同一种方式。

(一):来进行測试RoboActivity的子类,如果如今有一个使用Service 的Activity

public class MyRoboActivity extends RoboActivity {
@Inject MyService service; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
service.compute();
}
} public class MyService {
public void compute() {
...
}
}

然后须要编写測试用例来检查Activity是否正确调用了servce。

@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
private Service serviceMock = mock(Service.class); @Before
public void setup() {
// Override the default RoboGuice module
RoboGuice.overrideApplicationInjector(Robolectric.application, new MyTestModule());
} @After
public void teardown() {
// Don't forget to tear down our custom injector to avoid polluting other test classes
RoboGuice.Util.reset();
} @Test
public void createTriggersCompute() throws InterruptedException {
Robolectric.buildActivity(MyActivity.class).create().start();
verify(serviceMock).compute();
} public class MyTestModule extends AbstractModule {
@Override
protected void configure() {
bind(Service.class).toInstance(serviceMock);
}
}
}

该測试下面几项:

①:在设置的时候,会覆盖RoboGuice默认绑定而且使用一个自己定义的測试模块

②:这个測试模块绑定Service到mock上面

③:该測试创建一个MyActivity的实例,该会通过注入(injection)获取mock

      ④:验证mock应该被调用.

(二):測试一个服务,如果有个service例如以下:

public class MyService {
@Inject Vibrator vibrator;
@Inject Context context; public void compute() {
context...
vibrator.vibrate(...);
}
}

然后我们能够编写一个简单的測试用来检測service是否正确调用了vibrator

@RunWith(RobolectricTestRunner.class)
public class ServiceTest {
protected Vibrator vibratorMock = mock(Vibrator.class);
protected Service service; @Before
public void setup() {
// Override the default RoboGuice module
RoboGuice.overrideApplicationInjector(Robolectric.application, new MyTestModule());
service = RoboGuice.getInjector(Robolectric.application).getInstance(Service.class);
} @After
public void teardown() {
// Don't forget to tear down our custom injector to avoid polluting other test classes
RoboGuice.Util.reset();
} @Test
public void computeShouldCausePhoneToVibrate() {
service.compute();
verify(vibratorMock).vibrate(...);
} public class MyTestModule extends AbstractModule {
@Override
protected void configure() {
bind(Vibrator.class).toInstance(vibratorMock);
}
}
}

该測试下面几项:

①:在设置的时候,会覆盖RoboGuice默认绑定而且使用一个自己定义的測试模块

      ②:该測试模块绑定Vibrator到mock中

③:该測试模块会创建service的实例,通过注入会获取上下文以及mock

④:验证mock已经被调用了

【十】注入框架RoboGuice使用:(Your First Testcase)的更多相关文章

  1. 【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)

    上一篇我们简单的介绍了一下RoboGuice的使用([十]注入框架RoboGuice使用:(Your First Testcase)),今天我们来看下自己定义View的注入(Custom View). ...

  2. 【十二】注入框架RoboGuice使用:(Your First Injected ContentProvider)

    上一篇我们简单的介绍了一下RoboGuice的使用([十一]注入框架RoboGuice使用:(Your First Injection into a Custom View class)),今天我们来 ...

  3. 【十三】注入框架RoboGuice采用:(Logging via Ln)

    上一篇我们简单的介绍了一下RoboGuice的使用([十二]注入框架RoboGuice使用:(Your First Injected ContentProvider)),今天我们来看下Log日志使用. ...

  4. 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)

    上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受 ...

  5. 【四】注入框架RoboGuice使用:(Your First System Service Injection)

    上一篇我们简单的介绍了一下RoboGuice的使用([三]注入框架RoboGuice使用:(Your First Resource Injection)),今天我们来看下系统服务的使用注解的方法: 为 ...

  6. 【七】注入框架RoboGuice使用:(Your First Custom Binding)

    上一篇我们简单的介绍了一下RoboGuice的使用([六]注入框架RoboGuice使用:(Singletons And ContextSingletons)),今天我们来看下自己定义绑定(bindi ...

  7. 【三】注入框架RoboGuice使用:(Your First Resource Injection)

    上一篇我们简单的介绍了一下RoboGuice的使用([二]注入框架RoboGuice使用:(Your First View Injection)),今天我们来看下资源文件的使用注解的方法: 为了在Ac ...

  8. 【八】注入框架RoboGuice使用:(Your First Injected Fragment)

        上一篇我们简单的介绍了一下RoboGuice的使用([七]注入框架RoboGuice使用:(Your First Custom Binding)),今天我们来看下fragment的注解     ...

  9. 【六】注入框架RoboGuice使用:(Singletons And ContextSingletons)

    上一篇我们简单的介绍了一下RoboGuice的使用([五]注入框架RoboGuice使用:(Your First POJO Injection)),今天我们来看下单例以及上下文单例(ContextSi ...

随机推荐

  1. 【软件技巧】Sublime Text为不同语法定义不同高亮

    Sublime Text默认的语法高亮已经非常美丽了,可是对于个别语言还是有些不爽. 默认高亮规则叫Monokai,能够从Preferences->Settings - Default中看到: ...

  2. linux在文件打包和压缩

    1. 打包和压缩文件 linux现在经常使用gzip和bzip2要压缩的文件.tar压缩文件. 经常使用的扩展: *.gz   gzip压缩文件 *.bz2  bzip2压缩的文件 *.tar   t ...

  3. Flashback Query(函数示例)

    Flashback Query 函数,存储过程,包,触发器等对象Flashback Drop 可以闪回与表相关联的对象, 如果是其他的对象,比如function,procedure,trigger等. ...

  4. iOS 之URL schemes

    添加 URL  schemes 步骤: 1.打开info.plist文件. 2.点击 “+ ”号添加,或者在列表上点击鼠标右键,选择 Add Row. 3.选择 URL types. 4.点击三角号展 ...

  5. UIView的常用方法

    bringSubviewToFront: 把指定的子视图移动到顶层 - (void)bringSubviewToFront:(UIView *)view 参数 view 需要移到顶层的视图 conve ...

  6. javascript版1024游戏源码

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. 嵌入式Linux LED小灯点亮实验

    问:怎么写LED驱动程序? 1.搭建一个字符驱动的框架(上一节已经完成) 2.完善硬件的操作 问:驱动里操作硬件寄存器与单片机操作硬件寄存器有什么不一样的地方? 答:单片机操作的寄存器地址是物理地址, ...

  8. linux下python3连接mysql数据库

    python语言的3.x完全不向前兼容,导致我们在python2.x中可以正常使用的库,到了python3就用不了了.比如说mysqldb 1.安装pymysql pymysql就是作为python3 ...

  9. [Head First Python]6. summary

    1- 字典-内置数据结构,数据值与键值关联 键-字典中查找部分 值-字典中数据部分 使用dict()工厂函数或者只用{}可以创建一个空字典 >>> list = {} >> ...

  10. 异常处理与调试5 - 零基础入门学习Delphi54

    调试(Debug) 让编程改变世界 Change the world by program [caption id="attachment_2731" align="al ...