Ce contenu n'est pas disponible dans la langue sélectionnée.

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
}
Copy to Clipboard Toggle word wrap
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; 
  }   
}
Copy to Clipboard Toggle word wrap
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.
Retour au début
Red Hat logoGithubredditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance. Découvrez nos récentes mises à jour.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez le Blog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

Theme

© 2025 Red Hat