Hamcrest Object Matchers

ハムクレストオブジェクトマッチャー

1. 概要

Hamcrest provides matchers for making unit test assertions simpler and more legible.利用可能なマッチャーhereのいくつかの探索を開始できます。

このクイックチュートリアルでは、オブジェクトマッチャーについて詳しく説明します。

2. セットアップ

Hamcrestを取得するには、add the following Maven dependency to our pom.xmlを実行する必要があります。


    org.hamcrest
    java-hamcrest
    2.0.0.0
    test

最新のハムクレストバージョンはMaven Centralにあります。

3. オブジェクトマッチャー

Object matchers are meant to perform checks over object’s properties

マッチャーを調べる前に、例をわかりやすくするためにいくつかのBeanを作成します。

最初のオブジェクトはLocationと呼ばれ、プロパティはありません。

public class Location {}

2番目のBeanにCityという名前を付け、次の実装を追加します。

public class City extends Location {

    String name;
    String state;

    // standard constructor, getters and setters

    @Override
    public String toString() {
        if (this.name == null && this.state == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        sb.append("Name: ");
        sb.append(this.name);
        sb.append(", ");
        sb.append("State: ");
        sb.append(this.state);
        sb.append("]");
        return sb.toString();
    }
}

CityLocationを拡張することに注意してください。 後でそれを利用します。 それでは、オブジェクトマッチャーから始めましょう!

3.1. hasToString

名前が示すように、hasToString method verifies that certain object has a toString method that returns a specific String

@Test
public void givenACity_whenHasToString_thenCorrect() {
    City city = new City("San Francisco", "CA");

    assertThat(city, hasToString("[Name: San Francisco, State: CA]"));
}

したがって、Cityを作成し、そのtoStringメソッドが必要なStringを返すことを確認します。 これをさらに一歩進めて、同等性をチェックする代わりに、他の条件をチェックします。

@Test
public void givenACity_whenHasToStringEqualToIgnoringCase_thenCorrect() {
    City city = new City("San Francisco", "CA");

    assertThat(city, hasToString(
      equalToIgnoringCase("[NAME: SAN FRANCISCO, STATE: CA]")));
}

ご覧のとおり、hasToString is overloaded and can receive both a String or a text matcher as a parameterです。 そのため、次のようなこともできます。

@Test
public void givenACity_whenHasToStringEmptyOrNullString_thenCorrect() {
    City city = new City(null, null);

    assertThat(city, hasToString(emptyOrNullString()));
}

テキストマッチャーhereの詳細については、こちらをご覧ください。 それでは、次のオブジェクトマッチャーに移りましょう。

3.2. typeCompatibleWith

このマッチャーrepresents an is-a relationshipLocationスーパークラスが登場します。

@Test
public void givenACity_whenTypeCompatibleWithLocation_thenCorrect() {
    City city = new City("San Francisco", "CA");

    assertThat(city.getClass(), is(typeCompatibleWith(Location.class)));
}

これは、Cityが-Location,であり、これが真であり、このテストに合格する必要があることを示しています。 また、否定的なケースをテストしたい場合:

@Test
public void givenACity_whenTypeNotCompatibleWithString_thenCorrect() {
    City city = new City("San Francisco", "CA");

    assertThat(city.getClass(), is(not(typeCompatibleWith(String.class))));
}

もちろん、CityクラスはString.ではありません

最後に、すべてのJavaオブジェクトが次のテストに合格する必要があることに注意してください。

@Test
public void givenACity_whenTypeCompatibleWithObject_thenCorrect() {
    City city = new City("San Francisco", "CA");

    assertThat(city.getClass(), is(typeCompatibleWith(Object.class)));
}

the matcher is consists of a wrapper over another matcher with the purpose of making the whole assertion more readable.であることを忘れないでください

4. 結論

Hamcrest provides a simple and clean way of creating assertions.すべての開発者の生活をよりシンプルにし、すべてのプロジェクトをより読みやすくするさまざまなマッチャーがあります。

また、オブジェクトマッチャーは、クラスのプロパティをチェックする簡単な方法です。

いつものように、完全な実装はthe GitHub projectにあります。