年度归档: 2016 年
使用 Mockito 单元测试
目录
2. 使用 存根(Stub) 和 模拟对象(Mock Object) 进行测试
1.需求知识
该教程需要理解单元测试和熟悉JUnit框架的使用。
如果您不熟悉JUnit,请阅读JUnit教程。
2. 使用 存根(Stub) 和 模拟对象(Mock Object) 进行测试
2.1. 为什么需要模拟?
一个单元测试需要在隔离的环境下执行。如果可以的话需要消除其他依赖的服务影响。但实际上,软件中是充满依赖关系的.我们会基于service类写操作类,而service类又是基于数据访问类(DAOs)的,依次下去.
为了解决这个问题, 可以使用 存根 (Stub) 或者 模拟 (Mock) 对象的方法进行测试。
2.2. 存根(Stub) vs. 模拟对象 (Mock)
存根(Stub)类是实现了一个接口或者抽象类的类,可以在测试过程中使用该类,例如:
一个模拟对象(mock object)是一个接口或者抽象类的虚拟实现。例如:
存根和模拟对象都可以传递给其他的对象进行测试。你的一些单元测试可以测这些类的正确性等。利用存根对象或者模拟对象可以保证测试过程中不受到其他的影响。
存根对象需要自定义实现方法;
模拟对象只需要更少的代码和简单的配置。
以下的内容将详细介绍模拟对象的使用方法。
2.3. 行为测试 vs. 状态测试
Mock 对象允许你对行为进行测试。有一些测试不需要验证结果,但是需要检查某些方法是否被正确的参数调用过。这种测试为行为测试。
状态测试只是关注与结果是否正确,而行为测试能够判断一个应用调用结构以及层次。
2.4. 生成模拟对象
你们可以使用Mock 框架来生成模拟对象。Mock 框架允许你在运行期间创建对象,并且定义它的一些行为。
一个典型的例子就是使用模拟对象来模拟数据库DAO层。在生产环境上是使用运行的数据库,但是在单元测试环境中完全可以用模拟对象来模拟数据,确保单元测试的正确条件。这样就不需要依赖于外部的数据。
3. 模拟框架( Mock Framework)
比较流行的模拟框架有 EasyMock、jMock 和 Mockito。下面的列表是这些框架的链接。
# jMock
http://jmock.org/
# EasyMock
http://easymock.org/
# Mockito
http://mockito.org/
4. Mockito
4.1. 使用 Mockito 模拟对象
Mockito 是比较流行的模拟框架,可以与JUnit 联合起来测试。它允许你进行创建和配置模拟对象。
Mockito的官方网站: Mockito 主页.
4.2. 使用 Mockito
Mockito 支持使用 mock() 静态方法创建模拟对象。
同样也支持 @Mock注解方式,如果使用注解的方式,需要使用在初始化方法调用 MockitoAnnotation.InitMock( this ) 方法
例如,下面的例子就是使用 Mockito 进行对类 ClassToTest 的单元测试。
提示
可以使用静态导入方法调用方法 mock()
4.3. Mockito的限制
Mockito 以下的类型不能进行构造:
-
终态类(final classes)
-
匿名类(anonymous classes)
-
基本数据类型(primitive types)
4.4. 模拟对象的配置
Mockito 可以使用 verify() 方法来确认某些方法是否被调用过.
when(....).thenReturn(....) 结构可以为某些条件给定一个预期的返回值.
同样可以使用doReturn(object).when(kdskfsk).methodCall 结构
4.5. 验证模拟对象的行为
Mockito 跟踪了所有的方法调用和参数的调用情况。verify()可以验证方法的行为。
查看下面的例子:
4.6. Spy
@Spy 或者方法 spy() 可以包含一个真实的对象. 每次调用,除非特出指定,委托给改真实对象的调用.
5. Mockito 在 Android 平台测试
5.1. 在 Android 使用 Mockito
Mockito 同样也可以在安卓平台上进行测试。
5.2. 安装
在 Android 测试项目中使用 Mockito。添加下面的包到Android 测试项目的 libs 目录
https://mockito.googlecode.com/files/mockito-all-1.9.5.jar
http://dexmaker.googlecode.com/files/dexmaker-1.0.jar
http://dexmaker.googlecode.com/files/dexmaker-mockito-1.0.jar
接下来可以在你的测试项目中使用 Mockito 。
6. 链接和参考
Mockito 项目主页
Mockito 的依赖注入功能
Unit tests with Mockito - Tutorial
使用 Mockito 单元测试 – 教程
使apk具有system权限
使apk具有system权限的方法:
1. 在应用程序的AndroidManifest.xml
中的manifest节点中加入android:sharedUserId="android.uid.system"
这个属性。如下所示:
2. 修改Android.mk文件,加入LOCAL_CERTIFICATE := platform
这一行。
3. 使用mm命令来编译,或者参照ANDROID系统证书PLATFORM.X509.PEM,PLATFORM.PK8转换为.KEYSTORE文件对编译好的APK进行单独签名。
Android自动测试之Monkey工具
什么是Monkey
Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中。它向系统发送伪随机的用户事件流(如按键输入、触摸屏输入、手势输入等),实现对正在开发的应用程序进行压力测试。Monkey测试是一种为了测试软件的稳定性、健壮性的快速有效的方法。
Monkey的基本用法
基本语法如下:
如果不指定options,Monkey将以无反馈模式启动,并把事件任意发送到安装在目标环境中的全部包。下面是一个更为典型的命令行示例,它启动指定的应用程序,并向其发送500个伪随机事件:
更加详细的命令参数,参考下表:
标题 | 选项 | 描述 |
---|---|---|
General | --help |
Prints a simple usage guide. |
-v |
Each -v on the command line will increment the verbosity level. Level 0 (the default) provides little information beyond startup notification, test completion, and final results. Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. Level 2 provides more detailed setup information such as activities selected or not selected for testing. | |
Events | -s <seed> |
Seed value for pseudo-random number generator. If you re-run the Monkey with the same seed value, it will generate the same sequence of events. |
--throttle <milliseconds> |
Inserts a fixed delay between events. You can use this option to slow down the Monkey. If not specified, there is no delay and the events are generated as rapidly as possible. | |
--pct-touch <percent> |
Adjust percentage of touch events. (Touch events are a down-up event in a single place on the screen.) | |
--pct-motion <percent> |
Adjust percentage of motion events. (Motion events consist of a down event somewhere on the screen, a series of pseudo-random movements, and an up event.) | |
--pct-trackball <percent> |
Adjust percentage of trackball events. (Trackball events consist of one or more random movements, sometimes followed by a click.) | |
--pct-nav <percent> |
Adjust percentage of "basic" navigation events. (Navigation events consist of up/down/left/right, as input from a directional input device.) | |
--pct-majornav <percent> |
Adjust percentage of "major" navigation events. (These are navigation events that will typically cause actions within your UI, such as the center button in a 5-way pad, the back key, or the menu key.) | |
--pct-syskeys <percent> |
Adjust percentage of "system" key events. (These are keys that are generally reserved for use by the system, such as Home, Back, Start Call, End Call, or Volume controls.) | |
--pct-appswitch <percent> |
Adjust percentage of activity launches. At random intervals, the Monkey will issue a startActivity() call, as a way of maximizing coverage of all activities within your package. | |
--pct-anyevent <percent> |
Adjust percentage of other types of events. This is a catch-all for all other types of events such as keypresses, other less-used buttons on the device, and so forth. | |
Constraints | -p <allowed-package-name> |
If you specify one or more packages this way, the Monkey will only allow the system to visit activities within those packages. If your application requires access to activities in other packages (e.g. to select a contact) you'll need to specify those packages as well. If you don't specify any packages, the Monkey will allow the system to launch activities in all packages. To specify multiple packages, use the -p option multiple times — one -p option per package. |
-c <main-category> |
If you specify one or more categories this way, the Monkey will only allow the system to visit activities that are listed with one of the specified categories. If you don't specify any categories, the Monkey will select activities listed with the category Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -c option multiple times — one -c option per category. | |
Debugging | --dbg-no-events |
When specified, the Monkey will perform the initial launch into a test activity, but will not generate any further events. For best results, combine with -v, one or more package constraints, and a non-zero throttle to keep the Monkey running for 30 seconds or more. This provides an environment in which you can monitor package transitions invoked by your application. |
--hprof |
If set, this option will generate profiling reports immediately before and after the Monkey event sequence. This will generate large (~5Mb) files in data/misc, so use with care. See Traceview for more information on trace files. | |
--ignore-crashes |
Normally, the Monkey will stop when the application crashes or experiences any type of unhandled exception. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. | |
--ignore-timeouts |
Normally, the Monkey will stop when the application experiences any type of timeout error such as a "Application Not Responding" dialog. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. | |
--ignore-security-exceptions |
Normally, the Monkey will stop when the application experiences any type of permissions error, for example if it attempts to launch an activity that requires certain permissions. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. | |
--kill-process-after-error |
Normally, when the Monkey stops due to an error, the application that failed will be left running. When this option is set, it will signal the system to stop the process in which the error occurred. Note, under a normal (successful) completion, the launched process(es) are not stopped, and the device is simply left in the last state after the final event. | |
--monitor-native-crashes |
Watches for and reports crashes occurring in the Android system native code. If --kill-process-after-error is set, the system will stop. | |
--wait-dbg |
Stops the Monkey from executing until a debugger is attached to it. |