2020년 11월 6일 금요일

Hamcrest Test Framework

 Hamcrest란?

test framework을 이용하여 단위 테스트 진행 시 assert를 유연하게 하기 위해 matcher를 이용하여 좀 더 쉽고 확장성 있는 assert를 하기 위해 만들어진 framework이다. 

Hamcrest는 처음부터 test framework과 통합되도록 설계되어 JUnit, TestNG와 함께 사용할 수 있다.


maven dependency

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>

Hamcrest code

assertThat(actual, is(equalTo(expedted);
assertThat(result, instanceof String);
assertThat(result, is(hasItem(anyOf(equalTo("x", equalTo("y"), equalTo("z"))))));

Hamcrest Matcher 

allOf - matches if all matchers match (short circuits)
anyOf - matches if any matchers match (short circuits)
not - matches if the wrapped matcher doesn’t match and vice
equalTo - test object equality using the equals method
is - decorator for equalTo to improve readability
hasToString - test Object.toString
instanceOf, isCompatibleType - test type
notNullValue, nullValue - test for null
sameInstance - test object identity
hasEntry, hasKey, hasValue - test a map contains an entry, key or value
hasItem, hasItems - test a collection contains elements
hasItemInArray - test an array contains an element
closeTo - test floating point values are close to a given value
greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo
equalToIgnoringCase - test string equality ignoring case
equalToIgnoringWhiteSpace - test string equality ignoring differences in runs of whitespace
containsString, endsWith, startsWith - test string matching

댓글 없음:

댓글 쓰기