統合テストはもう少し複雑です。 コンテナのインフラストラクチャは除外することはできませんが、 自動テストを実行するためにわざわざアプリケーションサーバーへアプリケーションをデプロイしたいとも思わないでしょう。 そこで、 最低限必要なコンテナのインフラストラクチャをテスト環境に再現し、 性能を大きく損なうことなくアプリケーション全体を実行可能にする必要があります。
Seam により JBoss Enbedded のコンテナを使ってコンポーネントをテストすることができます。 詳細は「設定」の章を参照してください。 最小限のコンテナ内でアプリケーションを完全に実行するテストの記述が可能です。
public class RegisterTest extends SeamTest {
@Test
public void testRegisterComponent() throws Exception {
new ComponentTest() {
protected void testComponents() throws Exception {
setValue("#{user.username}", "1ovthafew");
setValue("#{user.name}", "Gavin King");
setValue("#{user.password}", "secret");
assert invokeMethod("#{register.register}").equals("success");
assert getValue("#{user.username}").equals("1ovthafew");
assert getValue("#{user.name}").equals("Gavin King");
assert getValue("#{user.password}").equals("secret");
}
}.run();
}
...
}
public class RegisterTest extends SeamTest {
@Test
public void testRegisterComponent() throws Exception {
new ComponentTest() {
protected void testComponents() throws Exception {
setValue("#{user.username}", "1ovthafew");
setValue("#{user.name}", "Gavin King");
setValue("#{user.password}", "secret");
assert invokeMethod("#{register.register}").equals("success");
assert getValue("#{user.username}").equals("1ovthafew");
assert getValue("#{user.name}").equals("Gavin King");
assert getValue("#{user.password}").equals("secret");
}
}.run();
}
...
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
統合テスト環境では使用できないリソースを必要とする Seam コンポーネントの置き換えが必要な場合があります。 たとえば、 支払処理システムのファサードに次の Seam コンポーネントを使用するとします。
@Name("paymentProcessor")
public class PaymentProcessor {
public boolean processPayment(Payment payment) { .... }
}
@Name("paymentProcessor")
public class PaymentProcessor {
public boolean processPayment(Payment payment) { .... }
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
統合テストの場合、 次のようなモックコンポーネントを作成します。
@Name("paymentProcessor")
@Install(precedence=MOCK)
public class MockPaymentProcessor extends PaymentProcessor {
public boolean processPayment(Payment payment) {
return true;
}
}
@Name("paymentProcessor")
@Install(precedence=MOCK)
public class MockPaymentProcessor extends PaymentProcessor {
public boolean processPayment(Payment payment) {
return true;
}
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
MOCK の優先度はアプリケーションコンポーネントのデフォルト優先度より高くなるため、 モック実装がクラスパスにあるときは必ず Seam はそれをインストールします。 実稼働環境にデプロイする場合はモック実装は存在しないので、 実際のコンポーネントがインストールされます。