Testing is a vital phase in any software application and software cannot be complete if it is not completely tested. Unit Testing allows individual software components to be tested completely whereas Integration Testing ensures that multiple components in a system works fine.
also read:
In this article, we will see the support given by Spring framework towards Unit and Integrating Testing. The first section of the article deals with the support given by Spring towards Unit Testing, especially the mock interfaces and the classes. Also discussed in this article are the various supportive and utility classes for Integration. The final section of the article discusses the various annotations available in Spring that facilitate testing.
Testing Support in Spring Framework Example Code
- [download id=”3″]
Testing Support in Spring Framework
Unit Testing with Spring
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 UserGroup classes. User is dependent on UserGroup object, whereas UserGroup is not dependent on User. In this scenario, we have to make sure that User and UserGroup classes are working individually to the expected extent.
Mock objects come into picture in the case of dependent 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 UserDao classes. 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 Mock classes 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 above Controller 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; } }
The mock classes related to Spring Web MVC are present in the package ‘org.springframework.mock.web’. For example in the first test, we have made use of MockHttpServletRequest and MockHttpServletResponse, populated them with desired values, then called the handleRequest() method by passing in these arguments. We have also used jUnit package in tandem with this test class. Similarly there are mock classes available for JNDI (org.springframework.mock.jndi), Portlets (org.springframework.mock.web.portlet) etc.
Integration Testing with Spring Framework
In this section, we will discuss about the Integration support available in Spring. For explanation purposes, we will take the example of a system that maintains Customer and Account objects. Account object represents the banking account object and it will contain details like the id, the name of the account as well as the name of the bank where the account is held. Given below is the code listing for the Account object.
Account
Account.java
package net.javabeat.spring.articles.testing.custacc; public class Account { private String id; private String name; private String bankName; 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 getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } }
Next we will look into the model Customer objects. The Customer object has id which is uniquely used to identify a Customer, name of the customer. It is possible that a Customer can hold multiple accounts, so we have devised a property accounts which is a Set. The following provides the complete listing for the Customer object.
Customer
Customer.java
package net.javabeat.spring.articles.testing.custacc; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; public class Customer { private String id; private String name; private Set&t;Account> accounts; public Customer(){ accounts = new LinkedHashSet&t;Account>(); } 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 Set&t;Account> getAccounts() { return accounts; } public void setAccounts(Set&t;Account> accounts) { this.accounts = accounts; } public void addAccount(Account account){ if (account == null){ throw new NullAccountException("Account object is NULL"); } checkForDuplicateAccount(account); accounts.add(account); } public void removeAccount(Account account){ if (account == null){ throw new NullAccountException("Account object is NULL"); } accounts.remove(account); } private void checkForDuplicateAccount(Account accountToCheck){ Iterator&t;Account> iterator = accounts.iterator(); while (iterator.hasNext()){ Account account = iterator.next(); if (account.getId().equals(accountToCheck.getId())){ throw new DuplicateAccountException("Account id " + accountToCheck.getId() + " is duplicate"); } } } }
The above class also provides support for adding and removing Account objects to a Customer object. While adding an Account object to the Customer object, the incoming Account object is checked for the duplicate nature, and if it is not the case, then the Account object will be linked to the Customer object.
Account Service
AccountService.java
package net.javabeat.spring.articles.testing.custacc; public class AccountService { public void linkAccountTo(Account account, Customer customer){ customer.addAccount(account); } public void unlinkAccountFrom(Account account, Customer customer){ customer.removeAccount(account); } }
The above service ‘AccountService’ provides support for linking and de-linking account objects to customer objects through the methods linkAccountTo and unlinkAccountFrom methods. The CustomerService service which is given below provides for registering and de-registering customer objects. In the registerCustomer() method, the id and the name of the customer are taken as inputs for registering a Customer, internally a map is maintained for storing all the Customer objects. In the case of deregistration, the customer object which is passed as an input is removed from the internal input.
Customer Service
CustomerService.java
package net.javabeat.spring.articles.testing.custacc; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class CustomerService { private Set&t;Customer> customers; public CustomerService(){ customers = new HashSet&t;Customer>(); } public Customer registerCustomer(String id, String name){ Customer customer = new Customer(); customer.setId(id); customer.setName(name); checkForDuplicateCustomer(customer); customers.add(customer); return customer; } private void checkForDuplicateCustomer(Customer customerToCheck){ Iterator&t;Customer> iterator = customers.iterator(); while (iterator.hasNext()){ Customer customer = iterator.next(); if (customer.getId().equals(customerToCheck.getId())){ throw new DuplicateCustomerIdException("Customer Id is duplicate"); } } } public void deregisterCustomer(Customer customer){ customers.remove(customer); } public Set&t;Customer> getAllCustomers(){ return customers; } }
To make things interesting, we have defined two Application exceptions, DuplicateAccountException and DuplicateCustomerIdException. The code listing for Duplicate Account Exception is given below. It will be thrown when the client attempts to link an account object to a Customer object, however it is found that the Account object seems to be a duplicate.
Duplicate Account Exception
DuplicateAccountException.java
package net.javabeat.spring.articles.testing.custacc; public class DuplicateAccountException extends RuntimeException{ /** * Default serial version UID. */ private static final long serialVersionUID = 1L; public DuplicateAccountException(String message){ super(message); } }
The exception, Duplicate Customer Id Exception (the code listing for which is given below) will be thrown when a customer registration happens through CustomerService.registerCustomer() and at a later point of time, it is found that the Customer Id is duplicate.
Duplicate Customer Id Exception
DuplicateCustomerIdException.java
package net.javabeat.spring.articles.testing.custacc; public class DuplicateCustomerIdException extends RuntimeException{ /** * Default serial version UID */ private static final long serialVersionUID = 1L; public DuplicateCustomerIdException(String message){ super(message); } }
Using Abstract Spring Context Test
To start with, we will see how to make use of AbstractSpringContextTests class. Note that all the Spring Test classes extends jUnit’s TestCase, so it is mandatory to have junit.jar in the application’s classpath. Note that we have defined the configuration file customer-account.xml that contains the Customer Service and the Account Service objects. The snippet of the configuration file will be shown soon. The method loadContext() has to be overridden to return an array of strings representing the configuration files containing the bean definitions that will be used within the test classes.
CustomerServiceTest.java
package net.javabeat.spring.articles.testing.custacc; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.AbstractSpringContextTests; public class CustomerServiceTest extends AbstractSpringContextTests{ @Override protected ConfigurableApplicationContext loadContext(Object arg0)throws Exception { String locations[] = {"customer-account.xml"}; ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(locations); return context; } public void testCreateCustomer() throws Exception{ ApplicationContext context = getContext("customer-test"); CustomerService customerService = (CustomerService)context.getBean("customerService"); Customer customer = customerService.registerCustomer("1", "David"); assertNotNull(customer); } public void testCreateDuplicateCustomer() throws Exception{ ApplicationContext context = getContext("customer-test"); CustomerService customerService = (CustomerService)context.getBean("customerService"); try{ customerService.registerCustomer("1", "Freddy"); fail("Exception for duplicate customer id should have been thrown"); }catch (DuplicateCustomerIdException exception){ // Test passes } } }
Now, let us take an example test. Since the context is already loaded, we have retrieved the bean in the test method from the context and then we have called the desired methods. Note that in all the test methods, we have manually populated the test objects, in this case, the Customer Service object.
Using Abstract Dependency Injection Spring Context Tests
One could have noticed that in the above example, the test objects have to be manually populated, before the execution of a test method. However, this can be avoided by making use of AbstractDependencySpringContextTests class. From the name of the class, one can guess that this class can be used to resolve the dependencies (i.e this class has the capability to populate the test dependant objects).
CustomerServiceTest1.java
package net.javabeat.spring.articles.testing.custacc; import org.springframework.test.AbstractDependencyInjectionSpringContextTests; public class CustomerServiceTest1 extends AbstractDependencyInjectionSpringContextTests{ protected CustomerService customerService; public CustomerServiceTest1(){ setPopulateProtectedVariables(true); } protected String[] getConfigLocations(){ String[] locations = {"customer-account.xml"}; return locations; } public void testCreateCustomer(){ Customer customer = customerService.registerCustomer("3", "Jason"); assertNotNull(customer); } }
In the above example, we have used the same CustomerService object, however, we haven’t populated this test object manually from the context, however, this object will be automatically populated. Two approaches can be followed to achieve this. First one is to call the setPopulateProtectedVariables() method to true, and to make the dependant test objects marked with the protected modifier. If for some reasons, this approach is not desirable, then a setter method – example setCustomerService() can be provided that will get called for populating the instance variable customerService.
Annotations Support in Spring Testing Package
In this final section, we will see how to make use of Spring annotations dedicated for supporting testing.The Spring Framework provides a common set of Spring-specific annotations in the org.springframework.test.annotation package that you can use in your testing if you are developing against Java 5 or greater. Given below are the annotations added and we will go through them with code example.
- @Repeat
- @Timed
- @NotTransactional
- @ExpectedException
- @IfProfileValue
- @ProfileValueSourceConfiguration
Annotations Test
AnnotationsTest.java
package net.javabeat.spring.articles.testing.annotations; import junit.framework.Assert; import net.javabeat.spring.articles.testing.custacc.Account; import net.javabeat.spring.articles.testing.custacc.AccountService; import net.javabeat.spring.articles.testing.custacc.Customer; import net.javabeat.spring.articles.testing.custacc.CustomerService; import net.javabeat.spring.articles.testing.custacc.DuplicateCustomerIdException; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.annotation.ExpectedException; import org.springframework.test.annotation.NotTransactional; import org.springframework.test.annotation.Repeat; import org.springframework.test.annotation.Timed; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration (locations = {"customer-account.xml"}) public class AnnotationsTest { private int index = 0; private String customerName; protected CustomerService customerService; private AccountService accountService; public AnnotationsTest(){ customerService = new CustomerService(); } @Test @Repeat(5) public void testRepeatAnnotation(){ setCustomer(); System.out.println("Index is " + index); System.out.println("Name is " + customerName); customerService.registerCustomer("" + index, customerName); } @Test @Timed(millis = 1000) public void testTimeoutAnnotation(){ try{ Thread.sleep(1000); }catch (Exception e){ e.printStackTrace(); } } @Test @NotTransactional public void testLinkAccount(){ Customer customer = customerService.registerCustomer("10", "SomeOne"); Account account = new Account(); account.setId("1"); account.setBankName("ABC bank"); account.setName("Savings account"); accountService.linkAccountTo(account, customer); } @Test @ExpectedException(value = DuplicateCustomerIdException.class) public void testDuplicateCustomer(){ Customer customer1 = customerService.registerCustomer("11", "SomeOne"); Assert.assertNotNull(customer1); Customer customer2 = customerService.registerCustomer("11", "SomeOne"); Assert.assertNotNull(customer2); } private void setCustomer(){ customerName = ""; customerName = "test-customer" + index; index ++; } }
In the above example, we have annotated the method testRepeatAnnotation() with @Repeat annotation. This annotation indicates that the method has to be invoked as many times as the value being passed as an argument to the annotation. For example, in the above case, the test method with @Repeat(5) will be called 5 times and the customer registration service will be invoked for 5 times with different values. The annotation @Timed takes input as the number of mili-seconds. It instructs the framework that if the test method is not going to be completed within the specified milli-seconds, then an exception has to be thrown.
The @ExpectedException annotation indicates that the test method is expected to throw the exception as mentioned as a class object in the annotation. If the test method calls didn’t return the expected expection, then this test method fails. For example, in the above example, we have tried to do a duplicate customer registration and it is expected that the service will throw a DuplicateCustomerIdException.
Profile Source
At times, it is expected that certain test cases need to run based on environmental properties. For example, it is wise to run performance related test-cases only in Performance Testing Environment as opposed to Development Environment. Another case would be to run suite of test cases only in Unix operating system and not in Windows. Here Performance Testing Environment and Unix Operating System are the factors for determining the runnable nature of test cases. Let us have the following example below.
ProfileAnnotationTest.java
package net.javabeat.spring.articles.testing.annotations; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.annotation.IfProfileValue; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration (locations = {"customer-account.xml"}) @ProfileValueSourceConfiguration(value = CustomProfileSource.class) public class ProfileAnnotationTest { @IfProfileValue(name = "os.name", value = "Windows XP") @Test public void test1(){ System.out.println("This test will be executed only in Windows XP OS"); } @IfProfileValue(name = "bank.name", value = "ICICI") @Test public void test2(){ System.out.println("This test will be executed if the ICICI bank accounts"); } @IfProfileValue(name = "country.name", value = "India") @Test public void test3(){ System.out.println("This test will be executed only if the country is India"); } }
In the test method test1(), the annotation @IfProfieValue() is used. The key and value to this annotation are ‘os.name’ and ‘Windows XP’. This indicates to the framework that this test method will be run only on Windows Environment. This value for the property key ‘os.name’ comes from the system property (which is System.getProperty(propertyKey)). However, there can be also be scenarios, when there can be some set of custom properties. In such cases, an implementation of ProfieValueSource can be provided and the same can be attached to the annotation @ProfileValueSourceConfiguration.
Consider the following example below. An implementation of ProfileValueSource is provided for the keys ‘bank.name’ and ‘country.name’ and this class is attached to the annotation @ProfileValueSourceConfiguration that is specified in the above class. In the methods test2() and test3(), we have used the profile keys ‘bank.name’ and ‘country.name’.
Custom Profile Source
CustomProfileSource.java
package net.javabeat.spring.articles.testing.annotations; import org.springframework.test.annotation.ProfileValueSource; public class CustomProfileSource implements ProfileValueSource{ @Override public String get(String key) { if (key.equals("bank.name")){ return "ICICI"; }else if (key.equals("country.name")){ return "India"; } return null; } }
Conclusion
In this article, we saw how to use the Support provided by Spring framework for performing Unit and Integration Testing. The first section of the article concentrated on Unit Testing with the supportive mock classes provided by Spring framework. The latter section of the article dealt with making use of AbstractSpringContextTests and AbstractSpringDependencyInjectionContextTests for performing Integration testing. Finally we discussed about the various annotations added to Spring framework for facilitating testing.
also read:
If you are interested in receiving the future articles from JavaBeat, please subscribe here. If you have any questions while reading the articles, please write it in the comments section. We would be happy to help you understand the concept.