ㅇ Matcher 클래스

 

: Matcher 클래스는 대상 문자읠 패턴을 해석하고 주어진 패턴과 일치하는지 판별할 때 주로 사용됩니다.

입력 값으로는 CharSequence라는 새로운 인터페이스가 주로 사용되는데, 다양한 형태의 입력 데이트로부터 문자 단위의 매칭 기능을

지원 받을 수 있습니다.

 

 

ㅇ Matcher 클래스 주요 메서드

 

matches() : 대상 문자열과 패턴이 일치할 경우 true 반환합니다.
find() : 대상 문자열과 패턴이 일치하는 경우 true를 반환하고, 그 위치로 이동합니다.
find(int start) : start위치 이후부터 매칭검색을 수행합니다.
start() : 매칭되는 문자열 시작위치 반환합니다.
start(int group) : 지정된 그룹이 매칭되는 시작위치 반환합니다.
end() : 매칭되는  문자열 끝 다음 문자위치 반환합니다.
end(int group) : 지정되 그룹이 매칭되는 끝 다음 문자위치 반환합니다.
group() : 매칭된 부분을 반환합니다.
group(int group) : 매칭된 부분중 group번 그룹핑 매칭부분 반환합니다. 
groupCount() : 패턴내 그룹핑한(괄호지정) 전체 갯수를 반환합니다.

 

 

 

 

ㅇ Hamcrest

: 소프트웨어 테스트를 위한 framework

: 기존의 matchers 클래스를 통해 코드의 조건을 확인 할 수 있음

: JUnit 에서 Hamcrest matcher를 사용하려면 assertThat 문 뒤에 하나 또는 여러 개의 matchers 를 사용한다.

: Spring boot 에서 사용하기 위해선 dependenct 를 추가해줘야 한다.

 

 

 

ㅇ 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

* 참조 : http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html

 

Matchers (Hamcrest)

org.hamcrest Class Matchers java.lang.Object org.hamcrest.Matchers public class Matchersextends java.lang.Object Constructor Summary Matchers()               Method Summary static allOf(java.lang.Iterable > matchers)           Create

hamcrest.org

 

 

'JAVA' 카테고리의 다른 글

객체 지향 프로그래밍  (0) 2022.07.08
String 중간 공백기준으로 배열 만들기  (0) 2022.07.06
Optional 이란?  (0) 2022.07.02
상속을 응용한 스택 구현  (0) 2021.10.07
배열 큐  (0) 2021.10.07

+ Recent posts