CI Tools and Best Practices in the Cloud

Continuous Integration

Subscribe to Continuous Integration: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Continuous Integration: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Continuous Integration Authors: Derek Weeks, Elizabeth White, PagerDuty Blog, Pat Romanski, Jason Bloomberg

Related Topics: Java EE Journal, Apache Web Server Journal, XML Magazine, Eclipse Platform, Continuous Integration

J2EE Journal: Article

An Introduction to Maven - Part III

Application development management using Maven 2 and Eclipse

Note that the 'getEmployeeInfo' method internally calls 'getConnectionFactory' method to get a reference to the actual connection factory. The 'getConnectionFactory' is a protected method, as shown below, and helps in making the bean testable.

protected ConnectionFactory getConnectionFactory() throws Exception {
     Context context = new InitialContext();
     return (ConnectionFactory) context.lookup
   (connectionFactoryJNDIName);
}

Running JUnit Tests Using Maven
We'll create a JUnit test case for 'GetEmployeeInfoBeanTest' under the 'src/test/java' directory with one 'testGetEmployeeInfo' test method as shown below:

public class GetEmployeeInfoBeanTest extends TestCase {
   public void testGetEmployeeInfo() throws Exception {
     String employeeId = "1000";
     GetEmployeeInfoBean employeeInfoBean = new GetEmployeeInfoBean() {
       protected ConnectionFactory getConnectionFactory() throws Exception {
     EmployeeInfoSPIManagedConnectionFactory managedConnectionFactory = new
     EmployeeInfoSPIManagedConnectionFactory();
       return (ConnectionFactory)managedConnectionFactory.createConnectionFactory();
     }
       };
     Employee employee = employeeInfoBean.getEmployeeInfo(employeeId);
     assertNotNull( employee );
     assertTrue( employee.getId().equals(employeeId) );
   }
}

To run the test cases using Maven, right-click on the module's POM file in Eclipse and then in 'Run As' options, select 'Maven2 test.' Maven will compile the Java files found in 'src/test/java' and execute JUnit test cases. The result of test case execution can be found in the console.

Skipping Maven Test Execution
Maven will execute test cases whenever a 'test' phase or any other phase that occurs after a 'test' phase in the Maven lifecycle management is invoked. For example, Maven will execute test cases when a Maven 'package' or 'install' phase is invoked. Maven does this by triggering 'maven-surefire-plugin' mojo at the time of the 'test' phase. This plug-in is responsible for executing the JUnit test cases found in the 'test-classes' directory in the project target build directory.

Test case execution can be explicitly skipped for compelling reasons. There are a couple of ways to make Maven skip test execution. One way is to explicitly include 'maven-surefire-plugin' in the POM file and set the value of the 'skip' configuration element to 'true' as shown here:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
       <skip>true</skip>
    </configuration>
       </plugin>

If Maven is executed at the command line, test case execution can be skipped by setting the 'maven.test.skip' system property to 'true' as shown below:

mvn -Dmaven.test.skip=true install

Test cases can also be selectively skipped. This can be done by including 'maven-surefire-plugin' in the POM and explicitly specifying test classes using wildcard patterns in the 'excludes' configuration element as shown in the following snippet:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
       <excludes>
          <exclude>**/*FunctionTest.java</exclude>
       </excludes>
    </configuration>
</plugin>

For more information on 'maven-surefire-plugin,' see the plug-in documentation at http://maven.apache.org/plugins/maven-surefire-plugin/index.html.

Reporting Source Code Test Coverage Using Maven
Code coverage is a measure of how thoroughly test cases exercise the main application source code. This is an indirect way of measuring the quality of the tests. Code coverage is a type of white-box testing as logical test assertions are made against the internals of our classes and not against other subsystems or components the current application may interact with. Code coverage helps to isolate logical paths in the source code that aren't exercised under the test.

There are many tools to measure code coverage. Cobertura is an open source tool that can be used to measure test coverage. Cobertura instruments the code base and tracks lines of code that are being executed and lines that aren't being executed as the test suite runs. Besides identifying untested code, Cobertura can also optimize code by flagging dead and unreachable code. Cobertura has a Maven plug-in that can be configured in the POM. The plug-in has a set of goals that can be invoked separately. For more information on Cobertura Maven plug-ins, refer to plug-in documentation at http://maven-plugins.sourceforge.net/maven-cobertura-plugin/. We'll add the following plug-in configuration to the 'ejb' module POM.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.0</version>
    <executions>
       <execution>
          <goals>
             <goal>cobertura</goal>
          </goals>
       </execution>
    </executions>
       </plugin>


More Stories By Murali Kashaboina

Murali Kashaboina leads Enterprise Architecture at United Airlines, Inc. He has 15+ years of enterprise software development experience utilizing a broad range of technologies, including JEE, CORBA, Tuxedo, and Web services. Murali previously published articles in WLDJ and SilverStream Developer Center. He has master’s degree in mechanical engineering from the University of Dayton, Ohio.

More Stories By Geeth Narayanan

Geeth Narayanan is a senior architect at Ecommerce Technology, United Airlines, Inc. He has 10 years of experience in the IT industry, specializing in solutions using Java EE technologies. Geeth has master's degree in electrical engineering from the University of Toledo, Ohio.

Comments (6) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Aladin SOHAILI 10/06/07 04:38:08 PM EDT

Any link to download source files ?

Bob Arnold 09/11/07 02:34:39 PM EDT

Regarding:

"It's time to add connector implementation Java classes. The source files that we developed for the connector are shown in Figure 4.
Download and review the source files to understand the complete implementation."

Please specify the download link for the source code referred to.

vinny 09/04/07 08:40:13 AM EDT

Where is the link to download source files for connector EmployeeInfoCCIConnection??

Magne 09/04/07 03:25:58 AM EDT

It was with great interest I read these articles.
Article 3 refers to source files available for download.
Where can they be downloaded form?

-magne

Duty Editor 08/31/07 01:16:44 PM EDT

The Links to Parts I and II are to be found at the foot of the final page.

-Duty Editor

Jim 08/31/07 11:51:57 AM EDT

Next time you write part 3 of a three-part series, please include clickable links to the first two parts right there at the top.
Unless, of course, you want to keep the first two parts secret.