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);
}
}
|
|||
|
|