How to use describeMismatch method of org.hamcrest.core.Is class

Best junit code snippet using org.hamcrest.core.Is.describeMismatch

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package org.hamcrest.core;
2
3import org.hamcrest.StringDescription;
4import org.junit.Assert;
5import org.junit.Test;
6
7import static org.hamcrest.MatcherAssert.assertThat;
8import static org.hamcrest.core.CombinableMatcher.both;
9import static org.hamcrest.core.IsEqual.equalTo;
10import static org.hamcrest.core.IsNot.not;
11import static org.hamcrest.core.IsNull.notNullValue;
12import static org.hamcrest.number.OrderingComparison.greaterThan;
13import static org.junit.Assert.assertEquals;
14
15public class CombinableTest {
16    private static final CombinableMatcher<Integer> EITHER_3_OR_4 = CombinableMatcher.<Integer>either(equalTo(3)).or(equalTo(4));
17    private static final CombinableMatcher<Integer> NOT_3_AND_NOT_4 = CombinableMatcher.<Integer>both(not(equalTo(3))).and(not(equalTo(4)));
18
19    @Test
20    public void bothAcceptsAndRejects() {
21        assertThat(2, NOT_3_AND_NOT_4);
22        assertThat(3, not(NOT_3_AND_NOT_4));
23    }
24
25    @Test
26    public void acceptsAndRejectsThreeAnds() {
27        CombinableMatcher<? super Integer> tripleAnd = NOT_3_AND_NOT_4.and(equalTo(2));
28        assertThat(2, tripleAnd);
29        assertThat(3, not(tripleAnd));
30    }
31
32
33    @Test
34    public void bothDescribesItself() {
35        assertEquals("(not <3> and not <4>)", NOT_3_AND_NOT_4.toString());
36
37        StringDescription mismatch = new StringDescription();
38        NOT_3_AND_NOT_4.describeMismatch(3, mismatch);
39        assertEquals("was <3>", mismatch.toString());
40    }
41
42    @Test
43    public void eitherAcceptsAndRejects() {
44        assertThat(3, EITHER_3_OR_4);
45        assertThat(6, not(EITHER_3_OR_4));
46    }
47
48    @Test
49    public void acceptsAndRejectsThreeOrs() {
50        final CombinableMatcher<Integer> orTriple = EITHER_3_OR_4.or(greaterThan(10));
51        assertThat(11, orTriple);
52        assertThat(9, not(orTriple));
53    }
54
55    @Test
56    public void eitherDescribesItself() {
57        Assert.assertEquals("(<3> or <4>)", EITHER_3_OR_4.toString());
58        StringDescription mismatch = new StringDescription();
59
60        EITHER_3_OR_4.describeMismatch(6, mismatch);
61        Assert.assertEquals("was <6>", mismatch.toString());
62    }
63
64    @Test
65    public void picksUpTypeFromLeftHandSideOfExpression() {
66        assertThat("yellow", both(equalTo("yellow")).and(notNullValue()));
67    }
68}
69
Full Screen

Source: Is.java Github

copy
1package org.hamcrest.core;
2
3import org.hamcrest.BaseMatcher;
4import org.hamcrest.Description;
5import org.hamcrest.Matcher;
6
7import static org.hamcrest.core.IsEqual.equalTo;
8import static org.hamcrest.core.IsInstanceOf.instanceOf;
9
10/**
11 * Decorates another Matcher, retaining the behaviour but allowing tests
12 * to be slightly more expressive.
13 *
14 * For example:  assertThat(cheese, equalTo(smelly))
15 *          vs.  assertThat(cheese, is(equalTo(smelly)))
16 */
17public class Is<T> extends BaseMatcher<T> {
18    private final Matcher<T> matcher;
19
20    public Is(Matcher<T> matcher) {
21        this.matcher = matcher;
22    }
23
24    @Override
25    public boolean matches(Object arg) {
26        return matcher.matches(arg);
27    }
28
29    @Override
30    public void describeTo(Description description) {
31        description.appendText("is ").appendDescriptionOf(matcher);
32    }
33
34    @Override
35    public void describeMismatch(Object item, Description mismatchDescription) {
36        matcher.describeMismatch(item, mismatchDescription);
37    }
38
39    /**
40     * Decorates another Matcher, retaining its behaviour, but allowing tests
41     * to be slightly more expressive.
42     * For example:
43     * <pre>assertThat(cheese, is(equalTo(smelly)))</pre>
44     * instead of:
45     * <pre>assertThat(cheese, equalTo(smelly))</pre>
46     * 
47     */
48    public static <T> Matcher<T> is(Matcher<T> matcher) {
49        return new Is<T>(matcher);
50    }
51
52    /**
53     * A shortcut to the frequently used <code>is(equalTo(x))</code>.
54     * For example:
55     * <pre>assertThat(cheese, is(smelly))</pre>
56     * instead of:
57     * <pre>assertThat(cheese, is(equalTo(smelly)))</pre>
58     * 
59     */
60    public static <T> Matcher<T> is(T value) {
61        return is(equalTo(value));
62    }
63
64    /**
65     * A shortcut to the frequently used <code>is(instanceOf(SomeClass.class))</code>.
66     * For example:
67     * <pre>assertThat(cheese, isA(Cheddar.class))</pre>
68     * instead of:
69     * <pre>assertThat(cheese, is(instanceOf(Cheddar.class)))</pre>
70     * 
71     */
72    public static <T> Matcher<T> isA(Class<T> type) {
73        final Matcher<T> typeMatcher = instanceOf(type);
74        return is(typeMatcher);
75    }
76}
77
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in Is

Trigger describeMismatch code on LambdaTest Cloud Grid

Execute automation tests with describeMismatch on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)