Hamcrest는 JUnit 기반의 단위 테스트에서 사용할 수 있는 Assertion Framework입니다.
✅ Hamcrest에서의 Assertion
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
public class HelloHamcrestTest {
@DisplayName("Hello Junit Test using hamcrest")
@Test
public void assertionTest1() {
String expected = "Hello, JUnit";
String actual = "Hello, JUnit";
assertThat(actual, is(equalTo(expected))); // (1)
}
}
assertEquals(expected, actual);
assertThat(actual, is(equalTo(expected)));
assert that actual is equal to expected’라는 하나의 영어 문장으로 자연스럽게 읽힙니다.
결과 값(actual)이 기대 값(expected)과 같다는 것을 검증(Assertion)한다.’ 정도로 해석할 수 있습니다.