Yandexでのテスト。 マッチャー:有用な場合と、それらを使用するのがいかに簡単か

オレンジはそれとは何の関係もありません。 右側の図から推測できるように、自動テストについて説明します。 より正確には、マッチャーなどのテクノロジーについて。 コードの重複を大幅に削減し、認識のためのテストコードを簡素化するのに役立ちます。マッチャーの作成と使用は非常に簡単です。



マッチャーの技術自体は新しいものではありません。現在の形式では、2012年7月にリポジトリにアップロードされ、さらに早く登場しました。 しかし、それにもかかわらず、多くの人はまだ聞いていないか、何らかの理由でそれを避けています。 私たちは、その使用から利益を得るのがどれほど簡単かを伝え、マッチャーのライブラリをあなたと共有したいと思います。



特定の果物のセットがあるとします。 それらの中で、最も一般的なラウンド、オレンジと甘い。



public class Fruit { ... public Color getColor() {...} public boolean isSweet() {...} public Shape getShape() {...} }
      
      







オレンジはまさにそのような条件を満たすことが知られています。 果物に加えて、そのような果物を通過させることができるコンベアもあります。 コンベアには、一連のテストを実施してオレンジではなくふるい分けをするタスクもあります。



そして幸運-果物が甘いかどうか、そしてどの色であるかを判断し、その形状を多くの有名なものと比較し、さらに多くのチェックを行うことができる装置が手元にありました。 このデバイスはJUnitと呼ばれます



テストが始まる前に、新しい果物がコンベアに落ちます。



  @Before public void setUp() throws Exception { someFruit = getNextFruit(); }
      
      







まず、果実が丸いことを確認します。



  @Test public void orangeIsRound() { assertEquals("Expected shape - " + Shape.ROUND + ", but was - " + someFruit.getShape(), someFruit.getShape(), Shape.ROUND); }
      
      







その果物は甘いです。



  @Test public void orangeIsSweet() { assertTrue("Fruit should be sweet - expected TRUE", someFruit.isSweet()); }
      
      







そして最後に、その色を見てください。



  @Test public void orangeHasOrangeColor() { assertEquals("Orange has orange color, but was - " + someFruit.getColor(), someFruit.getColor(), Color.ORANGE); }
      
      







すぐに明らかな欠点は何ですか? 第一に、各チェックでは、期待値、実際の値、追加の改良の値、およびさまざまな間投詞からのコメントを結び付ける必要がありました。 このリストの説明でさえ退屈です。



次に、オレンジの種類も決定し、このために特別な「テイスター」を追加して、「バレンシアの品種のみを承認する」と彼に言ったと想像してください。 しかし、「テイスター」は、試験機が何を決定したかを知りません。彼が自問しない場合、試験機はおしゃべりではありません。 最後に、彼は連続してすべてを試します。 甘さをチェックする時間がなかったボールで「テイスター」を毒しないようにするには、余分なものをすべて無視するように彼に教える必要があります。 これを行うには、彼は機械に個別に独立して尋ねる必要があります。その後、拒否されたものはすべて脇に置き、もう触れないようにする必要があります。



