Spring Book – Chapter 7 – Testing

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

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

6 thoughts on “Spring Book – Chapter 7 – Testing

  1. 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.

  2. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *