因我的能力有限,可能会出现理解错误的地方,欢迎指正和交流!html
一般一个优秀的开源框架,通常都会有很完善的单元测试。java
举个例子:android
很差意思,我调皮了 :)git
在这两个优秀的开源框架中,都很是注重单元测试的编写,并且单元测试占的比例也很大,甚至在某些方面上,测试代码会远远多于该框架自己的代码。这里咱们以android-architecture中的todoapp为例,看看它的测试代码覆盖率是多少:框架
恩,至关可观的数据。至少对我而言.单元测试
至于如何查看测试代码覆盖率,方法很简单,看图操做:测试
第一步this
<img src="http://o8wshxdfk.bkt.clouddn.com/1.png" width="600" height="600" />
第二步
<img src="http://o8wshxdfk.bkt.clouddn.com/2.png" width="600" height="600" />
第三步

Show Time
因此...
写单元测试很重要.
写单元测试很重要..
写单元测试很重要..
接下来就让咱们开始使用Mockito进行单元测试吧
dependencies { testCompile 'junit:junit:4.12' // 若是要使用Mockito,你须要添加此条依赖库 testCompile 'org.mockito:mockito-core:1.+' // 若是你要使用Mockito 用于 Android instrumentation tests,那么须要你添加如下三条依赖库 androidTestCompile 'org.mockito:mockito-core:1.+' androidTestCompile "com.google.dexmaker:dexmaker:1.2" androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2" }
在 Mockito 中你可使用 mock()
方法来建立一个模拟对象,也可使用注解的方式 @Mock
来建立 ,这里推荐使用注解。须要注意的是,若是是使用注解方式,须要在使用前进行初始化。这里有三种初始化方式:
1.使用 MockitoAnnotations.initMocks(this) 方式
public class MockitoAnnotationsTest { @Mock AccountData accountData; @Before public void setupAccountData(){ MockitoAnnotations.initMocks(this); } @Test public void testIsNotNull(){ assertNotNull(accountData); } }
2.使用 @RunWith(MockitoJUnitRunner.class) 方式
@RunWith(MockitoJUnitRunner.class) public class MockitoJUnitRunnerTest { @Mock AccountData accountData; @Test public void testIsNotNull(){ assertNotNull(accountData); } }
3.使用 MockitoRule 方式
public class MockitoRuleTest { @Mock AccountData accountData; @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); @Test public void testIsNotNull(){ assertNotNull(accountData); } }
这里我以AccountData类为例,进行一个简单的对象模拟测试
public class AccountData { private boolean isLogin; private String userName; public boolean isLogin() { return isLogin; } public void setLogin(boolean login) { isLogin = login; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
假设如今你想对AccountData的mock对象进行模拟测试:
好比我但愿 isLogin()
方法被调用时返回true,那么你能够这样写:
@Test public void testIsLogin(){ when(accountData.isLogin()).thenReturn(true); boolean isLogin=accountData.isLogin(); assertTrue(isLogin); }
若是你但愿 getUserName()
被调用返回Jack
@Test public void testGetUserName(){ when(accountData.getUserName()).thenReturn("Jack"); assertEquals("Jack",accountData.getUserName()); }
若是你但愿对 setUserName(String userName)
方法中参数进行测试
@Test public void testSetUserName(){ accountData.setUserName("wang"); verify(accountData).setUserName(Matchers.eq("wang")); }
若是你想统计 'isLogin()' 方法被调用的次数:
@Test public void testIsLoginTimes(){ accountData.isLogin(); accountData.isLogin(); verify(accountData,times(2)).isLogin(); }
又或者
verify(mock, never()).someMethod("never called"); verify(mock, atLeastOnce()).someMethod("called at least once"); verify(mock, atLeast(2)).someMethod("called at least twice"); verify(mock, times(5)).someMethod("called five times"); verify(mock, atMost(3)).someMethod("called at most 3 times");
......
是否是使用起来简单明了。
那还等什么,赶忙Get起来吧,若是你想进一步了解,那就赶忙打开 Mockito 的官网吧。
文中代码示例
Mockito Website
Mockito GitHub
Unit tests with Mockito - Tutorial
我是Jack Wang...个人心愿是....Google回归.....