上一篇我们简单的介绍了一下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. 百度之星B题(组合数)

    Problem B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  2. 年底小回顾(MVC+NHibernate+Jquery+JqueryUI——网站)

    1.附:利用MVC+NHibernate+Jquery+JqueryUI这些技术可以做出一个比较好的前台+后台网站.下面是本人对这些技术的笔记,作为私人年底小结吧.呵呵 好久没写文章了,感觉下不了笔吐 ...

  3. poj1330Nearest Common Ancestors(LCA小结)

    题目请戳这里 题目大意:意如其名. 题目分析:本题只有一个查询,所以可以各种乱搞过去. 不过对于菜鸟而言,还是老老实实练习一下LCA算法. LCA有很多经典的算法.按工作方式分在线和离线2种. tar ...

  4. Linux curses库使用

     相信您在网路上一定用过如 tin,elm 等工具, 这些软体有项共同的特色, 即他们能利用上下左右等方向键来控制游标的位置. 除此之外, 这些程式的画面也较为美观. 对Programming 有兴趣 ...

  5. Qt快速入门系列教程目录

    Qt快速入门系列教程目录

  6. java.util.concurrent.ExecutionException

    java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start com ...

  7. Hacker(14)----扫描目标计算机端口

    端口是目前计算机与外界的通道,因而黑客一旦锁定目标计算机,便会扫描该计算机中已经开放的端口,从而得到更多的有用信息.扫描目标计算机端口一般使用SuperScan.X-Scan等. 一.端口扫描原理 扫 ...

  8. 表单元素-select

    <form> <select size="2"> <option value="JMS HADEN">JMS HADEN&l ...

  9. 经典SQL语句大全(转载)

    原文http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html#top 一.基础 1.说明:创建数据库CREATE DATABAS ...

  10. C# List<T>的详细用法

    所属命名空间:System.Collections.Generic List<T>类是 ArrayList 类的泛型等效类.该类使用大小可按需动态增加的数组实现 IList<T> ...