Android 单元测试之 Mockito 浅析

1,115 阅读2分钟

写在前面

因个人能力有限,可能会出现理解错误的地方,欢迎指正和交流!

关于单元测试

通常一个优秀的开源框架,一般都会有很完善的单元测试。

举个例子:

不好意思,我调皮了 :)

在这两个优秀的开源框架中,都非常注重单元测试的编写,而且单元测试占的比例也很大,甚至在某些方面上,测试代码会远远多于该框架本身的代码。这里我们以android-architecture中的todoapp为例,看看它的测试代码覆盖率是多少:

0.png

恩,相当可观的数据。至少对我而言.

至于如何查看测试代码覆盖率,方法很简单,看图操作:

第一步

第二步

第三步

3.png

Show Time

4.png

所以…

写单元测试很重要.

写单元测试很重要..

写单元测试很重要..

Mockito

接下来就让我们开始使用Mockito进行单元测试吧

准备工作

dependencies {
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.+'
    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 {
    AccountData accountData;
    public void setupAccountData(){
        MockitoAnnotations.initMocks(this);
    }
    public void testIsNotNull(){
        assertNotNull(accountData);
    }
    }

2.使用 @RunWith(MockitoJUnitRunner.class) 方式

(MockitoJUnitRunner.class)
public class MockitoJUnitRunnerTest {
    AccountData accountData;
    public void testIsNotNull(){
        assertNotNull(accountData);
    }
    }

3.使用 MockitoRule 方式

public class MockitoRuleTest {
    AccountData accountData;
    public MockitoRule mockitoRule = MockitoJUnit.rule();
    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,那么你可以这样写:


public void testIsLogin(){
    when(accountData.isLogin()).thenReturn(true);
    boolean isLogin=accountData.isLogin();
    assertTrue(isLogin);
    }

如果你希望 getUserName() 被调用返回Jack


public void testGetUserName(){
    when(accountData.getUserName()).thenReturn("Jack");
    assertEquals("Jack",accountData.getUserName());
    }

如果你希望对 setUserName(String userName) 方法中参数进行测试


public void testSetUserName(){
    accountData.setUserName("wang");
    verify(accountData).setUserName(Matchers.eq("wang"));
    }

如果你想统计 ‘isLogin()’ 方法被调用的次数:


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回归…..