因我的能力有限,可能會出現理解錯誤的地方,歡迎指正和交流!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" />
第三步
![3.png](http://o8wshxdfk.bkt.clouddn.com/3.png)
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迴歸.....