【十】注入框架RoboGuice使用:(Your First Testcase)
上一篇我们简单的介绍了一下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)的更多相关文章
- 【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)
上一篇我们简单的介绍了一下RoboGuice的使用([十]注入框架RoboGuice使用:(Your First Testcase)),今天我们来看下自己定义View的注入(Custom View). ...
- 【十二】注入框架RoboGuice使用:(Your First Injected ContentProvider)
上一篇我们简单的介绍了一下RoboGuice的使用([十一]注入框架RoboGuice使用:(Your First Injection into a Custom View class)),今天我们来 ...
- 【十三】注入框架RoboGuice采用:(Logging via Ln)
上一篇我们简单的介绍了一下RoboGuice的使用([十二]注入框架RoboGuice使用:(Your First Injected ContentProvider)),今天我们来看下Log日志使用. ...
- 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)
上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受 ...
- 【四】注入框架RoboGuice使用:(Your First System Service Injection)
上一篇我们简单的介绍了一下RoboGuice的使用([三]注入框架RoboGuice使用:(Your First Resource Injection)),今天我们来看下系统服务的使用注解的方法: 为 ...
- 【七】注入框架RoboGuice使用:(Your First Custom Binding)
上一篇我们简单的介绍了一下RoboGuice的使用([六]注入框架RoboGuice使用:(Singletons And ContextSingletons)),今天我们来看下自己定义绑定(bindi ...
- 【三】注入框架RoboGuice使用:(Your First Resource Injection)
上一篇我们简单的介绍了一下RoboGuice的使用([二]注入框架RoboGuice使用:(Your First View Injection)),今天我们来看下资源文件的使用注解的方法: 为了在Ac ...
- 【八】注入框架RoboGuice使用:(Your First Injected Fragment)
上一篇我们简单的介绍了一下RoboGuice的使用([七]注入框架RoboGuice使用:(Your First Custom Binding)),今天我们来看下fragment的注解 ...
- 【六】注入框架RoboGuice使用:(Singletons And ContextSingletons)
上一篇我们简单的介绍了一下RoboGuice的使用([五]注入框架RoboGuice使用:(Your First POJO Injection)),今天我们来看下单例以及上下文单例(ContextSi ...
随机推荐
- java与.net比较学习系列(6) 数组
这一篇文章要总结的内容是数组,数组可以简单地看成是同种数据类型变量的集合,它在我们的开发中经常用到,我们主要从以下几个方面进行总结: 1,数组的创建和初始化 2,数组的访问和遍历 3,数组的总结 数组 ...
- C语言获取系统当前时间转化成时间字符串
因为保存的文件须要加上保存的时间,所以须要一个函数来将系统当前时间获取出来,同一时候转换成时间字符串.详细的时间代码例如以下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- nginx配置文件(反向代理+集群+动静分离)
1.nginx纯反向代理配置(nginx.conf): #user nobody;worker_processes 4;error_log logs/error.log info;pid logs/n ...
- iOS 面试题 3
0.请写出代码,用blocks来取代上例中的protocol,并比较两种方法的优势.实际应用部分?请写出代码,用blocks取代协议或回调方法 声明: #import <Foundation/F ...
- Django 实战 之 搭项目(正在更新)
系统:win10 python版本:python 3.5 工具: pyCharm 3.4 professional 源码来源:https://github.com/ouzhigang/django-o ...
- poj1111 DFS
J - 搜索 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit I ...
- Python codes
Vertification of an assignment from Stochastic Processing. Using Brute Force and "itertools&quo ...
- (原)ubuntu16中安装moses
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5653186.html 在ubuntu14中,可以使用下面的语句安装moses: luarocks in ...
- 何时可以开启透明数据加密(TDE)?
TDE可以为我们的数据库提供加密保护,但是,不是任何情况下都可以随意开启TDE的,同时开启TDE后,我们的数据库维护管理工作也需要进行一些调整. 下面我们就先看看开启TDE需要的条件吧! 无法正常开启 ...
- 阿里巴巴JAVA常考面试题及汇总答案
一.String,StringBuffer, StringBuilder 的区别是什么?String为什么是不可变的? 答: 1.String是字符串常量,StringBuffer和StringB ...