Sunday, March 22, 2015

Configure testing of your web application using Jenkins

Purpose : To demonstrate the automation of testing of a web application after each build.
Assumptions : 1. The application build is already configured on Jenkins. 
                         2. We have a service method that runs all test cases.

I have a web application which is scheduled to build via Jenkins. Now I need to test the application after each build. The following steps can be used to configure test cases :

1. Create a main class that runs all test cases

If you are going to run the methods of your web application, then need to initialize the specific mappings before running the test cases. For example spring dependencies have to initialized before using them. During application build it is taken care by your web server. But here we are going to run as a java main class, so this step is necessary.

public class TestProcessor {
    static BeanFactory factory=null;
    static SessionFactory sessionFactory = null;
     * Initialized the Spring/Hibernate bean mappings
     * @param filePath
    public static void initialize(String filePath) {
        Resource res = new FileSystemResource(filePath);
        factory = new XmlBeanFactory(res);
        sessionFactory = (SessionFactory) factory.getBean("sessionFactory");

    public static void openSession(){
        Session s = sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
        //"loading test data for running JUnits");

     * This is the main class to run all test cases
     * Created for mapping to jenkins 
     * @param args
    public static void main(String[] args) {
        try {
            boolean testResult = ((TestService) getBean("testService")).runAllTests();
        } catch (Exception e) {



2. Create maven profile to run the above java main method

You can create a maven profile to run the above test method. If your web application is connecting to a database then make sure you add the oracle dependency to the profile. Also servlet api is something taken care of by web server. But as we are running this main class separately we need to add it as a dependency. Create a profile in pom.xml as below



3. Specify the build name in Jenkins

As I want to configure the test case after each build, I would specify it in a post build step. We choose an execute shell option as below :


And then configure execution of test case as below 

Note : There can be multiple ways to implement the same depending on the implementation of your web application. Further suggestions/improvement to this post are most welcome.

No comments:

Post a Comment