알팅스님의 개발 스토리



보다 유창한 Assertion을 해보자 - 1편

Readability가 뛰어난 Assert 구문을 작성하는게 좋다는 건 모두 가 잘 알고 있는 이야기일 것이다.

assertEquals(expect, actual); 

형식은 사용 방법부터 비직관적이라고 많은 사람들이 얘기하고 있다.

이에 개선을 하고자 Hamcrest를 많은 사람들이 사용하고 있다. (https://github.com/hamcrest/JavaHamcrest)
Hamcrest도 충분히 직관적이라고 생각하고 뛰어난 표현력과 유연한 notation, informative한 에러 메시지와 같이 assertEquals에 비해서 정말 뛰어나다고 생각을 하고 있었다.

public class BiscuitTest extends TestCase {
 
public void testEquals() {
   
Biscuit theBiscuit = new Biscuit("Ginger");
   
Biscuit myBiscuit = new Biscuit("Ginger");
    assertThat
(theBiscuit, equalTo(myBiscuit));
 
}
}

이런 assertThat equalTo같은 표현들이 hamcrest의 표현이다.

equalTo 외에도

A tour of common matchers

Hamcrest comes with a library of useful matchers. Here are some of the most important ones.

  • Core
    • anything - always matches, useful if you don't care what the object under test is
    • describedAs - decorator to adding custom failure description
    • is - decorator to improve readability - see "Sugar", below
  • Logical
    • allOf - matches if all matchers match, short circuits (like Java &&)
    • anyOf - matches if any matchers match, short circuits (like Java ||)
    • not - matches if the wrapped matcher doesn't match and vice versa
  • Object
    • equalTo - test object equality using Object.equals
    • hasToString - test Object.toString
    • instanceOfisCompatibleType - test type
    • notNullValuenullValue - test for null
    • sameInstance - test object identity
  • Beans
    • hasProperty - test JavaBeans properties
  • Collections
    • array - test an array's elements against an array of matchers
    • hasEntryhasKeyhasValue - test a map contains an entry, key or value
    • hasItemhasItems - test a collection contains elements
    • hasItemInArray - test an array contains an element
  • Number
    • closeTo - test floating point values are close to a given value
    • greaterThangreaterThanOrEqualTolessThanlessThanOrEqualTo - test ordering
  • Text
    • equalToIgnoringCase - test string equality ignoring case
    • equalToIgnoringWhiteSpace - test string equality ignoring differences in runs of whitespace
    • containsStringendsWithstartsWith - test string matching
요런 많은 Matcher들을 제공하고 있어서 다양한 표현들을 할 수 있다.
여기서 표현이 부족하다면 커스텀 Matcher를 생성할 수도 있고 말이다.

public class IsNotANumber extends TypeSafeMatcher<Double> {

 
@Override
 
public boolean matchesSafely(Double number) {
   
return number.isNaN();
 
}

 
public void describeTo(Description description) {
    description
.appendText("not a number");
 
}

 
@Factory
 
public static <T> Matcher<Double> notANumber() {
   
return new IsNotANumber();
 
}

}

난 이게 현재까지는 끝판왕이라고 생각하고 있었다.
이렇게 뛰어난 표현을 하고 에러메시지를 내 입맛에 맞게 정의할 수 있다는데에 너무 만족을 해서 

무언가 새로운 더 좋은 방법이 있을 것이라는 생각을 안해봤다는 말이 맞을 것이다.

헌데 얼마 전에 Fest-Assertion이라는 친구를 공유받았다.

아니, 엄밀히 말하면 나는 Fest Assertion이 포함되어 있는 코드를 보게 되었고, 그 코드를 보면서
"우와 이런 표현이라니 !!" 하면서 화들짝 놀랐다 진심으로....

<절단신공!>


일을 해야함으로 오늘은 여기서 끊고 ! Fest Assertion에 대한 소개는 다음으로 미루겠음!


캡슐화와 테스트 (부재: private 메소드에 대한 테스트)

본 글에서는 캡슐화와 테스트가 서로 충돌하게 되는 포인트에 대한 이야기를 적어보고자 한다.캡슐화와 테스트 사이에는 어떤 관계가 있길래 충돌을 얘기하고 있을까?테스트를 만들게 되면 public 메소드에 대해서만 호출을 해서 테스트하게 된다.사실 당연한 이야기이다. 테스트 클래스와 프로덕션 클래스는 엄연히 다른 클래스이기 때문에private 메소드같은건 호... » 내용보기

자바스크립트에서의 다국어 지원 jquery.i18n.properties

 » 내용보기

배열에 대한 ASSERT?

 » 내용보기

어떤 테스트케이스를 먼저 작성하는 것이 옳은 것일까?

 » 내용보기