Shashank Moghe
Nov 5 · 3 min read

Automate Integration Test Cases

Spring + In-memory DB(H2) + Junit + Mustache templates

Most of the times verifying the output from the large files/reports is done using manual approach.Automating such verification can be easily achieved by loading the data in in-memory DB and having a query based approach to verify.

For the integration test environments like Jenkins, you can configure a test job which can be executed in the pipeline as soon as component finishes execution.Such test job can verify the output before executing next steps of execution.This can be an efficient way of automating integration test cases.

Refer below steps to create a Java project and execute the tests in a Runnable manner:

  1. Create a maven based Spring boot project having support for h2 in-memory Java DB:

2.Configure h2 connection parameters in application.properties in resources.

3.Create a Junit test suite having test cases leveraging data loaded in h2 DB.Initial table creation and data loading in h2 can be done in run method of Springboot class using Spring JDBC/Data.

4.To execute all the test cases using a Runnable Jar(which could be a requirement for Integration test setup e.g Jenkins), JUnitCore APIs can be used from a mainClass of the project.

5.To create a report of the test execution, Mustache APIs can be used using a custom html template.

6.Build the project using mvn clean package spring-boot:repackage command and run using java -jar command.Sample test report:

Conclusion:

This could be a well suited approach for Java based projects wherein existing product java APIs can also be leveraged to get the expected output for the tests as per the business logic.Verification by writing SQL queries becomes far more easy job to automate significant number of test cases.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade