こちらの Customer.java を例にとって JUnit の簡単な実行例を見ていきましょう。
実行するにはやはり Ant を使用するのが簡単です。
Ant の使い方については以前の Ant の簡単な紹介(こちらとこちら)を見てください。
junit3.8.1 ディレクトリから junit.jar を Ant の lib ディレクトリ (ANT_HOME に設定したディレクトリの lib サブディレクトリ) にコピーします。

Customer.java は test ディレクトリの下で、パッケージ test の中の Customer クラスとしています。
また、その test.Customer クラスをテストする JUnit を使用したクラスは testSuites ディレクトリの下で、パッケージ testSuites に CustomerTest というクラスになります。

test.Customer です。前回のものからパッケージの指定を追加してあります。
package test;
import java.io.Serializable;
public class Customer implements Serializable
{
String custno = "";
String custname = "";
String addr = "";
public Customer () {
}
public String getCustno () {
return custno;
}
public void setCustno (String newCustno)
{
custno = newCustno;
}
public String getCustname () {
// System.out.println(custname);
return custname;
}
public void setCustname (String newCustname)
{
custname = newCustname;
// System.out.println(custname);
}
public String getAddr () {
return addr;
}
public void setAddr (String newAddr)
{
addr = newAddr;
}
}
testSuites.CustomerTest です。
Customer クラスに対しての直接のテスト実行クラスになります。
TestCase クラスを拡張し、testCustomer の中でテストを実行しています。
テストはたいへん簡単なもので、値を set して get の結果を assertEquals で比較している、というものです。
package testSuites;
import junit.framework.*;
import test.*;
import testSuites.*;
public class CustomerTest extends TestCase {
public CustomerTest(String name) {
super(name);
}
public void testCustomer() {
Customer cust = new Customer();
cust.setCustname("テスト");
assertEquals("テスト", cust.getCustname());
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new CustomerTest("testCustomer"));
return suite;
}
}
testSuites.AllTest になります。
今回は CustomerTest だけですが、いろいろなテストを追加してまとめて実行することができます。
package testSuites;
import junit.framework.*;
import testSuites.*;
public class AllTest extends TestCase {
public AllTest(String name) {
super(name);
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(CustomerTest.suite());
return suite;
}
}
build.xml はこんなかんじです。
今気がつきましたが junit.lib.dir というプロパティはこの中では使っていないので、関係ないですね。

本質的に言うと以下の部分だけあれば OK です。
haltonfailure はテストに失敗した時に処理を停止させるかどうかの設定です。
デフォルトは"false"なので別に指定しなくてもいいのですが、"true"にして確かめて見たりするのもいいでしょう。
test nane= でテストを行うクラスを指定します。
<target name="runtests" depends="compile">
<junit haltonfailure="false">
<formatter type="plain"/>
<classpath>
<pathelement location="${builddir}"/>
</classpath>
<test name="testSuites.AllTest"/>
</junit>
<echo>All Test complete!</echo>
</target>
個別にテストを実行できるようにテストクラスをプロパティとして設定することができます。
何も指定されないときのために test.properties というファイルに以下のようにファイルを指定して実行させるようにすれば OK です。
test.properties ファイルで以下のようにデフォルトで実行するクラスを指定します。
defaultTestClass=testSuites.AllTest
build.xml の一部分です。
<property file="test.properties"/>
<target name="preparetests" unless="testclass">
<property name="testclass" value="${defaultTestClass}"/>
</target>
<target name="runtests" depends="compile">
<junit haltonfailure="false">
<formatter type="plain"/>
<classpath>
<pathelement location="${builddir}"/>
</classpath>
<test name="${testclass}"/>
</junit>
<echo>All Test complete!</echo>
</target>
改めてこちらが build.xml の全文です。
<?xml version="1.0"?>
<project name="JUnitTest" default="runtests">
<property file="test.properties"/>
<property name="junit.lib.dir" value="c:\junit3.8.1"/>
<property name="sourcedir" value="."/>
<property name="builddir" value="build/classes"/>
<target name="-preparetest" unless="testclass">
<property name="testclass" value="${defaultTestClass}"/>
</target>
<target name="init">
<mkdir dir="${builddir}"/>
<echo>init complete!</echo>
</target>
<target name="compile" depends="init">
<javac srcdir="${sourcedir}"
destdir="${builddir}"
/>
<echo>compile complete!</echo>
</target>
<target name="runtests" depends="compile, -preparetest">
<junit haltonfailure="false">
<formatter type="plain"/>
<classpath>
<pathelement location="${builddir}"/>
</classpath>
<test name="${testclass}"/>
</junit>
<echo>All Test complete!</echo>
</target>
<target name="clean" depends="init">
<delete dir="${builddir}"/>
<delete>
<fileset dir="." includes="TEST-*"/>
</delete>
<echo>delete complete!</echo>
</target>
</project>
実行例です。

testclass プロパティを ant 実行時に指定した場合の実行例です。

以下のように"TEST-[テストクラス名].txt"でテスト結果が出力されます。
上の build.xml の clean ターゲットはこのファイルも消すように変更しています。

内容はこんなかんじです。

|
|