junit intro

24
JUnit入門 Yutaka Kinjo

Upload: yutaka-kinjyo

Post on 25-Dec-2014

147 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Junit intro

JUnit入門Yutaka Kinjo

Page 2: Junit intro

JUnitA programmer-oriented testing framework for Java

Page 3: Junit intro

Standard Method

assertThat(actual, is(expected));

Page 4: Junit intro

Standard Method

assertThat(actual, is(expected));

実際の値 期待値

Page 5: Junit intro

assertThat(actual, is(expected));

実際の値 期待値

ex.加算用のメソッドをテストする時

1 + 2 = 3 の場合、3を期待値とすると

Standard Method

Page 6: Junit intro

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

Page 7: Junit intro

Test resultテスト成功

テスト失敗

Page 8: Junit intro

Test resultテスト失敗

Expected: is <3>

but: was <-1>

Page 9: Junit intro

Add Methodpublic int add(int a, int b) {

return a - b; }

public int add(int a, int b) { return a + b;

}

テスト結果 Green

Page 10: Junit intro

Routine

テスト 修正

一度書けば何度でもテスト可能

Page 11: Junit intro

Routine

テスト 修正

テストの実行は軽量(対象のメソッドだけ)高速なデバックが可能になる!!

Page 12: Junit intro

What is “Matcher” ?

Page 13: Junit intro

Standard Method

assertThat(actual, is(expected));

Matcher

actual is expected が真なら Green

Page 14: Junit intro

Matcher Methodis not nullValue notNullValue sameInstance instanceOf

Page 15: Junit intro

Matcher Method

assertThat(actual, is(expected));

assertThat(actual, not(expected));

: actual と expected が同じ値なら Greenis

not : actual と expected が違う値なら Green

Page 16: Junit intro

Matcher Method

assertThat(actual, nullValue());

assertThat(actual, notNullValue());

: actual が null なら GreennullValue

notNullValue : actual が null でないなら Green

Page 17: Junit intro

Matcher Method

assertThat(actual,sameInstance(expected));

assertThat(actual,instanceOf(expected));

: actual と expected が 同じインスタンスなら Green

sameInstance

instanceOf : actual が expected で 指定したインスタンスなら Green

Page 18: Junit intro

What is “Annotation” ?

Page 19: Junit intro

Annotation

日本語だと「注釈」

Page 20: Junit intro

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 対象のメソッドを示す

Page 21: Junit intro

Annotation@Test @Before @After @BeforeClass @AfterClass @Ignore

Page 22: Junit intro

Annotation@Test : テスト対象を示す @Before : テストメソッドの前に毎回実行される @After : テストの後に毎回実行される @BeforeClass : テストクラスの前に実行される @AfterClass : テストクラスの後に実行される @Ignore : テスト対象でないことを示す

Page 23: Junit intro

JUnit入門 Key word

assertThat matcher annotation TDD

Page 24: Junit intro

What is “TDD” ?http://www.slideshare.net/t_wada/

devlove2012-twada-tdd