양쪽 이전 판
이전 판
다음 판
|
이전 판
|
wiki:java:junit [2020/09/08 18:37] dhan [표] |
wiki:java:junit [2023/01/13 18:44] (현재) |
| |
===== JUnit ===== | ===== JUnit ===== |
[[[[wiki:java:junit:JUnit A Cook's Tour|JUnit A Cook's Tour]] \\ | [[wiki:java:junit:JUnit A Cook's Tour|JUnit A Cook's Tour]] \\ |
자바 프로그래밍 언어용 단위 테스트 프레임워크입니다. \\ | 자바 프로그래밍 언어용 단위 테스트 프레임워크입니다. \\ |
| [[wiki:java:junit:JUnit5|JUnit5]]\\ |
| |
| |
| ===== 동작 순서(토비 스프링3) ===== |
| - 테스트 클래스에서 @Test가 붙은 public이고 void형이며 파라미터가 없는 테스트 메소드를 모두 찾는다. |
| - 테스트 클랙스의 오브젝트를 하나 만든다. |
| - @Before가 붙은 메소드가 있으면 실행한다. |
| - @Test가 붙은 메소드를 하나 호출하고 테스트 결과를 저장해둔다. |
| - @After가 붙은 메소드가 잇으면 실행한다. |
| - 나머지 테스트 메소드에 대해 2~5번을 반복한다. |
| - 모든 테스트의 결과를 종합해서 돌려준다. |
| |
\\ | \\ |
===== Assert ===== | ===== Assert ===== |
단언 | 단언 |
^ Assert 명 ^ 설명 ^ | ^ assert method ^ assertThat method ^ static Import ^ 설명 ^ |
| assertTrue | 참이라고 가정 | | | assertNull(value); | assertThat(actual, nullValue); | org.hamcrest.core.IsNull.nullValue | | |
| assertFalse | 거짓이라고 가정 | | | assertNotNull(value); | assertThat(actual, notNullValue); | org.hamcrest.core.IsNull.notNullValue | | |
| assertEqual | 동일하다고 가정 | | | assertTrue(value); | assertThat(actual, is(true)); | org.hamcrest.core.Is.is | 참이라고 가정 | |
| assertThat \\ * 부동 소수점 비교\\ assertThat(2.32*3, equalTo(6.96))\\ -> assertTrue(Math.abs((2.32*3) - 6.96) < 0.0005)\\ * 설명 추가 가능\\ assertThat('메시지', 실제 표현식, matcher) \\ * 실패하면 스택트레이스 출력 | 명확한 값을 비교, 햄크레스트\\ * assertThat(실제 표현식, matcher)\\ matcher\\ equalTo()\\ is(true)\\ startsWith('xyz')\\ not(equalTo())\\ is(not(nullValue()))\\ is(notNullValue())\\ closeTo(, )\\ import static org.hamcrest.number.IsCloseTo.* | | | assertTrue(1 > 3); | assertThat(1, greaterThan(3)); | org.hamcrest.number.OrderingComparison.greaterThan | | |
| | assertTrue(“abc”.contains(“d”)); | assertThat(“abc”, containsString(“d”)); | org.hamcrest.core.StringContains.containsString | | |
| | assertFalse(value); | assertThat(actual, is(false)); | org.hamcrest.core.Is.is | 거짓이라고 가정 | |
| | assertEquals(“expected”, “actual”); | assertThat(“actual”, is(“expected”)); | org.hamcrest.core.Is.is | 동일하다고 가정 | |
| | assertArrayEquals(new String[]{“test1”, “test2”}, new String[]{“test3”, “test4”}); | assertThat(new String[]{“test1”, “test2”}, is(new String[]{“test3”, “test4”})); | org.hamcrest.core.Is.is | | |
| | assertSame(expected, actual); | assertThat(actual, sameInstance(expected)); | org.hamcrest.core.IsSame.sameInstance | | |
| | assertNotSame(expected, actual); | assertThat(actual, not(sameInstance(expected))); | org.hamcrest.core.IsNot.not, org.hamcrest.core.IsSame.sameInstance | | |
| | assertThat \\ * 부동 소수점 비교\\ assertThat(2.32*3, equalTo(6.96))\\ -> assertTrue(Math.abs ( ( 2.32 * 3 ) - 6.96) < 0.0005)\\ * 설명 추가 가능\\ assertThat('메시지', 실제 표현식, matcher) \\ * 실패하면 스택트레이스 출력 | assertThat \\ * 부동 소수점 비교\\ assertThat(2.32*3, equalTo(6.96))\\ -> assertTrue(Math.abs ( ( 2.32 * 3 ) - 6.96) < 0.0005)\\ * 설명 추가 가능\\ assertThat('메시지', 실제 표현식, matcher) \\ * 실패하면 스택트레이스 출력 | | 명확한 값을 비교, 햄크레스트\\ * assertThat(실제 표현식, matcher)\\ matcher\\ equalTo()\\ is(true)\\ startsWith('xyz')\\ not(equalTo())\\ is(not(nullValue()))\\ is(notNullValue())\\ closeTo(, )\\ import static org.hamcrest.number.IsCloseTo.* | |
| | 정적 임포트 사용\\ import static org.junit.Assert.*\\ import static org.hamcrest.CoreMatchers.* 햄크레스트\\ 조건이 참이 아닐경우 동작\\ 테스트는 멈추고, failure 보고 ||| | |
| |
| |