第35章 Seam アプリケーションのテスト
Seam アプリケーションのほとんどは少なくとも 2 種類の自動テストが必要です。 個々の Seam コンポーネントを独立してテストする ユニットテスト と、 アプリケーションのすべての Java 層 (ページの表示を除きすべて) を実行するスクリプト化された 統合テスト です。
どちらのテストも簡単に作成できます。
35.1. Seam コンポーネントのユニットテスト リンクのコピーリンクがクリップボードにコピーされました!
リンクのコピーリンクがクリップボードにコピーされました!
Seam コンポーネントはすべて POJO (純粋な旧式 Java オブジェクト) であり、 ユニットテストを簡略化します。 また Seam はコンポーネント間でのやり取りやコンテキスト依存オブジェクトへのアクセスにバイジェクションを多用しているので、 通常のランタイム環境でなくてもとても簡単に Seam コンポーネントをテストすることができます。
次のような顧客アカウントのステートメントを作成する Seam コンポーネントを考えてみましょう。
@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
}
以下のように、 コンポーネントのビジネスロジックをテストする
calculateTotal メソッドをテストすることができます。
public class StatementOfAccountTest {
@Test
public testCalculateTotal {
List<Invoice> invoices =
generateTestInvoices(); // A test data generator
double statementTotal =
new StatementOfAccount().calculateTotal(invoices);
assert statementTotal = 123.45;
}
}
データの取り出しや永続化、 また Seam が提供する機能をテストしているわけではない点に注意してください。 ここでは POJO のロジックのみをテストしてます。 Seam コンポーネントは通常はコンテナのインフラストラクチャには直接依存しないため、 ほとんどのユニットテストは簡単です。
アプリケーション全体をテストする場合は、 この後の項をお読みください。