寫在前面
因個人能力有限,可能會出現理解錯誤的地方,歡迎指正和交流!
關于單元測試
通常一個優秀的開源框架,一般都會有很完善的單元測試。
舉個例子:
不好意思,我調皮了 :)
在這兩個優秀的開源框架中,都非常注重單元測試的編寫,而且單元測試占的比例也很大,甚至在某些方面上,測試代碼會遠遠多于該框架本身的代碼。這里我們以android-architecture中的todoapp為例,看看它的測試代碼覆蓋率是多少:
恩,相當可觀的數據。至少對我而言.
至于如何查看測試代碼覆蓋率,方法很簡單,看圖操作:
第一步
第二步
第三步
Show Time
所以...
寫單元測試很重要.
寫單元測試很重要..
寫單元測試很重要..
Mockito
接下來就讓我們開始使用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測試
在 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);
}
}
如何使用Mockito進行測試
這里我以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回歸.....