Advertisements
In a system with multiple components, Unit testing mandates that each component works fine individually. Various factors that are ensured at this stage are whether the functionality provided by the various methods (private and public) are correct in terms of passing arguments and handling of exceptions. As an example, let us assume there are User and UserGroupclasses . User is dependant on UserGroup object, whereas UserGroup is not dependant on User. In this scenario, we have to make sure that User and UserGroupclasses are working individually to the expected extent.
Mock objects come into picture in the case of dependant objects. Let us assume that there is a class called UserService which is dependant on UserDao object. UserDao class takes care of hitting the database for performing CRUD operations. In this case, it is also not possible that the UserDao class be available during the early stages of development for the simple reason that the database environment may not be available. In such cases, we mock the implementation of UserDaoclasses. Spring Framework supports such mocking facility for almost all the classes that cannot be tested. For example, let us take the example of a Servlet that parses and returns the request information back to the client. After writing the Servlet, it is impossible to test the Servlet in stand-alone mode, the only way to test it would be starting a Web Container and then invoking a client that can initiate HTTP requests.
This may not be ideal for all scenarios, as for every change done to the Servlet, the Servlet has to be re-deployed (hot deployment can be used if the Container supports) it. Let us jump into an example to see how Mockclasses come to the rescue. We will develop a Controler using Spring framework that accepts an account information and returns a view containing the various details related to the account such as the account name, customer name etc..
Account Info
AccountInfo.java
package net.javabeat.spring.articles.testing.mock.web.controller;
public class AccountInfo {
private String accountId;
private String customerName;
private String customerNumber;
private String debitCardNumber;
public AccountInfo(String customerName, String customerNumber, String debitCardNumber){
this.customerName = customerName;
this.customerNumber = customerNumber;
this.debitCardNumber = debitCardNumber;
}
public String getAccountId() {
return accountId;
}
public void setAccountId(String accountId) {
this.accountId = accountId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(String customerNumber) {
this.customerNumber = customerNumber;
}
public String getDebitCardNumber() {
return debitCardNumber;
}
public void setDebitCardNumber(String debitCardNumber) {
this.debitCardNumber = debitCardNumber;
}
}
The above class represents AccountInfo model object containing details ike the account id, name of the customer, id of the customer etc. To make the system more interesting, we will throw an Invalid Account Id Exception when the account id is invalid (that is, the account id is not present in the database).
Invalid Account Id Exception
InvalidAccountIdException.java
package net.javabeat.spring.articles.testing.mock.web.controller;
public class InvalidAccountIdException extends Exception{
/**
* Default serial version UID
*/
private static final long serialVersionUID = 1L;
public InvalidAccountIdException(String message){
super(message);
}
}
It is mandatory that the client has to pass the account id to the Controller so that the account details can be displayed. Given below is an Application exception that will be thrown when the account id is not passed from the client.
Null Account Id Exception
NullAccountIdException.java
package net.javabeat.spring.articles.testing.mock.web.controller;
public class NullAccountIdException extends Exception{
/**
* Default serial version UID
*/
private static final long serialVersionUID = 1L;
public NullAccountIdException(String message){
super(message);
}
}
Now here comes the Controller class that handles the core logic. Initially it checks for the account id as part of the request, if it is not present, then an Application exception is thrown. Then it checks whether the account id is legal by checking from the existing list of accounts. Then it constructs a Model and View object with the desired inputs.
Account Info Web Controller
AccountInfoWebController.java
package net.javabeat.spring.articles.testing.mock.web.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class AccountInfoWebController extends AbstractController{
private static Map&t;String, AccountInfo> mapOfAccounts;
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
String accountId = getAccountId(request);
if (accountId == null){
throw new NullAccountIdException("Account Id is invalid or empty");
}
if (!mapOfAccounts.containsKey(accountId)){
throw new InvalidAccountIdException("Account Id is invalid");
}
AccountInfo accountInfo = mapOfAccounts.get(accountId);
ModelAndView accountInfoMV = new ModelAndView("accountInfoView", "accountInfoModel", accountInfo);
return accountInfoMV;
}
private static String getAccountId(HttpServletRequest request){
try {
return ServletRequestUtils.getStringParameter(request, "ACCOUNT_ID");
} catch (ServletRequestBindingException e) {
e.printStackTrace();
return null;
}
}
static{
mapOfAccounts = new HashMap&t;String, AccountInfo>();
mapOfAccounts.put("12345", new AccountInfo("Jerry", "12345", "67890"));
mapOfAccounts.put("23456", new AccountInfo("Jefrey", "23456", "78901"));
}
}
Note that without the support of Mock classes, the only way to test this Controller, is by deploying this component as part of a Web Application, then starting the Sever and then by initiating a Http client on the running server. It is also impossible to create an instance of the aboveController object and then call its handleRequest() method because of dependencies.
Account Info Web Controller Test
AccountInfoWebControllerTest.java
package net.javabeat.spring.articles.testing.mock.web.controller;
import java.util.Iterator;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.ModelAndViewAssert;
import org.springframework.web.servlet.ModelAndView;
public class AccountInfoWebControllerTest {
private AccountInfoWebController controller;
@Before
public void init(){
controller = new AccountInfoWebController();
}
@Test
public void test1() throws Exception{
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
MockHttpServletResponse response = new MockHttpServletResponse();
try{
controller.handleRequest(request, response);
Assert.fail("Should have thrown Null Account Id Exception");
}catch (NullAccountIdException exception){
}catch (Exception exception){
Assert.fail(exception.getMessage());
}
}
@Test
public void test2() throws Exception{
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.addParameter("ACCOUNT_ID", "11111");
MockHttpServletResponse response = new MockHttpServletResponse();
try{
controller.handleRequest(request, response);
Assert.fail("Should have thrown Invalid Account Id Exception");
}catch (InvalidAccountIdException exception){
}catch (Exception exception){
Assert.fail(exception.getMessage());
}
}
@SuppressWarnings("unchecked")
@Test
public void test3() throws Exception{
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.addParameter("ACCOUNT_ID", "12345");
MockHttpServletResponse response = new MockHttpServletResponse();
try{
ModelAndView modelAndView = controller.handleRequest(request, response);
Assert.assertNotNull(modelAndView);
ModelAndViewAssert.assertAndReturnModelAttributeOfType(modelAndView, "accountInfoModel", AccountInfo.class);
ModelAndViewAssert.assertViewName(modelAndView, "accountInfoView");
Iterator&t;String> iterator = modelAndView.getModel().keySet().iterator();
if (iterator.hasNext()){
String key = iterator.next();
AccountInfo accountInfo = (AccountInfo)modelAndView.getModel().get(key);
Assert.assertEquals("Jerry", accountInfo.getCustomerName());
Assert.assertEquals("12345", accountInfo.getCustomerNumber());
Assert.assertEquals("67890", accountInfo.getDebitCardNumber());
}else{
}
}catch (Exception exception){
Assert.fail(exception.getMessage());
}
}
@After
public void destroy(){
controller = null;
}
}