Software Development

When to replace Unit Tests with Integration Test

Its been a while I was thinking about integration vs unit testing. Lots of googling, questions in stack overflow and looking in many books. In this post I would like share with you the information I have found and what decision I arrive at this.

I think if you put this question to any Java developer, you will get an answer supporting the unit test instantaneously. They will tell you that if you can not do unit testing, the unit of code you have written is large and you have to bring it down to multiple testable unit. I have found these questions in Stack Overflow

  1. Integration Testing best practices
  2. Integration vs Unit Testing
  3. Junit: splitting integration test and Unit tests
  4. What is an integration test exactly?

useful in getting a good understanding of both terminologies.

Unit testing is widely followed in Java domain with most of the projects now also enforcing the code coverage and unit testing. Integration testing is relatively new and misunderstood concept. Even through it is practiced much less than, unit testing there can be various uses of it. Lets take a simple user story and revisit both.

Lets consider it with a user story

User will give an INPUT in a UI form and when the submit button is clicked, the PROCESSED output should be shown in the next page. Since the results should be editable, both INPUT and OUTPUT should be saved in the database.

Technical Design

Lets assume that our application will grow rapidly in the future and we will design it now keeping that in mind.

As shown in the above image it is a standard 4-tier design with view, service, dao. It has a application layer which contains the logic of how to convert the input into output. To implement the story we wrote 5 methods(1 in service, 2 dao to save input and output, 1 contains the business and 1 method in view layer to prepare the input.)

Writing some Unit test cases

If we were to unit test the story, we need to write 5 tests. As the different layers are dependent, we might need to use mock objects for testing. But apart from one method in application layer that does the original process, is there any need to unit test the other part? For example the method,

public void saveInput(Input input){  
           Session session = sessionFactory.getCurrentSession();  
           session.save(input);  
 }

When you unit test this, you will typically use a mock object of sessionFactory and the code will always work. Hence I don’t see much point in wiring a unit test here. If you observe carefully, apart from the application layer, all the other methods will be similar to what we have discussed.

What can we achieve with integration test?

Read here as a heads up for the integration test. As we have seen most of the unit tests for this story were not effective. But we can’t skip testing as we want to find out the code coverage and make our code self testing. According to Martin flower in his article about Continuous Integration you should write the code that can test the other code. I fell the good integration tests can do this for you.

Lets write a simple integration test for this situation,

@Configurable(autowire = Autowire.BY_NAME)   
 @RunWith(SpringJUnit4ClassRunner.class)   
 @ContextConfiguration(locations = {"classpath:applicationContext.xml"})  
 public class SampleIntTest {  
      @Autowired  
      private SamService samService;  
      @Test  
      public void testAppProcessing(){  
      Input input = new Input();  
      //prepare input for the testing.  
      Output output = samService.processInputs(input);  
      //validate outpt gainst the given input.  
      if(!isValidate(input,output)){  
           Assert.fail("Validation failed!");  
      }  
 }

I have skipped the view layer here as it not so relevant. We are expecting the INPUT from the UI in a bean Input and that’s that we will have in the service layer. Here, With this few line of coding, you can achieve the full functionality of the application from the service layer.

It is preferred to use a in-memory database like H2 for integration tests. If the table structure is complicated it may not be possible in that case,you can use a test instance of DB and prepare a script to delete all the data and run it as part of the test so that the DB state is restored back. This is important because in the next run the same data will be saved again.

Another advantage of integration tests is that if you are changing the implementation the test need not be changed because it is concerned with the input and output.This is useful in refactoring the code without change in the test code. Also, you can schedule these tests to measure the stability of the application and any regression issues can be caught early.

As we have seen, integration test are easy to write and are useful, but we need to make sure it does not replace the unit testing completely. When ever there is a rule base system(like the LogicalProcesser in our case) it should be mandatory because you can’t cover all the scenarios with the integration test.

So as always JEE is about making choice asnd sticking to it. For the last few months we were practicing it in our teams and it is really going well. Currently we made integration test mandatory and unit tests and optional. Always review the code coverage and make sure you get good coverage in the core of the system(the LogicalProcesser in our case).

Reference: When to replace Unit Tests with Integration Test from our JCG partner Manu PK at the “The Object Oriented Life” Blog.

Related Articles :

Manu PK

Manu develops software applications using Java and related technologies. Geek, Tech Blogger, open source and web enthusiast.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button