III. 拓展工具
  1. AssertJ Android
  square/assertj-android
  极大的提高可读性。
  // 一般的JUnit
  assertEquals(View.GONE, view.getVisibility());
  // AssertJ Android
  assertThat(view).isGone();
  2. Robolectric
  Robolectric
  让模拟测试直接在开发机上完成,而不需要在Android系统上。
  主要是解决模拟测试中耗时的缺陷,模拟测试需要安装以及跑在Android系统上,也是需要在Android虚拟机或者设备上面,所以十分的耗时。基本上每次来来回回都需要几分钟时间。针对这类问题,业界其实已经有了一个现成的解决方案: Pivotal实验室推出的 Robolectric 。通过使用Robolectrict模拟Android系统核心库的 Shadow Classes 的方式,我们可以像写本地测试一样写这类测试,并且直接运行在工作环境的JVM上,十分方便。
  3. Mockito
  Mockito
  快速模拟控制系统架构返回参数。
  不同于Roblectric,Mockito可以通过模拟并控制或修改一些方法的行为。
  // 无论什么时候调用 myQueryObject.getCurrentTime,返回值都会是 1363027600
  Mockito.doReturn((long) 1363027600).when(myQueryObject).getCurrentTime();
  4. Robotium
  RobotiumTech/robotium
  (Integration Tests)模拟用户操作,事件流测试。
  通过模拟用户的操作的行为事件流进行测试,这类测试无法避免需要在虚拟机或者设备上面运行的。是一些用户操作流程与视觉显示强相关的很好的选择。
  Building Local Unit Tests
  Testing the Android way
  timber中的单元测试实例
  timber/build.gradle
  A BDD (RSpec-like) testing library for Java
  © 2012 - 2016, Jacksgong(blog.dreamtobe.cn). Licensed under the Creative Commons Attribution-NonCommercial 3.0 license (This license lets others remix, tweak, and build upon a work non-commercially, and although their new works must also acknowledge the original author and be non-commercial, they don’t have to license their derivative works on the same terms). http://creativecommons.org/licenses/by-nc/3.0/