Getting started with Mockito for Eclipse

Getting started with Mockito for Eclipse

Summary

In Java unit testing, we often encounter scenarios that depend on other modules, but other modules are not well developed, such as the following figure:
 
The "tested module A" depends on the module B/C/D. during unit test, you can Mock the B/D (Note: you do not need to Mock the C).  
Mockito(http://mockito.org/ )It is a popular Mock open source framework, and there are many online introductions, but I found that most of the articles did not introduce the first entry project for Eclipse, especially the introduction of dependency libraries, so I wrote this article.  
This code directly borrows "Android development advanced - from small workers to experts" to thank the author he Honghui!

Engineering example

Step 1: create a normal Java project named MockTest

Step 2: create the tested code

First, an entity class 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

Create another DAO interface:

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

Then create the class that will be tested. This class will depend on the instance of 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("Getting student information from the Internet");
        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

Step 3: test with Mockito

The StuController tested in the previous step depends on the instance of StudentDAO, but now it is assumed that StudentDAO has not been implemented, so Mock is required.  
Download 3 libraries, create and place them in the libs directory, and add project dependencies:
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/#/ 

To create a test class:

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";

        // Return returnstudio object when calling getStudentFromDB
        when(mStuDao.getStudentFromDB(anyInt())).thenReturn(returnStudent);
        // Call getStudentInfo
        Student student = mController.getStudentInfo(123);
        // Validation data
        assertEquals(student.id, 123);
        assertEquals(student.name, "mock-user");
    }

    @Test
    public void testGetStudentInfoFromServer() {
        // null returned when calling getStudentFromDB
        when(mStuDao.getStudentFromDB(anyInt())).thenReturn(null);
        // Call getStudentInfo
        Student student = mController.getStudentInfo(4536);
        // Validation data
        assertEquals(student.id, 456);
        assertEquals(student.name, "server-user");
    }

    @Test
    public void testCaptureParam() {
        // Note: create a mock object
        StuController mockController = mock(StuController.class) ;

        doAnswer(new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                int studentId = (Integer)invocation.getArguments()[0] ;
                System.out.println("Student 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

Step 4: run the test

Tags: Junit Eclipse Java Android

Posted on Tue, 28 Jan 2020 07:24:19 -0800 by Karve