テストにはなってないけど、これくらいしってりゃいいでしょ!というアノテーションを使ったサンプル
package junit4;
//assertEqualsって書くだけで使いたいからね
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class JUnit4Sample {
@BeforeClass
public static void initTest() {
System.out.println("テストケース単位の初期化");
System.out.println("注意:static metohdにしないと駄目");
}
@AfterClass
public static void dispoaseTest() {
System.out.println("テストケース単位の後始末");
System.out.println("注意:static metohdにしないと駄目");
}
@Before
public void init() {
System.out.println("テストの前処理 JUnit3のsetUp相当");
}
@Before
public void init2() {
System.out.println("setUpと違って複数指定可能");
}
@After
public void dispose() {
System.out.println("テストの後処理 JUnit3のtearDown相当");
}
@After
public void dispose2() {
System.out.println("tearDownと違って複数指定可能");
}
@Test
public void method() throws Exception {
System.out.println("テスト対象");
assertEquals("Assertクラスをstatic importしてるので使える", "期待値", "結果の値");
assertEquals("配列もサポート!!",
new String[]{"A", "B", "C"},
new String[]{"A", "B", "C"});
}
@Test
@Ignore("実行しない理由を書く")
public void method2() throws Exception {
System.out.println("Ignoreつけると実行されない");
}
@Test(expected = NullPointerException.class)
public void method3() throws Exception {
System.out.println("例外を投げる場合のテスト");
String str = null;
str.length();
}
@Test(timeout = 1000)
public void method4() throws Exception {
System.out.println("タイムアウトのテスト");
Thread.sleep(2000);
}
}