πŸ“œ ⬆️ ⬇️

Testing in Yandex. Matchers: when they are useful and how easy it is to use them

Oranges have nothing to do with it As you could guess from the picture on the right, we’ll talk about automated testing. More precisely about such technology as matchers. They help to seriously reduce duplication of code and simplify the code of tests for perception, and creating and using matchmakers is quite simple.

By itself, the technology of the matchrs is not new - in its current form, it was poured into the repository in July 2012, but it appeared even earlier. But despite this, many people have not heard about it yet or are avoiding it for some reason. We want to tell how easy it is to take advantage of its use, and share with you our library of matchmakers.

Suppose we have a set of some fruits. Among them, the most common round, orange and sweet.

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

')
It is known that an orange meets these conditions. In addition to fruit, we also have a conveyor through which such fruit can be passed. There is also a task for the conveyor - weed out no oranges by conducting a series of tests.

And here is luck - we just had an apparatus on hand that knows how to determine if a fruit is sweet and what color it is, compare its shape with a number of well-known ones and carry out many more checks. This device is called JUnit .

Before starting the test, a new fruit is thrown onto the conveyor.

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


First we determine that the fruit is round.

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


Then that fruit is sweet.

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


And finally, let's look at its color.

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


Which cons are immediately obvious? First, in each of the checks it was necessary to stick together a comment from the expected value, actual value, value of additional refinements and different interjections. Even the description of this list is tiring.

Secondly, imagine that we decided to define also a sort of oranges, added a special β€œtaster” for this and told him: β€œApprove only the sortβ€œ Valencia ””. But the "taster" does not know that he decided to check the machine, if he does not ask himself - the machine is not talkative. As a result, he will try everything. In order not to poison the β€œtaster” with a ball that you did not have time to check for sweetness, you need to teach him to ignore everything that is superfluous. To do this, he needs to ask the machine separately and independently, after which everything discarded is put aside and no longer touch.

A taster is just one more test in our JUnit machine, so you can and should use the built-in runtime mechanism to ignore the test - assume. Then the scenario of starting the tasting will look like this.

  @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)); //        . } 


It is well seen that each new β€œtaster” with this scenario description has two ways - copy-paste the script or make a new method for each set of pre-checks. And in each pretesting you need to take care of drawing up a message about the reason for rejection. Any of these options is difficult to maintain and very scary to perceive.

On top of that, you have to assertEquals , assertNotEquals , assertNotNull , assertArrayEquals , etc. In the standard JUnit distribution, these assert * are in almost every trivial case. And some more - for each type of argument. That is, the verification logic is enclosed in the name of the method and is strictly tied to its implementation. Now imagine how much code would need to be duplicated and maintained, if for each assert * you had to do the same assume *.

So, it is necessary to separate the logic of the test itself, the description output and the decision function:


This is where the gamers come in - small objects that contain decision-making logic, they know what they were waiting for and what they got, what they report on themselves. The first three checks with the help of matchers describe the action almost poetically. And it can be read by anyone who wants to know what the script is doing.

  @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)); } 


For such beauty, there is a special library Hamcrest . It contains both the interface for the implementation, and the methods of assertThat and assumeThat (the latter, in fact, is inside JUnit, but uses the interface from Hamcrest). They ask the matcher about the object, making a decision.

Starting with version 4.11, Hamcrest has a version not lower than 1.3 in JUnit dependencies. It was she who introduced the interface, which implements everything described later. Therefore, using maven, it is enough to connect JUnit 4.11, and the minimum necessary set of tools is ready to use. And for a complete set of all available matchrs from the Hamcrest delivery, you will need the hamcrest-all artifact, which can be connected separately.

This is what your pom might look like .

How it works?


The library has an abstract class 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 .

Source: https://habr.com/ru/post/184634/


All Articles