Chapter 35. Testing Seam applications

Most Seam applications will require at least two kinds of automated tests: unit tests, which test a particular Seam component in isolation, and scripted integration tests, which exercise all Java layers of the application (that is, everything except the view pages).
Both test types are easily written.

35.1. Unit testing Seam components

All Seam components are POJOs (Plain Old Java Objects), which simplifies unit testing. Since Seam also emphasizes the use of bijection for component interaction and contextual object access, testing Seam components outside their normal runtime environments is very easy.
The following Seam Component which creates a statement of account for a customer:
@Stateless
@Scope(EVENT)
@Name("statementOfAccount")
public class StatementOfAccount {
   
  @In(create=true) EntityManager entityManager
   
  private double statementTotal;
   
  @In
  private Customer customer;
   
  @Create
  public void create() {
    List<Invoice> invoices = entityManager
      .createQuery("select invoice from Invoice invoice where " + 
                   "invoice.customer = :customer")
      .setParameter("customer", customer)
      .getResultList();
    statementTotal = calculateTotal(invoices);
  }
   
  public double calculateTotal(List<Invoice> invoices) {
    double total = 0.0;
      for (Invoice invoice: invoices) {
        double += invoice.getTotal();
      }
      return total;
     }
   // getter and setter for statementTotal
}
We can test the calculateTotal method, which tests the component's business logic, as follows:
public class StatementOfAccountTest { 
  @Test 
  public testCalculateTotal { 
    List<Invoice> invoices = 
      generateTestInvoices(); // A test data generator 
    double statementTotal = 
      new StatementOfAccount().calculateTotal(invoices); 
    assert statementTotal = 123.45; 
  }   
}
Note that we are not testing data retrieval or persistence, or any of the functions provided by Seam. Here, we are testing only the logic of our POJOs. Seam components do not usually depend directly upon container infrastructure, so most unit tests are just as easy.
If you do want to test the entire application, read the section following.