AntでJUnit 4.10のテストを実行する
antのbuild.xmlで、コンパイルと共にJUnitのテストを実行する方法のメモです。ソースとテストケース
テスト対象のソースのサンプルがこちら。
このクラスの「add」メソッドをテストするケースを想定します。
- package sample;
- public class Main {
- public static void main(String[] args) {
- System.out.println(add(10, 5));
- }
- //引数2つを加えるだけ
- public static int add(int a, int b) {
- return a + b;
- }
- }
テストケースはこんな感じ。
10+5が15になるかテストしています。
- package sample;
- import org.junit.Test;
- import static org.junit.Assert.*;
- import static org.hamcrest.core.Is.is;
- public class MainTest {
- @Test
- public void testAdd() {
- assertThat(Main.add(10, 5), is(15));
- }
- }
ソースはそれぞれ、
src/main/java/sample/Main.java
src/test/java/sample/MainTest.java
に配置し、JUnitのjarファイルは
lib/junit-4.10.jar
に配置しています。
Eclipseからだと、このように見えています。

build.xml
コンパイルとテストを実行し、最終的に実行可能なjarファイルを出力することを想定しています。
jarファイルにMainTest.classを含めたくないので、こんな方針で行きます。
・Main.javaをコンパイルし、buildフォルダに出力する。
・MainTest.javaをコンパイルし、testbuildフォルダに出力する。
(この時、Main.classとjunit.jarにclasspathを通す必要あり)
・JUnitによるテストを実行する。
(この時、Main.classとMainTest.class、junit.jarにclasspathを通す必要あり)
・buildフォルダのclassをjarで固める。
あまりbuild.xmlを書き慣れていないので、ちゃんと流儀に添えているか
微妙ですがこんな感じになりました。
- <?xml version="1.0" encoding="UTF-8" ?>
- <project name="sample" default="dist" basedir=".">
- <description>
- Ant + JUnitの連携サンプル
- </description>
- <!-- Mainのビルドとjarファイルの作成に必要な情報 -->
- <property name="src" location="src/main/java"/>
- <property name="build" location="build"/>
- <property name="dist" location="dist"/>
- <!-- MainTestのビルドに必要な情報 -->
- <property name="test.src" location="src/test/java"/>
- <property name="test.build" location="testbuild"/>
- <property name="test.class.name" value="sample.MainTest" />
- <path id="test.classpath">
- <pathelement location="${build}" />
- <pathelement location="${test.build}" />
- <pathelement location="lib/junit-4.10.jar" />
- </path>
- <target name="init">
- <!-- Create the time stamp -->
- <tstamp/>
- <mkdir dir="${build}"/>
- <mkdir dir="${test.build}"/>
- </target>
- <target name="compile" depends="init" description="compile the source " >
- <!-- Mainをビルドし、buildフォルダに出力 -->
- <javac srcdir="${src}" destdir="${build}" includeAntRuntime="no"/>
- <!-- MainTestをビルドし、testbuildフォルダに出力 -->
- <javac srcdir="${test.src}" destdir="${test.build}" includeAntRuntime="no">
- <classpath refid="test.classpath" />
- </javac>
- </target>
- <target name="test" depends="compile" description="test the source " >
- <!-- JUnitによるテストを実行 -->
- <junit fork="yes" haltonfailure="yes">
- <test name="${test.class.name}" />
- <formatter type="plain" usefile="false" />
- <classpath refid="test.classpath" />
- </junit>
- </target>
- <target name="dist" depends="test" description="generate the distribution" >
- <!-- jarファイルの出力先ディレクトリを作成 -->
- <mkdir dir="${dist}"/>
- <!-- sample-yyyymmdd.jarという名前でjarファイルを作成 -->
- <jar jarfile="${dist}/sample-${DSTAMP}.jar" basedir="${build}">
- <manifest>
- <attribute name="Main-Class" value="sample.Main" />
- </manifest>
- </jar>
- </target>
- <target name="clean" description="clean up" >
- <!-- Delete the ${build} and ${dist} directory trees -->
- <delete dir="${build}"/>
- <delete dir="${test.build}"/>
- <delete dir="${dist}"/>
- </target>
- </project>
javacにincludeAntRuntime="no"をつけていますが、これがないと
warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
という警告が発生しました。
javac(ant) の警告
http://d.hatena.ne.jp/eggtoothcroc/20100526/p1
こちらを参考にさせていただいてます。
Eclipseのantプラグインで実行
antを実行するプラグインは、特にインストールした記憶が無いので、
初期状態で入っていると思います。
build.xmlを右クリックして実行でもよいのですが、
[ウィンドウ]-[ビューの表示]-[Ant]を実行します。

すると、antビューが表示されます。
ビルドファイルの追加ボタンをクリックし、対象のbuild.xmlを指定します。

好きなターゲットを選んで実行することができます。
distを選択して、「選択されたターゲットの実行」をクリック。

コンソールに実行結果が表示されます。
ちゃんとjunitによるテストも行われています。

distに出力されたjarファイルを見てみると、狙い通りMain.classだけ
含まれており、余分なMainTest.classは含まれていません。
※jarにするときに、ファイル名にTestがあるものは含めない。というフィルターでもよいかと。

JUnitエラー時の挙動
Mainクラスで足し算しているところを掛け算に変更し、テストがエラーになるようにしてみます。
- //引数2つを加えるだけ
- public static int add(int a, int b) {
- //間違えて掛け算する
- //return a + b;
- return a * b;
- }
BUILD FAILEDとなり、jarファイルは作成されません。

Buildfile: /home/workspace/junitsample/build.xml
init:
compile:
test:
[junit] Testsuite: sample.MainTest
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.011 sec
[junit] Testcase: testAdd took 0.004 sec
[junit] FAILED
[junit] Expected: is <15>
[junit] got: <50>
[junit] junit.framework.AssertionFailedError:
[junit] Expected: is <15>
[junit] got: <50>
[junit] at sample.MainTest.testAdd(Unknown Source)
BUILD FAILED
/home/workspace/junitsample/build.xml:40: Test sample.MainTest failed
Total time: 764 milliseconds
【参考URL】
JUnit FAQ - How do I run JUnit using Ant?
http://junit.sourceforge.net/doc/faq/faq.htm#running_5
Using Apache Ant - Example Buildfile
http://ant.apache.org/manual/index.html
javac(ant) の警告
http://d.hatena.ne.jp/eggtoothcroc/20100526/p1
コメント
antビルドの話、勉強になります!
2018/09/19 07:31 by 師子乃 URL 編集