JUnit API探訪:@SuiteClasses アノテーション

@SuiteClasses アノテーション

複数のクラスをまとめて実行する際に用いる。

検証用に幾つかテストクラスを作成。

package jp.shinyaa31.junit.forsuitecases;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class TestCaseA {

	@Test
	public void testCaseA_test1() {
		System.out.println("testCaseA_test1().");
		assertThat(1, is(1));
	}
	@Test
	public void testCaseA_test2() {
		System.out.println("testCaseA_test2().");
		assertThat(2, is(2));
	}
}
package jp.shinyaa31.junit.forsuitecases;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class TestCaseB {

	@Test
	public void testCaseB_test1() {
		System.out.println("testCaseB_test1().");
		assertThat(1, is(1));
	}
	@Test
	public void testCaseB_test2() {
		System.out.println("testCaseB_test2().");
		assertThat(2, is(2));
	}
}
package jp.shinyaa31.junit.forsuitecases;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class TestCaseC {

	@Test
	public void testCaseC_test1() {
		System.out.println("testCaseC_test1().");
		assertThat(1, is(1));
	}
	@Test
	public void testCaseC_test2() {
		System.out.println("testCaseC_test2().");
		assertThat(2, is(2));
	}
}
package jp.shinyaa31.junit.forsuitecases;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class TestCaseD {

	@Test
	public void testCaseD_test1() {
		System.out.println("testCaseD_test1().");
		assertThat(1, is(1));
	}

	@Test
	public void testCaseD_test2() {
		System.out.println("testCaseD_test2().");
		assertThat(2, is(2));
	}
}

@SuiteClasses アノテーションを用いたクラスを用意。
@RunWith アノテーションを記述、その引数にSuite.classを指定。
更には、@SuiteClasses アノテーションにてまとめて実行させたいテストクラスを各々指定。
テスト実行は通常のJUnitテストクラス実行同様の手法で可能。(※QuickJUnitの場合Ctrl+0)

package jp.shinyaa31.junit;

import jp.shinyaa31.junit.forsuitecases.TestCaseA;
import jp.shinyaa31.junit.forsuitecases.TestCaseB;
import jp.shinyaa31.junit.forsuitecases.TestCaseC;
import jp.shinyaa31.junit.forsuitecases.TestCaseD;

import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({
	TestCaseA.class,
	TestCaseB.class, 
	TestCaseC.class,
	TestCaseD.class
})
public class AllTestCase {
	//public static void main(String[] args) {
	//	JUnitCore.main(AllTestCase.class.getName());
	//}
}