Mockito Unit Test Examples

The subject is mocking with Mockito framework. Mocking class, injection mocking, using matchers are exist.

              Mockito version : 1.10.19
              Junit version : 4.12
              Jdk = 1.7 or 1.8
              github sourceCode is : github link

        <dependency>

            <groupId>org.mockito</groupId>

            <artifactId>mockito-core</artifactId>

            <version>1.10.19</version>

            <scope>test</scope>

        </dependency>



This service interface:

import java.util.List;


public interface IMockitoFooService {

    void voidMethod();

    String returnString();

    FooEntity returnFooEntity();

    void voidMethodWith3Arguments(String str, Integer intValue, FooEntity fooEntity);

    List<Double> getDoubleList();

    List<FooEntity> getFooList();

    void setFooList(List<FooEntity> fooList);
   

}

This is serviceImpl class:



import java.util.List;

public class FooServiceImpl implements IMockitoFooService {

    @Override
    public void voidMethod() {
        throw new RuntimeException("RuntimeException message");
    }

    @Override
    public String returnString() {
        throw new RuntimeException("RuntimeException message");
    }

    @Override
    public FooEntity returnFooEntity() {
        throw new RuntimeException("RuntimeException message");
    }

    @Override
    public void voidMethodWith3Arguments(String str, Integer intValue, FooEntity fooEntity) {
        throw new RuntimeException("RuntimeException message");
    }

    @Override
    public List<double> getDoubleList() {
        throw new RuntimeException("RuntimeException message");
    }

    @Override
    public List<fooentity> getFooList() {
        throw new RuntimeException("RuntimeException message");
    }

    @Override
    public void setFooList(List<fooentity> fooList) {
        throw new RuntimeException("RuntimeException message");
    }

}



This is entity class: 


public class FooEntity {

} 


This is Testing class: 


import java.util.ArrayList;
import java.util.List;

public class FooContainerClass {

    private IMockitoFooService service;

    public void setService(IMockitoFooService service) {
        this.service = service;
    }
   
    public void callServiceMethods(){
        String str = "str";
        Integer intValue = Integer.valueOf(1);
        FooEntity fooEntity = new FooEntity();
       
        service.voidMethodWith3Arguments(str , intValue , fooEntity );
        service.voidMethod();
        List<Double> doubleList = service.getDoubleList();
        List<FooEntity> fooList = service.getFooList();
        FooEntity fooEntityResult = service.returnFooEntity();
        String resultString = service.returnString();
        service.setFooList(new ArrayList<FooEntity>());
       
        System.out.println(doubleList);
        System.out.println(fooList);
        System.out.println(fooEntityResult);
        System.out.println(resultString);
    }
}



This is one of Unit test class:

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import com.groupId.test.mockito.FooContainerClass;
import com.groupId.test.mockito.FooEntity;
import com.groupId.test.mockito.IMockitoFooService;

/**
 *This's method injection using setter method.
 */
public class MockitoUnitTest {

    @InjectMocks
    FooContainerClass fooContainerClass = new FooContainerClass();
   
    @Before
    public void before(){
        MockitoAnnotations.initMocks(this);
    }
   
    @Test
    public void fooServiceTest(){
       
        IMockitoFooService fooService = Mockito.mock(IMockitoFooService.class);
        fooContainerClass.setService(fooService);
       
        List<Double> doubleList = new ArrayList<Double>();
       
        //Mock return value
        Mockito.when(fooService.getDoubleList()).thenReturn(doubleList);
       
        List<FooEntity> fooList = new ArrayList<>();
        //Mock return value, with foo entity
        Mockito.when(fooService.getFooList()).thenReturn(fooList);
       
        FooEntity fooEntity = new FooEntity();
        //Mock return value, with single value
        Mockito.when(fooService.returnFooEntity()).thenReturn(fooEntity );
       
        String str = "str";
        //Mock return value
        Mockito.when(fooService.returnString()).thenReturn(str );
       
        //Mock void method, with three argument
        Mockito.doNothing().when(fooService).voidMethodWith3Arguments(Matchers.anyString(), Matchers.anyInt(), Matchers.any(FooEntity.class));
       
        //Mock void method, with no arguments
        Mockito.doNothing().when(fooService).voidMethod();
       
        //Mocking void method, with foo entity list argument
        Mockito.doNothing().when(fooService).setFooList(Matchers.anyListOf(FooEntity.class));
       
        fooContainerClass.callServiceMethods();
    }
}


This is second unit test class:

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.groupId.test.mockito.FooContainerClass;
import com.groupId.test.mockito.IMockitoFooService;
/**
 *This's field injection. The field's all methods are empty and return null
 */
public class MockitoUnitTestWithInjection {

    @InjectMocks
    FooContainerClass fooContainerClass = new FooContainerClass();
   
    @Mock
    IMockitoFooService fooService;
   
    @Before
    public void before(){
        MockitoAnnotations.initMocks(this);
    }
   
    @Test
    public void fooServiceTest(){
        //All fooService methods are empty and return null, so if you don't you return value of service method, you don't have to mock methods.
        fooContainerClass.callServiceMethods();
    }
}