「テイスター」はJUnitデバイスの単なるテストであるため、組み込みのランタイムメカニズムを使用してテストを無視することができます。 次に、試飲の開始のシナリオは次のようになります。



  @Test public void degusto() { assumeTrue("Expected shape - " + Shape.ROUND + ", but was - " + someFruit.getShape(), someFruit.getShape().equals(Shape.ROUND)); assumeTrue("Fruit should be sweet - expected TRUE", someFruit.isSweet()); assumeTrue("Orange has orange color, but was - " + someFruit.getColor(), someFruit.getColor().equals(Color.ORANGE)); //        . }
      
      







このスクリプトの説明を含む新しい「テイスター」にはそれぞれ、スクリプトをコピーして貼り付けるか、事前チェックのセットごとに新しいメソッドを作成するという2つの方法があることがはっきりとわかります。 また、各事前チェックでは、拒否の理由に関するメッセージを編集する必要があります。 これらのオプションはいずれも維持が難しく、知覚するのが非常に怖いです。



assertEquals



assertNotEquals



assertNotNull



assertArrayEquals



assertArrayEquals



などを放棄する必要があります。 標準のJUnitパッケージでは、これらのassert *はほとんどすべての些細なケースに対応しています。 さらに、引数のタイプごとに。 つまり、検証ロジックはメソッドの名前にあり、その実装に厳密に結び付けられています。 すべてのアサート*に対して同様の仮定*を行う必要がある場合、どのくらいのコードを複製して維持する必要があるかを想像してください。



そのため、検証自体のロジック、説明の出力、および意思決定機能を分離する必要があります。





これがマッチャーの出番です-意思決定のロジックを含む小さなオブジェクトで、何を待っているのか、何を受け取ったのかを把握し、独自に報告します。 最初の3つのチェックでは、マッチャーの助けを借りて、アクションをほぼ詩的に説明します。 そして、スクリプトが何をしているかを知りたい人は誰でもそれを読むことができます。



  @Test public void orangeIsRoundWithMatcher() { assertThat(someFruit, is(round())); } @Test public void orangeIsSweetWithMatcher() { assertThat(someFruit, is(sweet())); } @Test public void orangeHasColorWithMatcher() { assertThat(someFruit, hasColor(Color.ORANGE)); }
      
      







そのような美しさのために、特別なHamcrestライブラリがあります。 実装用のインターフェースと、 assertThatメソッドassumeThatメソッドの両方が含まれています(後者は実際にはJUnit内にありますが、Hamcrestのインターフェースを使用しています)。 ゲーマーにオブジェクトについて尋ね、決定を下します。



バージョン4.11以降、JUnitの依存関係では、Hamcrestライブラリのバージョンは1.3以上です。 さらに説明されているすべてのものが実装されるインターフェースを導入したのは彼女でした。 したがって、Mavenを使用すると、JUnit 4.11を接続するだけで十分であり、最低限必要なツールセットを使用する準備ができています。 そして、Hamcrestの配信で利用可能なすべてのプレーヤーの完全なセットには、hamcrest-allアーティファクトが必要になります。これは個別に接続できます。



これはあなたのポンポンのように見えるかもしれません



どのように機能しますか?



ライブラリには、 TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit) — , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .








た抽象クラスTypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit) — , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .








TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




 TypeSafeMatcher,     .      : 
      



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




 TypeSafeMatcher,     .      : 
      



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




 TypeSafeMatcher,     .      : 
      



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




 TypeSafeMatcher,     .      : 
      



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .




TypeSafeMatcher, . :



public boolean matchesSafely(Fruit fruit)




— , public void describeTo(Description description)



— , protected void describeMismatchSafely(Fruit item, Description mismatchDescription)



— .



, , — null



.



, , , :



public class ShapeMatcher extends TypeSafeMatcher<Fruit> { private Shape expected; public ShapeMatcher(Shape expected) { this.expected = expected; } @Override public boolean matchesSafely(Fruit fruit) { return expected.equals(fruit.getShape()); } @Override protected void describeMismatchSafely(Fruit item, Description mismatchDescription) { mismatchDescription.appendText("fruit has shape - ").appendValue(item.getShape()); } @Override public void describeTo(Description description) { description.appendText("shape - ").appendValue(expected); } @Factory public static ShapeMatcher round() { return new ShapeMatcher(Shape.ROUND); } }







. , , , , — !





, , , , . — . Java FeatureMatcher<WhatWeGet, WhatWeWannaCheck>



, : .



3 :



, WhatWeWannaCheck , ( ), ( - )

, f eatureValueOf



. Hamcrest .



, , .



public class Matchers { public static Matcher<Fruit> hasShape(final Shape shape) { return new FeatureMatcher<Fruit, Shape>(equalTo(shape), "fruit has shape - ", "shape -") { @Override protected Shape featureValueOf(Fruit fruit) { return fruit.getShape(); } }; } public static Matcher<Fruit> round() { return hasShape(Shape.ROUND); } public static Matcher<Fruit> sweet() { return new FeatureMatcher<Fruit, Boolean>(is(true), "fruit should be sweet", "sweet -") { @Override protected Boolean featureValueOf(Fruit fruit) { return fruit.isSweet(); } }; } public static Matcher<Fruit> hasColor(Color color) { return new FeatureMatcher<Fruit, Color>(equalTo(color), "fruit have color - ", "color -") { @Override protected String featureValueOf(Fruit fruit) { return fruit.getColor(); } }; } }







Feel the POWER OF MATCHERS

— . Hamcrest : allOf, anyOf, both, either. , .



, «» :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assumeThat(someFruit, both(round()).and(sweet()).and(hasColor(Color.ORANGE))); // }







.



, , — . , . — :

@Test public void orangeBothSweetRoundAndOrangeColorWithMatchers() throws Exception { assertThat(someFruitList, everyItem(both(round()).and(sweet()).and(hasColor(Color.ORANGE)))); }





, : . — , , hasItem()



.



.





, . , — , - . , , — , . , -. .



github.com/yandex-qatools/matchers-java .







All Articles