양쪽 이전 판
이전 판
다음 판
|
이전 판
|
wiki:java:junit [2022/05/04 18:14] 127.0.0.1 바깥 편집 |
wiki:java:junit [2023/01/13 18:44] (현재) |
===== 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 | | |
| 정적 임포트 사용\\ import static org.junit.Assert.*\\ import static org.hamcrest.CoreMatchers.* 햄크레스트\\ 조건이 참이 아닐경우 동작\\ 테스트는 멈추고, failure 보고 || | | 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 보고 ||| | |
| |
| |