Mockito Mock Object’s Default Return Values


In our previous article on Mockito, we have seen how to stub a mock object.

What happens if we don’t stub one of its methods?

Assuming the method is returning something, what would be its return value if we haven’t stubbed the method?

By default, for all methods that return value, mock returns null. If the method is returning a collection, by default, an empty collection will be returned. If the return type is primitive/primitive wrapper value then an appropriate value will be returned, for example, 0, false etc.

In case you are new to Mockito, it is an open source mock unit testing framework for Java. In this article, we will see an example of a mock object that is not stubbed and then call methods that return values to see what would be their default values.

Below are my setup details:


<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">

System Under Test

Our system under test is about employee tracking system. We have an interface called EmployeeManager that contains methods which all return something.


package com.javarticles.mockito;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

public interface EmployeeManager {
    char getEmployeesInitial(Employee emp);
    float getBonusPercent(Employee emp);

    short getEmpCode(Employee emp);
    Collection getAllEmployeesJoinedBetween(Date from, Date to);
    Map<Employee, Long> getEmployeesSalary();
    long getSalary(Employee emp);
    double getTax(Employee emp);
    int getLeavesAvailed(Employee emp);
    Employee getManager(Employee emp);
    Integer getTotalEmployees();
    byte getTableIdAsByte();

Mock Object’s Default Return Values

Below are the default values based on type. You can see the corresponding test in MockDefaultValuesTests.

  1. Char – Default value is 0
  2. float – Default value is 0f
  3. short – Default value is 0
  4. Collection – returns empty collection
  5. Map – returns empty map
  6. long – Default value is 0
  7. double – Default value is 0d
  8. int – Default value is 0
  9. Integer – will return 0. the wrapper objects return the same as the primitive.
  10. byte – returns 0


package com.javarticles.mockito;

import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

import java.util.Date;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class MockDefaultValuesTests {
    private EmployeeManager empManager;
    private Employee emp;
    public void createMock() {
        empManager = mock(EmployeeManager.class);
        emp = new Employee();
    public void defaultCharValue() {
        assertEquals(0, empManager.getEmployeesInitial(emp));
    public void defaultFloatValue() {
        assertEquals(0f, empManager.getBonusPercent(emp));
    public void defaultShortValue() {
        assertEquals(0, empManager.getEmpCode(emp));
    public void defaultCollectionValue(){
        assertTrue(empManager.getAllEmployeesJoinedBetween(new Date(), new Date()).isEmpty());
    public void defaultMapValue(){
    public void defaultLongValue(){
        assertEquals(0, empManager.getSalary(emp));
    public void defaultDoubleValue(){
        assertEquals(0d, empManager.getTax(emp));
    public void defaultIntValue(){
        assertEquals(0, empManager.getLeavesAvailed(emp));
    public void defaultCustomTypeValue(){
    public void defaultIntWrapperValue(){
        assertEquals(new Integer(0), empManager.getTotalEmployees());
    public void defaultByteValue(){
        assertEquals(0, empManager.getTableIdAsByte());


Download the source code

This was an example about Mockito Mock object’s default return values.

You can download the source code here:

Comments are closed.