// set up ... // execute ... // verify assertThat ("OK", is (ret.msg)) // tear down
アノテーション | 概要 |
@Test | テストメソッドであることを示す |
@Test(timeout=1000) | 1000msたってもテストメソッドが終わらない場合には、失敗と見なす |
@Test(expected = NullPointerException?.class) | NullPointerException?が起きることを期待する |
@Ignore | 無視するテストメソッドであることを示す |
@Before | それぞれのテストメソッド実行前に実行されるメソッド。わかりやすいように setUp() とする |
@After | それぞれのテストメソッド実行後に実行されるメソッド。わかりやすいように tearDown() とする |
@BeforeClass? | テストバッチの最初に一回だけ実行されるメソッド |
@AfterClass? | テストバッチの最後に一回だけ実行されるメソッド |
package com.snail;
import static org.junit.Assert.*;
import org.junit.*;
public class Junit4Exam {
private int x = 100; // it will be overrided @Before (the before() method)
private int y = 100; // it will be overrided @Before (the before() method)
@Test
public void addTest() {
System.out.println("---Test(addTest()) is called");
assertEquals(3, x + y);
System.out.println("---Test(addTest()) was success");
}
@Test
public void subTest() {
System.out.println("---Test(subTest()) is called");
assertEquals(1, x - y);
System.out.println("---Test(subTest()) was success");
}
@Test(timeout = 1000)
public void timeoutTest() {
System.out.println("---Test(timeoutTest()) is called");
try {
Thread.sleep(2000);
System.out.println("---Test(timeoutTest()) was success");
} catch (InterruptedException e) {
fail();
}
}
@Test(expected = NullPointerException.class)
public void exceptionTest() {
System.out.println("---Test(exceptionTest()) is called");
String str = null;
str.subSequence(0, 20);
System.out.println("---Test(exceptionTest()) was fail");
}
@Before
public void before() {
System.out.println("--Before is called");
x = 2;
y = 1;
}
@After
public void after() {
System.out.println("--After is called");
}
@BeforeClass
public static void beforeClass() {
System.out.println("Before Class is called");
}
@AfterClass
public static void afterClass() {
System.out.println("After Class is called");
}
}
Before Class is called
--Before is called
---Test(addTest()) is called
---Test(addTest()) was success
--After is called
--Before is called
---Test(subTest()) is called
---Test(subTest()) was success
--After is called
--Before is called
---Test(timeoutTest()) is called
--After is called
--Before is called
---Test(exceptionTest()) is called
--After is called
After Class is called
package com.snail.exam;
import static org.junit.Assert.assertEquals;
import junit.framework.JUnit4TestAdapter;
import org.junit.Test;
public class UnitTestExam {
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(UnitTestExam.class);
}
@Test
public void testTest() throws Exception {
assertEquals(2, 2);
}
}
package com.snail;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
public class JUnit4ExamBase {
protected int x = 100;
protected int y = 100;
@Before
public void before() {
System.out.println("--Before is called");
x = 2;
y = 1;
}
@After
public void after() {
System.out.println("--After is called");
}
@BeforeClass
public static void beforeClass() {
System.out.println("Before Class is called");
}
@AfterClass
public static void afterClass() {
System.out.println("After Class is called");
}
}
package com.snail;
import static org.junit.Assert.*;
import org.junit.*;
public class JUnit4ExamSub extends JUnit4ExamBase{
@Test
public void addTest() {
System.out.println("---Test(addTest()) is called");
assertEquals(3, x + y);
System.out.println("---Test(addTest()) was success");
}
@Test
public void subTest() {
System.out.println("---Test(subTest()) is called");
assertEquals(1, x - y);
System.out.println("---Test(subTest()) was success");
}
@Test(timeout = 1000)
public void timeoutTest() {
System.out.println("---Test(timeoutTest()) is called");
try {
Thread.sleep(2000);
System.out.println("---Test(timeoutTest()) was success");
} catch (InterruptedException e) {
fail();
}
}
@Test(expected = NullPointerException.class)
public void exceptionTest() {
System.out.println("---Test(exceptionTest()) is called");
String str = null;
str.subSequence(0, 20);
System.out.println("---Test(exceptionTest()) was fail");
}
@Before
public void subBefore() {
System.out.println("--(Sub)Before is called");
x = 2;
y = 1;
}
@After
public void subAfter() {
System.out.println("--(Sub)After is called");
}
@BeforeClass
public static void subBeforeClass() {
System.out.println("(Sub)Before Class is called");
}
@AfterClass
public static void subAfterClass() {
System.out.println("(Sub)After Class is called");
}
}
Before Class is called (Sub)Before Class is called --Before is called --(Sub)Before is called ---Test(addTest()) is called ---Test(addTest()) was success --(Sub)After is called --After is called --Before is called --(Sub)Before is called ---Test(subTest()) is called ---Test(subTest()) was success --(Sub)After is called --After is called --Before is called --(Sub)Before is called ---Test(timeoutTest()) is called --Before is called --(Sub)Before is called ---Test(exceptionTest()) is called --(Sub)After is called --After is called --(Sub)After is called --After is called (Sub)After Class is called After Class is called