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:
- I am using Maven – the build tool
- Eclipse as the IDE, version Luna 4.4.1.
- TestNG is my testing framework.
- Add Mockito dependency to our
pom.xml
.
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javarticles.mockito</groupId> <artifactId>mockitoVerifyBehavior</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.8</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>2.0.5-beta</version> </dependency> </dependencies> </project>
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.
EmployeeManager:
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
.
Char
– Default value is 0float
– Default value is 0fshort
– Default value is 0Collection
– returns empty collectionMap
– returns empty maplong
– Default value is 0double
– Default value is 0dint
– Default value is 0Integer
– will return 0. the wrapper objects return the same as the primitive.byte
– returns 0
MockDefaultValuesTests:
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; @BeforeMethod public void createMock() { empManager = mock(EmployeeManager.class); emp = new Employee(); } @Test public void defaultCharValue() { assertEquals(0, empManager.getEmployeesInitial(emp)); } @Test public void defaultFloatValue() { assertEquals(0f, empManager.getBonusPercent(emp)); } @Test public void defaultShortValue() { assertEquals(0, empManager.getEmpCode(emp)); } @Test public void defaultCollectionValue(){ assertTrue(empManager.getAllEmployeesJoinedBetween(new Date(), new Date()).isEmpty()); } @Test public void defaultMapValue(){ assertTrue(empManager.getEmployeesSalary().isEmpty()); } @Test public void defaultLongValue(){ assertEquals(0, empManager.getSalary(emp)); } @Test public void defaultDoubleValue(){ assertEquals(0d, empManager.getTax(emp)); } @Test public void defaultIntValue(){ assertEquals(0, empManager.getLeavesAvailed(emp)); } @Test public void defaultCustomTypeValue(){ assertNull(empManager.getManager(emp)); } @Test public void defaultIntWrapperValue(){ assertEquals(new Integer(0), empManager.getTotalEmployees()); } @Test public void defaultByteValue(){ assertEquals(0, empManager.getTableIdAsByte()); } }
Download the source code
This was an example about Mockito Mock object’s default return values.