Mockito for Android

Real purpose of unit testing is to test an functionality in complete isolation from other components. At times it becomes very difficult to test the functionality because the component is tightly coupled with other application components. In such scenarios Mockito comes to rescue. Mockito is a JAVA-based framework that is used for effective unit testing of JAVA applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in unit testing. This tutorial should help you learn how to create unit tests with Mockito as well as how to use its APIs in a simple and intuitive way. For example, if you are required to unit test login screen then you can use mockito to stub network calls and service responses.

  1. Add gradle dependency
    dependencies {

    testCompile ‘org.mockito:mockito-core: 2.+ ‘

    }
  2. Create a test class as shown in the example below:
    public class LoginControllerTest {@Rule
    public MockitoRule rule = MockitoJUnit.rule();

    @Mock
    LoginService loginService;
    private LoginController controller;
    @Before
    public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    controller= new LoginController();
    }

    @Test
    public void testDoLogin() {
    controller.doLogin(“admin”, “admin”);
    verify(view, times(1)).callLoginService(any(String.class));
    when(loginService.doLogin(“admin”, “admin”)).thenReturn(“Success”);
    }
    }

  3. Stubbing in mockito
    Using when you can stub methods. You can use when API when you want the mock to return particular value when particular method is called. when(mock.someMethod()).thenReturn(10); or
    when(mock.doLogin(“userName”, “password”)).thenReturn(“Success”);
  4. Verifying method execution
    Check methods were called with given arguments. Following example show this: verify(view, times(1)).showProgress();
    verify(view, times(1)).showProgress(any(String.class));
    Here any() matches any object of given type, excluding nulls.
  5. Spying
    Using Mockito’s spy feature, we can mock only those methods of a real object that we want to, thus retaining the rest of the original behavior. Following is an example of spy():
    List list = new LinkedList();
    List spy = spy(list);    //optionally, you can stub out some methods:   when(spy.size()).thenReturn(100);    //using the spy calls real methods   spy.add(“one”);   spy.add(“two”);    //prints “one” – the first element of a list   System.out.println(spy.get(0));    //size() method was stubbed – 100 is printed   System.out.println(spy.size());    //optionally, you can verify   verify(spy).add(“one”);   verify(spy).add(“two”);

 

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