Taylor

Run JUnit Tests

From Taylor

Various types of JUnit tests are generated: Jpa, Ejb3, Jsf/Seam, Bpm, Pageflow, etc.

taylor-jboss-embedded is used to initialize the embedded JBoss and Seam containers using the generated descriptors. This library provides the ContainerTestCase and Bootstrap classes to simplify initialization. NOTE: taylor-jboss-embedded contains a port of the Seam TestNG classes to JUnit.

Additional base test classes are provided by taylor-commons, such as JpaCrudTest and SeamCrudTest.

You can run and debug the code from within Eclipse and you can run them with Maven.

The first step is to tweak the test code. Some default test implementations where created. So you will need to tweak the test methods. You will also need to add negative tests. Don't forget to use the @NOT generated tag or no tag at all.

Contents

JPA Project

A JUnit test is generated for each Entity Bean in the jpa project.

  • Implement the initData() method to initialize the database with related data.
  • Implement the prePersist() method to initialize the entity.
public class TicketTest extends JpaCrudTest<Ticket> {
...
    /** @generated */
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTest(new TicketTest("testCrud"));
        return new Bootstrap(suite);
    }   
...
    /** @generated */
    protected void initData(EntityManager em) throws Exception {
    }

    /** @generated */
    protected void prePersist(EntityManager em) throws Exception {
	entity = new Ticket();
	entity.setTitle("Test Ticket");
	entity.setOwner("user");
    }    
...
}

EJB Project

A JUnit test is generated for each service interface in the ejb project. The order that the operations are defined in the interface dictates the order in which the test methods are executed. (See the Modify Code section for more on tweaking the code)

There is one method that is generated that is intended to be changed. This is the create() method used to initialize the entity bean that is being tested. For a basic service with default save, get, query, and delete operation, this is the only method that would need to be implemented for a positive test case.

public class TicketServiceTest extends ContainerTestCase {
...
    /** @generated */
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTest(new TicketServiceTest("testSave"));
        suite.addTest(new TicketServiceTest("testGet"));
        suite.addTest(new TicketServiceTest("testQuery"));
        suite.addTest(new TicketServiceTest("testDelete"));
        
	return new Bootstrap(suite);
    }   
...
    private void create() {
        entity = new Ticket();
        entity.setTitle("service test ticket");
        entity.setOwner("user");
    }
...    
}
  • TODO - bpm tests

JSF Project

A Seam junit test is generated in the Jsf project for each entity. These tests verify that the Finder and Editor components are working properly.

  • You might need to tweak the default values for properties.
public class TicketSeamTest extends SeamCrudTest<Ticket> {
...
    /** @generated */
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTest(new TicketTest("testCrud"));
	return new Bootstrap(suite);
    }   
...
    /** @generated */
    protected void initData(EntityManager em) throws Exception {
...
    }    
...
}
  • TODO - pageflow tests

QA Project

A FaceletRenderTest is generated in the QA project. This test uses Cargo to start the JBoss server and JSFUnit to execute each facelets page.

This is a basic so-called idiot test that simply opens every page to ensure that they do not blow up on first entry, but does no functional testing.

The base test class is found in taylor-results.

public class FaceletRenderTest extends BaseFaceletRenderTest {

	/** @generated */
	public FaceletRenderTest(String name) {
		super(name);
	}

	/** @generated */
	public static Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTest(new FaceletRenderTest("testTicket"));
		return suite;
	}

	/** @generated */
	public void testTicket() throws Exception {		
		super.doTest("Ticket");
	}
}
  • Run these tests from the command line using mvn clean install -Ptest
    • NOTE the use of the -Ptest maven profile
    • NOTE this will download a jboss server zip from the Taylor Maven repository. So this will take extra overhead the first time.
  • Additional functional tests can be added to the QA project using Taylor Results.

Loading Test Data

Tests will be run with an HSQLDB database that needs to be initialize at the beginning of each test run. The net.taylor.seam.ImportInit component handles this.

  • Create data online and then export to <project>-qa\src\main\webapp\WEB-INF\classes\data\*.xml
  • Configure <project>-qa\src\main\webapp\WEB-INF\classes\net.taylor.seam\ImportInit.component.xml with names of files to import

Descriptors

Various descriptors are generated under src/test/resources to initialize the test container.

Logging

A log4j.xml file is generated in src/test/resource and will create a target/test.log file.

In addition, several aop trace interceptors have been configured in the ejb-jar.xml and orm.xml deployment descriptors that will handle most of your logging needs.

Code Coverage

The Cobertura Test Coverage Maven Report is generated into the project's pom.xml. Use this report to ensure you are getting enough test coverage.

To run the report type this at the command line:

   mvn clean site

Maven Tips

  • Run a single test
mvn test -Dtest=TestName
  • Skip tests
mvn clean install -Dmaven.test.skip=true
  • pom.xml tweaks
    • change skip to true if necessary (not recommended)
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
               <skip>false</skip>
            </configuration>
         </plugin>		  
  • Run QA integration tests with Cargo
mvn clean install -Ptest
  • Use JUnitPerf for basic performance testing

Next >>