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 :
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.
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");
openSession();
}
public static void openSession(){
Session s = sessionFactory.openSession();
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));
//logger.info("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) {
initialize("applicationContext.xml");
try {
boolean testResult = ((TestService) getBean("testService")).runAllTests();
} catch (Exception e) {
e.printStackTrace();
}
}
}
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
<profiles>
<profile>
<id>myTest</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>com.gv.test.TestProcessor</mainClass>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
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