You are currently viewing Android – Unit Testing with Mockito framework

Android – Unit Testing with Mockito framework

  • Post author:
  • Post category:Android
  • Post comments:0 Comments
  • Post last modified:December 10, 2021
  • Reading time:5 mins read

What is Mockito?

Mockito is a mocking framework for Unit testing that helps us to test with more flexibility and efficiency. In this part we will be discussing the very basic of Mockito.

If you are new at this then i suggest you should have a look at the previous tutorial.

Setup

To start with mockito lets add dependency in your app ⇒ build.gradle file.

testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.23.4'

Classes

We have our Calculator.java  class and here we have a method named execute().

public int execute(int a, int b){
    return sum(a,b)*a;    // What is sum?
  }

Here sum is another method from the interface which we have created earlier named CalculatorService.java and it is an Interface which is implemented in Calculator.java class.

CalculatorService.java

public interface CalculatorService {
  int sum(int i, int j);
}

Calculator.java

public class Calculator implements CalculatorService{

  public int execute(int a, int b){
    return sum(a,b)*a;
  }

  @Override
  public int sum(int i, int j) {
    return i+j;
  }
}

This is the very basic implementation of interface.

Now go to the ExampleUnitTest, add this method and run the test, see what happen.

@Test
public void checkSum(){
  assertEquals(8,calculator.execute(2,2));
  System.out.print(calculator.execute(2,2));
}

It will pass and print 8 for this case. Up to this we haven’t used mockito yet.

Use of Mockito

Now lets just think of some situation when you have to test your own method which is or are dependent on some other method provided by some other service provider!!!!!  What was that? Let’s clear it. Assume that you want to use an api or google api or some method by google in your method, then test it. Now when you run the test it JUnit will try to test google method which is completely unnecessary. So we want to avoid google method testing. This is where mockito comes handy. It’s like your service might not activated but you already know the output.

let’s start coding.

We have created a service named CalcuatorService.java and that is an interface shown above. In the Calculator.java class we need to use the sum methos but we don’t know if it is functional or not. So what we need to do is to create a mock of that interface ( in this case ) and pass it to the field class initialization. Then use static import of when() and  verify() like below.

ExampleUnitTest.java

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class ExampleUnitTest {

    private Calculator calculator = null;

    private CalculatorService service = mock(CalculatorService.class);

    @Before
    public void setUp(){
        calculator = new Calculator(service);
    }

    @Test
    public void checkSum(){
        when(service.sum(2,2)).thenReturn(4);
        assertEquals(8,calculator.execute(2,2));
        verify(service).sum(2,2);
    }

}

Calculator.java

public class Calculator{

    private CalculatorService service;

    public Calculator(CalculatorService service) {
        this.service = service;
    }

    public int execute(int a, int b){
        return service.sum(a,b)*a;
    }
}

So we believe that service.sum(2,2) will work and it will return as expected. So what we did, we called a method named when() and pass the method and get something as return. The explanation goes like this.

When when() method gets service.sum(2,2) then it will return 4

In this case we don’t know how the method operates.

Again, if we want to check if the method service.sum(2,2) is used or not then we can use verify(). It checks if we are using the method or we are just calculating ourselves.

More Uses

Below i have shared some other implementations of mockito ⇒

  • Simple usage : stub, use, verify
  • Programatic creation of mocks via mock() or spy()
  • Programmatic stubbing via
    • Mockito.when(mock.action()).thenReturn(true)
    • BDDMockito.given(mock.action()).willReturn(true)
  • Customize mock answer or provide your own
  • Programmatic verification via
    • Mockito.verify(mock).action()
    • BDDMockito.then(mock).should().action()
  • Annotation sugar via @Mock, @Spy, @Captor or @InjectMocks
  • JUnit first class support via the runner MockitoJUnitRunner and the now favored rule MockitoJUnit.rule()

Rules

  • Do not mock types you don’t own
  • Don’t mock value objects
  • Don’t mock everything
  • Show some love with your tests

You can find more about mockito at their website and their Github and always keep in touch with AndroVaid.

Thank you for being with us.

Leave a Reply