JUnit4 使ってみた

とりあえずサンプルを書いてみた。
まずはテストする対象のソース。

/**
 *
 */
package sample4;
/**
 * @author ssiscirine
 *
 */
public class Sample4 {
	public String method0() {
		return "method0";
	}
	public String method1(String param1) {
		return "method1";
	}
	public String method2(String param1, int param2) {
		return "method2(String param1:\"" + param1 + "\", int param2:" + param2 + ")";
	}
	public String method2(int param1, String param2) {
		return "method2(int param1:" + param1 + ", String param2:\"" + param2 + "\")";
	}
}

そしてJUnit用のソース。
ベースはテスト用のフォルダで右クリック⇒新規⇒その他…⇒Java⇒JUnit⇒Junitテスト・ケースで、作成するスタブのチェックを入れ、テスト元クラスを指定するとザクっと作ってくれる。
ビルドパスのJunit4が無かったら、確認した上で追加してくれる。※Eclipse 4.7.2の場合
ま、テスト内容は
fail(“まだ実装されていません”); // TODO
しか入らないけどね。

/**
 *
 */
package sample4;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runners.MethodSorters;
/**
 * @author ssiscirine
 *
 */
// テストケースの名前順に実行する指定
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Sample4Test {
	@Rule
	//テストケースのメソッド名を知りたくて追加。
	public TestName name = new TestName();
	/**
	 * JUnitの実行で最初に実行する処理
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		//
		System.out.println("setUpBeforeClass() JUnitを開始します。");
	}
	/**
	 * JUnitの実行で最後に実行する処理
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		//
		System.out.println("tearDownAfterClass() JUnitを終了します");
	}
	/**
	 * 各テストケースの事前に実行する処理
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		//
		System.out.println(name.getMethodName() + "のための setUp() テストケースの事前処理を行います。");
	}
	/**
	 * 各テストケースの事後に実行する処理
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		//
		System.out.println(name.getMethodName() + "のための" + " tearDown() テストケースの事後処理を行います。");
	}
	/**
	 * {@link sample4.Sample4#method0()} のためのテスト・メソッド。
	 */
	@Test
	public final void testMethod0() {
		System.out.println(name.getMethodName() + "() テストケース・その1");
		fail("まだ実装されていません"); // TODO
	}
	@Test
	public final void testMethod0_01() {
		System.out.println(name.getMethodName() + "() テストケース・その2");
		Sample4 sample4 = new Sample4();
		// Assert : 予想と違ったらNGに(JUnitの実行結果をX表記)する
		Assert.assertTrue(true);
		//
		Assert.assertNull(null);
		Assert.assertNotNull("Not null");
		//
		Assert.assertEquals(sample4.method0(), "method0");
		Assert.assertNotEquals(sample4.method0(), "method0_01");
		//
		Assert.assertThat(sample4.method0(), CoreMatchers.is(equalTo("method0")));
		Assert.assertThat(sample4.method0(), CoreMatchers.is(not(equalTo("method0_01"))));
		//予想と違ったら見なかったことに(JUnitの実行結果をIgnore表記)する
		Assume.assumeTrue(true);
		Assume.assumeFalse(false);
	}
	/**
	 * {@link sample4.Sample4#method1(java.lang.String)} のためのテスト・メソッド。
	 */
	@Ignore
	public final void testMethod1() {
		fail(name.getMethodName() + "() まだ実装されていません"); // TODO
	}
	/**
	 * {@link sample4.Sample4#method2(java.lang.String, int)} のためのテスト・メソッド。
	 */
	@Ignore
	public final void testMethod2StringInt() {
		fail(name.getMethodName() + "() まだ実装されていません"); // TODO
	}
	/**
	 * {@link sample4.Sample4#method2(int, java.lang.String)} のためのテスト・メソッド。
	 */
	@Ignore
	public final void testMethod2IntString() {
		fail(name.getMethodName() + "() まだ実装されていません"); // TODO
	}
}

Assert.assertTrue(true); 等はassertTrueがどこのクラスのメソッドか判りやすくするための書き方で、assertTrueがpublicでstaticなメソッドなのだから import org.junit.Assert;してあれば、assertTrue(true);で十分推測可能でしょうけど、いつでもどの環境でもメソッドの上にツールチップが出る訳ではないので、faileとか、isがCoreMatchers.isなのは覚えるしかないかもしれない。
実行してみると

setUpBeforeClass() JUnitを開始します。
testMethod0のための setUp() テストケースの事前処理を行います。
testMethod0() テストケース・その1
testMethod0のための tearDown() テストケースの事後処理を行います。
testMethod0_01のための setUp() テストケースの事前処理を行います。
testMethod0_01() テストケース・その2
testMethod0_01のための tearDown() テストケースの事後処理を行います。
tearDownAfterClass() JUnitを終了します

一度、作り込んでしまえば、System.out.printlnのログをコンソールに出す必要はほとんど無い。しかし、Junitの実行時間がとても長い場合は、setUpBeforeClassやtearDownAfterClassに短いログを出した方が安心だし、時折挫折することもあるテストでは、setUpやtearDownでも何か出して欲しい。データベースの初期データを投入します。とか、データベースを初期状態にロールバックします。とかね。
また、JTestの様なツールで使うテストケースではJTest実行中は大量に出てしまい邪魔になりそうなので、System.out.printlnよりはvoid log(String msg)などを作ってソコからSystem.out.printlnに出すようにした方がいいだろう。勿論、EclipseでJUnitを使うならログを標準出力に出さないのが本道だが、環境が悪いと途中で止まってしまったかのようにも見えてしまうので用意した方がいいだろう。
JUnitのビューは
sample4.Sample4Test
 [X]testMethod0
 [O]testMethod0_01
となっていて、[x]のメソッドをクリックする、障害トレースにassertXXXやasumeXXXのログが出てくる。ドキュメントはここ
勿論、privateなメソッドだったり、相手がMVCのクラスだったり、直接テスト対象にアクセスできない場合はそれなりの機能がある様だが、それは次回に続く。




コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA