Integration Testing
Unit tests are very useful for testing a module or class in isolation. But it is also important to do integration testing of the application to see how various modules would work together when assembled in the integrated environment. The integration tests include testing round-trip calls between client and service layers of the application.
Other objective of integration testing is to test aspects such as remote service, state management, web flow and transaction management in an application.
One of the inherent constraints of integration test is that it takes longer time to run these tests. Since the application needs to be deployed inside appropriate containers, there is also the server setup and configuration overhead involved in running these tests.
There should be enough integration tests written so as to cover different use case scenarios in the application that cannot be tested with unit tests.
Testing Support by Spring
Spring framework was designed based on agile testing strategies. The framework aids in iterative and agile software development of the application. Agile testing and early feedback is the main part of iterative development. Spring provides extensive support for both unit and integration testing.
It also provides a great support for running integration tests outside the application server. Spring is a non-invasive framework in the sense that when we use Spring, there is a minimal dependency of application code on the framework. This becomes one of the main reasons for testability of applications written in Spring.
Unit testing of Spring application can be done by using stub and mock objects. It is a common practice to mock or stub collaborator classes under test so that the test is independent of the actual implementations.
Unit Testing with Stubs
A stub is an object simulating another object in an application. It provides the same interface but it is configured to return pre-arranged results. For the same reason stubs are also called fakes.
Implementing unit tests using stubs is easy to use as it doesn’t require additional dependencies to be brought in. Consider a case in which a service implementation is under test. The service class namely CustomerServiceImpl has a repository namely CustomerRepository as an instance variable which is entrusted to do the actual connection to database and do the various operation related to Customer model. For testing this rather than using the actual repository, we create a stub repository which implements the various methods in the CustomerRepository interface. The service implementation class is shown in Listing 7-1. The stub repository used for testing the service is shown in Listing 7-2. The test class in Listing 7-3 uses this stub repository to do the actual test.
Listing 7-1. Customer service implementation class
[java]public class CustomerServiceImpl implements CustomerService {
private CustomerRepository customerRepository;
public CustomerServiceImpl(CustomerRepository customerRepository){
this.customerRepository = customerRepository;
}
public void setCustomerRepository(CustomerRepository customerRepository){
this.customerRepository = customerRepository;
}
//part of CustomerService inteface
public boolean customerExists(String user){
return customerRepository.customerExists(user);
}
public Customer getCustomer(String user){
if(“john”.equals(user))
return new Customer(“john”, “12345”);
else
return null;
}
}[/java]
Listing 7-2. Stub repository of Customer model
[java]public class StubCustomerRepository implements CustomerRepository{
public boolean customerExists(String user){
if(“john”.equals(user))
return true;
else
return false;
}
public Customer getCustomer(String user){
if(“john”.equals(user))
return new Customer(“john”, “12345”);
else
return null;
}
}[/java]
Listing 7-3. Stub testing class used for testing the service
[java]public class CustomerServiceTests{
private CustomerService customerService;
@Before
public void setUp() {
customerService = new CustomerServiceImpl(new StubCustomerRepository());
}
@Test
public void customerExistsTest(String user){
assertTrue(customerService.customerExists(“john”));
}
}[/java]
Page Visitors: 5812
Tomcy John
Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
I wanted to put you the little note to finally thank you over again with your wonderful knowledge you’ve documented on this page. This is really shockingly generous of people like you to deliver publicly all most people would’ve made available as an e book to earn some cash for their own end, primarily considering the fact that you might have tried it if you wanted. Those tips likewise acted like the fantastic way to be sure that someone else have similar zeal just as my own to realize many more in regard to this issue. I’m certain there are several more pleasurable sessions up front for people who see your blog.
Valuable information. Fortunate me I found your web site by accident, and I’m shocked why this coincidence didn’t took place earlier! I bookmarked it.
Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I will certainly comeback.
As a Newbie, I am continuously browsing online for articles that can aid me. Thank you
I think this is a real great article post.Really looking forward to read more. Want more.
Thanks, nice post