Creating Mock Objects
If you’re new to Mockito, you might find other examples to create mock objects in different ways using the same framework. As a matter of fact, there are three ways to create mock objects in Mockito:
- static method org.mockito.Mockito.mock(…)
- @Mock and calling MockitoAnnotations.initMocks(…) before anything else in @Before method
- @Mock with @RunWith(MockitoJUnitRunner.class)
In this article, we’ll be using version 2.0.2-beta which is the latest version as of this writing. I recommend using a more stable version in real-life applications. We’ll also use JUnit 4.12.
Software Requirements
- Java 7
- Eclipse Luna
- Mockito 2.0.2-beta
- JUnit 4.12
- Maven
Using static method Mockito.mock(…)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.turreta.mockito.mockobject; import java.util.ArrayList; import java.util.List; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Matchers; import org.mockito.Mockito; public class StudentServiceTest_MockObjectWay1 { private StudentService service; private StudentDao dao; @Before public void setUp() throws Exception { // Creating a mock of dao dao = Mockito.mock(StudentDao.class); service = new StudentServiceImpl(); ((StudentServiceImpl) (service)).setDao(dao); } @Test public void testFindStudents() { // Stub findStudents method Mockito.when(dao.findStudents(Matchers.anyString())).thenReturn(new ArrayList<String>()); List<String> list = service.findStudents("1"); Assert.assertTrue(list.isEmpty()); } } |
Objects with @Mock and MockitoAnnotations.initMocks(…)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.turreta.mockito.mockobject; import java.util.ArrayList; import java.util.List; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; public class StudentServiceTest_MockObjectWay2 { private StudentService service; // Create a mock of dao @Mock private StudentDao dao; @Before public void setUp() throws Exception { // This has to be the first line MockitoAnnotations.initMocks(this); service = new StudentServiceImpl(); ((StudentServiceImpl) (service)).setDao(dao); } @Test public void testFindStudents() { // Stub findStudents method Mockito.when(dao.findStudents(Matchers.anyString())).thenReturn(new ArrayList<String>()); List<String> list = service.findStudents("1"); Assert.assertTrue(list.isEmpty()); } } |
Using @Mock with @RunWith(MockitoJUnitRunner.class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.turreta.mockito.mockobject; import java.util.ArrayList; import java.util.List; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class StudentServiceTest_MockObjectWay3 { private StudentService service; // Create a mock of dao @Mock private StudentDao dao; @Before public void setUp() throws Exception { service = new StudentServiceImpl(); ((StudentServiceImpl) (service)).setDao(dao); } @Test public void testFindStudents() { // Stub findStudents method Mockito.when(dao.findStudents(Matchers.anyString())).thenReturn(new ArrayList<String>()); List<String> list = service.findStudents("1"); Assert.assertTrue(list.isEmpty()); } } |
Download the source code files
The files are available on this link.