JUnit EE の実行例 (JBoss 3.0.6)

JUnit についてこちらなどで簡単に紹介しましたが、
サーブレットや EJB を対象に JUnit でテストを行うために JUnitEE というものがあります。

そのついてくるサンプルの実行例を見ながら、どんなものかを見ていきましょう。


サンプルの導入

まず、JUnitEE には Ant のタスクがあり、JUnit でのテストの実行機構である TestRunner に相当する TestServlet を含んだかたちでの WAR/EAR ファイルを作成することができます。

サンプルには以下のように ant を実行すると junitee-example.ear という EAR ファイルができますので、これを適切にデプロイします。

JBoss の場合は server/default/deploy にコピーして起動すれば OK です。
以下のようなメッセージが出てきて、デプロイされていることが確認できます。

実行

/examplestest/ をアクセスすると、以下のような画面になります。
「Select test suite(s) to run」に適当にチェックを入れて、テストケースを実行して見てください。

こんな結果が表示されます。

また JUnit は XML で結果を返すことができ、それをデモンストレイトするための /examplestestxml/ という URI もあります。

結果について

テスト対象になっている EJB の一部です。

テストの実行結果で「足し算の結果がおかしい」ということになっていますが、計算を実装しているメソッドを見るとわざとそうなっているのがわかります。

*/
public String addTwoNumbers(String first, String second) throws BadNumberException {
try {
int firstInt = Integer.parseInt(first);
int secondInt = Integer.parseInt(second);

return Integer.toString(firstInt + secondInt - 1); // oops
} catch (NumberFormatException ex) {
throw new BadNumberException(ex.getMessage());
}
}


/**
*/
public double emc2(double m, double c) {
// to demostrate EJBException output in the test result
throw new EJBException("e = mc2");
}
}

こちらがテストを行っている JUnit のクラスです。

/*
* $Id: EinsteinTest.java.html,v 1.1 2002/09/22 22:32:41 o_rossmueller Exp $
* $Source: /cvsroot/junitee/JUnitEE/doc/tutorial/org/junitee/ejb/einstein/test/EinsteinTest.java.html,v $
*/

package org.junitee.ejb.einstein.test;

import java.rmi.RemoteException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import junit.framework.TestCase;

import org.junitee.ejb.einstein.BadNumberException;
import org.junitee.ejb.einstein.Einstein;
import org.junitee.ejb.einstein.EinsteinHome;

/**
*/
public class EinsteinTest extends TestCase
{
/**
* The fixture
*/
protected Einstein ein;

/**
*/
public EinsteinTest(String name) { super(name); }

/**
*/
protected void setUp() throws Exception
{
Context jndiContext = new InitialContext();

Object einRef = jndiContext.lookup("java:comp/env/ejb/EinsteinEJB");
EinsteinHome home = (EinsteinHome)PortableRemoteObject.narrow(einRef, EinsteinHome.class);

this.ein = home.create();
}

/**
*/
protected void tearDown() throws Exception
{
this.ein = null;
}

/**
*/
public void testSimpleAddition() throws RemoteException, BadNumberException
{
String result = this.ein.addTwoNumbers("7", "10");
assertTrue("Result is " + result + " but should be 17", result.equals("17"));
}

/**
*/
public void testMalformedInput() throws RemoteException
{
boolean ok = false;
try
{
String result = this.ein.addTwoNumbers("20", "asdf");
}
catch (BadNumberException ex)
{
ok = true;
}
assertTrue("Accepted bad number 'asdf'", ok);

ok = false;
try
{
String result = this.ein.addTwoNumbers("20a", "5");
}
catch (BadNumberException ex)
{
ok = true;
}
assertTrue("Accepted bad number '20a'", ok);

ok = false;
try
{
String result = this.ein.addTwoNumbers("20a", "d5");
}
catch (BadNumberException ex)
{
ok = true;
}
assertTrue("Accepted bad numbers '20a' and 'd5'", ok);
}


/**
*/
public void testEmc2() throws RemoteException, BadNumberException
{
double result = ein.emc2(2.998, 1.998);
assertTrue("Result is " + result + " but should be 11.96802799", result == 11.96802799);
}
}

[Top Pageに戻る]

給料前でお金がない・・ 給料前でお金がない・・ 給料前でお金がない・・
[PR] | 店舗設計韓国食材債務整理川越蒲田古河代官山ESTA 申請 日本語中国SEO対策消費者金融車 買取テンプレート沖縄旅行免許合宿二輪引越しプレゼントゴルフ会員権留学レーシックマッサージFXアフィリエイトFXホームページ制作デイトレードハワイ旅行タイバンコクハワイ レンタカーベスト ハワイ ホテル レーツバリ島Hawaii hotelsHawaii Activitiesbhhrハワイホテルテキスト広告
【運営会社「パラダイムシフト」サービス】 ハワイ現地オプショナルツアーリラックマ) - ビジネスクラス航空券 - 格安航空券(1) - 格安航空券(2) - 海外ホテル - 韓国旅行 - タイムシェア - ホテル 予約
無料ホームページ - 携帯ホームページ - 無料ホームページ作成 - レンタルサーバー - ブログ - ヴィラ - ハワイ コンドミニアム - バリ島 ホテル - プーケット ホテル - レップチェッカー - 旅行情報 - 格安国際電話 - ホノルルマラソン - サイトパトロール - 誹謗中傷 - 宿泊料金比較 - ノースウエスト航空 マイル - クチコミ