Eclipse使用Mockito入门

Eclipse使用Mockito入门

概述

在Java单元测试时,经常遇到依赖其它模块、但其它模块没有开发好的场景,比如下图: 
Eclipse使用Mockito入门_第1张图片 
“被测模块A”依赖于模块B/C/D,单元测试时可以对B/D做Mock(注意:无需对C做Mock)。 
Mockito(http://mockito.org/)是一个流行的Mock开源框架,网上介绍也很多,但是我发现大多数文章都没有针对Eclipse做第一个入门工程的介绍,特别是依赖库的引入,所以写这篇文章。 
本文代码直接借用《Android开发进阶——从小工到专家》,向作者何红辉致谢!

工程例子

步骤1:创建一个普通的Java工程,工程名MockTest

步骤2:创建被测代码

首先是一个实体类Student:

public class Student {
    public int id;
    public String name;

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + "]";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

再创建一个DAO接口:

public interface StudentDAO {
    Student getStudentFromDB(int sid);
}
  • 1
  • 2
  • 3

再创建真正将被测试的类,这个类会依赖于StudentDAO 的实例:

public class StuController {
    private StudentDAO mStudentDAO;

    public Student getStudentInfo(int sid) {
        Student student = null;
        if (mStudentDAO != null) {
            student = mStudentDAO.getStudentFromDB(sid);
        }
        if (student == null) {
            student = fetchStudentInfo(sid);
        }
        return student;
    }

    private Student fetchStudentInfo(int sid) {
        System.out.println("从网络上获取学生信息");
        Student student = new Student();
        student.id = 456;
        student.name = "server-user";
        return student;
    }

    public void setStudentDAO(StudentDAO dao) {
        this.mStudentDAO = dao;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

步骤3:用Mockito测试

前面步骤中被测试的StuController 依赖于StudentDAO 的实例,但是现在假定StudentDAO 还没有实现,那就需要Mock。 
下载3 个库,创建并放到libs目录下,并添加工程依赖: 
mockito-core-2.0.71-beta.jar,http://mockito.org/ 
objenesis-tck-2.4.jar,http://objenesis.org/ 
byte-buddy-1.4.5.jar,http://bytebuddy.net/#/ 
Eclipse使用Mockito入门_第2张图片

创建测试类:

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.*;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

public class StuControllerTest {

    StuController mController;
    StudentDAO mStuDao;

    @Before
    public void setUp() throws Exception {
        mController = new StuController();
        mStuDao = mock(StudentDAO.class);
        mController.setStudentDAO(mStuDao);
    }

    @Test
    public void testGetStudentInfo() {
        Student returnStudent = new Student();
        returnStudent.id = 123;
        returnStudent.name = "mock-user";

        // 调用getStudentFromDB时返回returnStudent对象
        when(mStuDao.getStudentFromDB(anyInt())).thenReturn(returnStudent);
        // 调用getStudentInfo
        Student student = mController.getStudentInfo(123);
        // 验证数据
        assertEquals(student.id, 123);
        assertEquals(student.name, "mock-user");
    }

    @Test
    public void testGetStudentInfoFromServer() {
        // 调用getStudentFromDB时返回null
        when(mStuDao.getStudentFromDB(anyInt())).thenReturn(null);
        // 调用getStudentInfo
        Student student = mController.getStudentInfo(4536);
        // 验证数据
        assertEquals(student.id, 456);
        assertEquals(student.name, "server-user");
    }

    @Test
    public void testCaptureParam() {
        // 注意: 创建一个mock对象
        StuController mockController = mock(StuController.class) ;

        doAnswer(new Answer() {
            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                int studentId = (Integer)invocation.getArguments()[0] ;
                System.out.println("学生id : " + studentId);
                assertEquals(666, studentId);
                return null;
            }
        }).when(mockController).getStudentInfo(anyInt()) ;

        mockController.getStudentInfo(666) ;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

步骤4:运行测试

Eclipse使用Mockito入门_第3张图片

你可能感兴趣的:(单元测试)