public class Fruit { ... public Color getColor() {...} public boolean isSweet() {...} public Shape getShape() {...} }
@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); }
@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)); // . }
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 *. @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)); }
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 .
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