** JUnitによるユニットテスト [#g8fba70c]
#setlinebreak(on);
JUnitによるユニットテストの方法を記載する。
テストファーストとする為、ここでは先にテストコードを作成する。
(1) 前準備
1.1 junit.jar をビルドパスに追加する。
・対象のプロジェクトを右クリック -> [プロパティ] -> [Javaのビルドパス] -> [ライブラリ]を選択。
・[ライブラリの追加] で JUnit を追加する。
※JUnitがない場合は junitプラグインをダウンロード&インストールしてから上記の操作を行う。
複数人プロジェクトで使用する場合は lib に放り込んでおきつつ、CLASSPATHに含めておくといいかも。
1.2 テストコード格納用のソースフォルダを作成する
・[新規] -> [ソースフォルダ] ※ここではフォルダ名を test とする
1.3 テストクラスの出力先を設定する(実際のソースとテストコードを分ける為)
・プロジェクトを右クリック -> [プロパティ] -> [Javaのビルドパス] -> [ソース] を選択
・[ソースフォルダ毎に出力フォルダの指定を可能にする] をチェックする。
・テストコード用のフォルダ展開して、出力フォルダーをテストコード用のフォルダと同じ所に設定する。
※これでテストコードのクラスがコンパイルされると、テストコード用のフォルダに出力されるようになる。
#html(<table><tr><td style="padding-left:20px;"> </td><td align="top" style="padding-top:0px;">)
&ref(junit_init.jpg,nolink);
#html(</td></tr></table>);
1.4 ファイル構成
・順番が前後するが、最終的に作成するファイルの構成は下図のようになる。※test_junitはプロジェクト名
#html(<table><tr><td style="padding-left:20px;"> </td><td align="top" style="border:1px solid #000000;padding-top:0px;">)
&ref(junit00.jpg,nolink);
#html(</td></tr></table>);
(2) テスト対象クラス仕様の確認
テスト対象となるクラスの仕様は以下の通りとする。
|パッケージ名|クラス名|メソッド名|引数|処理内容|h
|util|MyMath|add|int a, int b|a + b を返す|
|~|~|sub|int a, int b|a - b を返す|
|~|~|mul|int a, int b|a * b を返す|
|~|~|div|int a, int b|a / b を返す&br;0割りされた場合はExceptionでなく0を返す|
(3) 空のテスト対象クラスを作成する
package util;
public class MyMath {
/*
* 足し算
*/
public static int add(int a, int b){
return 0;
}
/*
* 引き算
*/
public static int sub(int a, int b){
return 0;
}
/*
* 掛け算
*/
public static int mul(int a, int b){
return 0;
}
/*
* 割り算
*/
public static int div(int a, int b){
return 0;
}
}
※エラー確認を行う為に、とりあえず全て0を返すように。
(4) テストコードの作成と対象クラスの修正
4.1 テストクラスの作成
1. [MyMath.javaを右クリック] -> [新規] -> [JUnitテスト・ケース]
2. ソースフォルダをテストコード用のフォルダに変更して [次へ]
3. テストしたいメソッドにチェックを入れて[終了]
4.2 テストクラスを下記のようにコーディングする。
見ての通り、例えば '''メソッドA''' のテストコードは、 test'''メソッドA''' というメソッドに記述する。
※Java1.5対応のJUnitであれば、この名前規則に従う必要はなく @Test というアノテーションを記述すれば良い。
package util;
import junit.framework.TestCase;
/*
* MyMathのテストケース
*/
public class MyMathTest extends TestCase {
public MyMathTest(String arg0) {
super(arg0);
}
/**
* 足し算のテスト
*/
public void testAdd() {
int res1 = MyMath.add(1,2);
assertEquals(3, res1);
int res2 = MyMath.add(0,0);
assertEquals(0, res2);
int res3 = MyMath.add(1,-4);
assertEquals(-3, res3);
}
/**
* 引き算のテスト
*/
public void testSub() {
int res1 = MyMath.sub(1,2);
assertEquals(-1, res1);
int res2 = MyMath.sub(0,0);
assertEquals(0, res2);
int res3 = MyMath.sub(1,-4);
assertEquals(5, res3);
}
/**
* 掛け算のテスト
*/
public void testMul() {
int res1 = MyMath.mul(1,2);
assertEquals(2, res1);
int res2 = MyMath.mul(0,0);
assertEquals(0, res2);
int res3 = MyMath.mul(1,-4);
assertEquals(-4, res3);
int res4 = MyMath.mul(-2,-4);
assertEquals(8, res4);
}
/**
* 割り算のテスト
*/
public void testDiv() {
int res1 = MyMath.div(6,2);
assertEquals(3, res1);
int res2 = MyMath.div(10,6);
assertEquals(1, res2);
int res3 = MyMath.div(0,10);
assertEquals(0, res3);
int res4 = MyMath.div(10,0);
assertEquals(0, res4);
}
}
(5) テストの実行
5.1 とりあえず一回テストを実行してみる。
・MyMathTest.java を右クリック -> [実行] -> [JUnitテスト]
#html(<table><tr><td align="top" style="padding-left:20px;padding-top:0px;">[実行結果](MyMath が、まだきちんと実装されていないので、当然コケル。)<br/>)
&ref(junit01.jpg,nolink);
#html(</td></tr></table>);
5.2 MyMathの各メソッドを下記のように実装して、再度テストを実行。
package util;
public class MyMath {
/*
* 足し算
*/
public static int add(int a, int b){
return a + b;
}
/*
* 引き算
*/
public static int sub(int a, int b){
return a - b;
}
/*
* 掛け算
*/
public static int mul(int a, int b){
return a * b;
}
/*
* 割り算
*/
public static int div(int a, int b){
return a / b;
}
}
#html(<table><tr><td style="padding-left:20px;padding-top:0px;">[実行結果]<br/>)
&ref(junit02.jpg,nolink);
#html(</td></tr></table>);
5.3 testDivでコケている事がわかるので、障害トレースに表示された情報を元に、
MyMathのdivメソッドを下記のように修正して再度、テストを実行。
/*
* 割り算
*/
public static int div(int a, int b){
if (b == 0){
return 0;
} else {
return a / b;
}
}
#html(<table><tr><td style="padding-left:20px;padding-top:0px;">[実行結果]<br/>)
&ref(junit03.jpg,nolink);
#html(</td></tr></table>);
エラーがなくなった。