INTRODUCTION
Mockito is one of the testing framework which provides easy way to test the DAO and Service layer API methods.
It provides set of APIs which helps to avoid the actual call to the database or any external service to fetch the data and verify the data.
The idea is to mock the required object and test the methods with the mocked object.
IMPLEMENTATION
Consider I am having a service method which actually performs the addition of new employee into the existing system.
Create a Model for Person.
- package java2ranjith.mokito.dto;
- public class Person {
- private String id;
- private String name;
- private String age;
- private String address;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }
Create a Service.
- package java2ranjith.mokito.service;
- import java2ranjith.mokito.dao.DataHolder;
- import java2ranjith.mokito.dto.Person;
- public class PersonService {
- DataHolder dataholder = new DataHolder();
- public void add(Person person) {
- dataholder.add(person);
- }
- public void remove(String id) {
- dataholder.remove(id);
- }
- public Person get(String id) {
- return dataholder.get(id);
- }
- }
Create a Data Holder which actually stores the data into the map instead storing into the specific database.
- package java2ranjith.mokito.dao;
- import java.util.HashMap;
- import java.util.Map;
- import java2ranjith.mokito.dto.Person;
- public class DataHolder {
- public static Map<String, Person> data = new HashMap<>();
- public void add(Person person) {
- data.put(person.getId(), person);
- }
- public Person get(String id) {
- return data.get(id);
- }
- public void remove(String id) {
- data.remove(id);
- }
- }
And finally, my test class is MockitoTest.java which contains the list of test methods to test the actual service methods.
- package java2ranjith.mokito.test;
- import static org.mockito.Matchers.anyString;
- import java2ranjith.mokito.dao.DataHolder;
- import java2ranjith.mokito.dto.Person;
- import java2ranjith.mokito.service.PersonService;
- import junit.framework.Assert;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.mockito.InjectMocks;
- import org.mockito.Mock;
- import org.mockito.Mockito;
- import org.mockito.Spy;
- import org.powermock.api.mockito.PowerMockito;
- import org.powermock.core.classloader.annotations.PrepareForTest;
- import org.powermock.modules.junit4.PowerMockRunner;
- import org.powermock.reflect.Whitebox;
- @RunWith(PowerMockRunner.class)
- @PrepareForTest({ PersonService.class, Person.class, DataHolder.class })
- public class MockitoTest {
- @Before
- public void setUp() throws Exception {
- mockPrivateClasses();
- }
- @Spy
- @InjectMocks
- PersonService personService = initPersonService();
- @Mock
- DataHolder mockedDataHolder;
- @Test
- public void testAdd() {
- final String empId = "1000";
- // GIVEN (do the required things before the actual service method called.)
- /**
- * Here i am building the required object to
- * be stored into the database.
- */
- Person person = buildPerson();
- person.setId(empId); // setting my own id.
- /**
- * Mocking the return object of the DAO method. The meaning of
- * the below code is, when any service method which calls
- * the dao method get() with any string, i.e any empId, then return
- * my constructed(mocked) object.
- */
- Mockito.doReturn(person).when(mockedDataHolder).get(anyString());
- // WHEN (fire the service method)
- personService.add(person); // calling the service method
- // to check retrieve the Person object with the empId.
- Person newPersonObject = personService.get(empId);
- // THEN (do the necessary testing on the retrived object.)
- Assert.assertNotNull(newPersonObject);
- Assert.assertEquals(newPersonObject.getName(), person.getName());
- }
- /**
- * Construct the Person object with required values.
- *
- * @return
- */
- private Person buildPerson() {
- Person person1 = new Person();
- person1.setId("1");
- person1.setName("one");
- person1.setAge("10");
- person1.setAddress("OneOneOne");
- return person1;
- }
- /**
- * Mock the private classes for the testing.
- */
- private void mockPrivateClasses() {
- PowerMockito.mockStatic(PersonService.class);
- PowerMockito.mockStatic(Person.class);
- PowerMockito.mockStatic(DataHolder.class);
- }
- private PersonService initPersonService() {
- return Whitebox.newInstance(PersonService.class);
- }
- }
No comments :
Post a Comment