JUnit入門
Yutaka Kinjo
JUnit
A programmer-oriented
testing framework for Java
Standard Method
assertThat(actual, is(expected));
Standard Method
assertThat(actual, is(expected));
実際の値 期待値
assertThat(actual, is(expected));
実際の値 期待値
ex.加算用のメソッドをテストする時
1 + 2 = 3 の場合、3を期待値とすると
Standard Method
Add Method Test
@Test
public void addで1と2の加算結果が返却される() {
!
int a = 1;
int b = 2;
int expected = 3;
Calc sut = new Calc();
int actual = sut.add(a,b);
!
assertThat(actual ,is(expected));
テスト結果 Green or Red
Test result
テスト成功
テスト失敗
Test result
テスト失敗
Expected: is <3>
but: was <-1>
Add Method
public int add(int a, int b) {
return a - b;
}
public int add(int a, int b) {
return a + b;
}
テスト結果 Green
Routine
テスト 修正
一度書けば何度でもテスト可能
Routine
テスト 修正
テストの実行は軽量(対象のメソッドだけ)
高速なデバックが可能になる!!
What is Matcher ?
Standard Method
assertThat(actual, is(expected));
Matcher
actual is expected が真なら Green
Matcher Method
is
not
nullValue
notNullValue
sameInstance
instanceOf
Matcher Method
assertThat(actual, is(expected));
assertThat(actual, not(expected));
: actual と expected が同じ値なら Greenis
not : actual と expected が違う値なら Green
Matcher Method
assertThat(actual, nullValue());
assertThat(actual, notNullValue());
: actual が null なら GreennullValue
notNullValue : actual が null でないなら Green
Matcher Method
assertThat(actual,sameInstance(expected));
assertThat(actual,instanceOf(expected));
: actual と expected が
同じインスタンスなら Green
sameInstance
instanceOf : actual が expected で
指定したインスタンスなら Green
What is Annotation ?
Annotation
日本語だと「注釈」
Annotation
@Test
public void addで1と2の加算結果が返却される() {
!
int a = 1;
int b = 2;
int expected = 3;
Calc sut = new Calc();
int actual = sut.add(a,b);
!
assertThat(actual ,is(expected));
Test 対象のメソッドを示す
Annotation
@Test
@Before
@After
@BeforeClass
@AfterClass
@Ignore
Annotation
@Test : テスト対象を示す
@Before : テストメソッドの前に毎回実行される
@After : テストの後に毎回実行される
@BeforeClass : テストクラスの前に実行される
@AfterClass : テストクラスの後に実行される
@Ignore : テスト対象でないことを示す
JUnit入門 Key word
assertThat
matcher
annotation
TDD
What is TDD ?
http://www.slideshare.net/t_wada/
devlove2012-twada-tdd

Junit intro