First Sight of Mockito

Mockito is a mock testing tool which can help developers to do testing without completing all functions. In this article, I’ll be writing some code to show how it works. To begin with, you need to build Mockito into your project. The following maven repository is the latest release version when I’m writing this article. To build project with maven, see POM Confiurations.

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.10.19</version>
    <scope>test</scope>
</dependency>

To make life easier, you may also want to statically import the following methods:

import static org.mockito.Mockito.*;

Next, choose a class that you want to mock up:

StudentDAO studentDAO = mock(StudentDAO.class);

In the above code, since studentDAO is mocked up by Mockito, this instance becomes a puppet. You can manipulate the output (return value) of any method in StudentDAO class by monitoring the input (parameter) in order to perform a mock test. The following code shows how to register a manipulation role, and use it.

@Test //this test case tests if the logic part in that method functioning correctly
public void delete(){
    Student student1 = new Student();
    // set some properties to student1 which are meant to let it pass the logic part in StudentService().delete()
    when(studentDAO.delete(student1)).thenReturn(true); // this is how studentDAO.delete() being manipulated

    AssertTrue(new StudentService().delete(student1));
}

StudentService class stub:

// imports

public class StudentService{

    // some other variables and methods...

    StudentDAO studentDAO;

    public boolean delete(Student student){

        // perform some logic which may cause a return of false, and stop deleting the given Student

        return studentDAO.delete(student); // this studentDAO.delete() method has been interfered to return true when student1 is passed in
    }
}

Benefits of Mock Testing

Because of the ability of manipulate the return value of a method, it gives you the power to test your program based on assumptions. For example, you’ve had a class structure and method definitions, yet been implemented. The methods would return null value. Therefore, you can’t test other classes which depend on the functions that this class provides, if you just do the regular test. With mock testing, you can pretend you had those methods implemented by controlling the results. On the other hand, it also lets you to isolate the problems to what you are focusing on.

Other Commonly Used Methods

verify

To verify if a method is being called or not, you can verify it.

public static <T> T verify(T mock) // verify if a certain method is called once
public static <T> T verify(T mock, VerificationMode mode) // verify if a certain method is called certain time(s)

The above code are the method signatures of verify function. To use one of these methods, you need to mock a class, and run your test first. Then followed by verify method to check if the method is called. For example:

studentDAO = mock(StudentDAO.class);
studentService.studentDAO = studentDAO;
studentService.update();
verify(studentDAO).update();

In this example, I want to test if studentDAO.update() is called by studentService.update(). If it is called, then test passed, otherwise, test would fail.

@Mock

Instead of doing something like this:

StudentDAO studentDAO = mock(StudentDAO.class);

with @Mock annotation, you can initialize mocked classes easier:

@Mock
StudentDAO studentDAO;

@Mock
StudentService studentService;

@Mock
SomethingElse somethingElse;

Then, all you need to do is to initiate them at once:

MockitoAnnotations.initMocks(this);

doThrow

If your mocking method has a void return type, then you have to reverse the when-then order. Following code shows how to let a void method throw an exception:

doThrow(new Exception()).when(studentService).update(new Student()); // update() is a void method
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s