How to use CombinableMatcher class of org.hamcrest.core package

Best junit code snippet using org.hamcrest.core.CombinableMatcher

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
copy
1package org.hamcrest;
2
3import org.hamcrest.core.AllOf;
4import org.hamcrest.core.AnyOf;
5import org.hamcrest.core.CombinableMatcher;
6import org.hamcrest.core.CombinableMatcher.CombinableBothMatcher;
7import org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher;
8import org.hamcrest.core.DescribedAs;
9import org.hamcrest.core.Every;
10import org.hamcrest.core.Is;
11import org.hamcrest.core.IsAnything;
12import org.hamcrest.core.IsCollectionContaining;
13import org.hamcrest.core.IsEqual;
14import org.hamcrest.core.IsInstanceOf;
15import org.hamcrest.core.IsNot;
16import org.hamcrest.core.IsNull;
17import org.hamcrest.core.IsSame;
18import org.hamcrest.core.StringContains;
19import org.hamcrest.core.StringEndsWith;
20import org.hamcrest.core.StringStartsWith;
21
22public class CoreMatchers {
23    public static <T> Matcher<T> allOf(Iterable<Matcher<? super T>> matchers) {
24        return AllOf.allOf((Iterable) matchers);
25    }
26
27    @SafeVarargs
28    public static <T> Matcher<T> allOf(Matcher<? super T>... matchers) {
29        return AllOf.allOf((Matcher[]) matchers);
30    }
31
32    public static <T> AnyOf<T> anyOf(Iterable<Matcher<? super T>> matchers) {
33        return AnyOf.anyOf((Iterable) matchers);
34    }
35
36    @SafeVarargs
37    public static <T> AnyOf<T> anyOf(Matcher<? super T>... matchers) {
38        return AnyOf.anyOf((Matcher[]) matchers);
39    }
40
41    public static <LHS> CombinableBothMatcher<LHS> both(Matcher<? super LHS> matcher) {
42        return CombinableMatcher.both(matcher);
43    }
44
45    public static <LHS> CombinableEitherMatcher<LHS> either(Matcher<? super LHS> matcher) {
46        return CombinableMatcher.either(matcher);
47    }
48
49    public static <T> Matcher<T> describedAs(String description, Matcher<T> matcher, Object... values) {
50        return DescribedAs.describedAs(description, matcher, values);
51    }
52
53    public static <U> Matcher<Iterable<? extends U>> everyItem(Matcher<U> itemMatcher) {
54        return Every.everyItem(itemMatcher);
55    }
56
57    public static <T> Matcher<T> is(Matcher<T> matcher) {
58        return Is.is((Matcher) matcher);
59    }
60
61    public static <T> Matcher<T> is(T value) {
62        return Is.is((Object) value);
63    }
64
65    public static void is(Class<?> cls) {
66    }
67
68    public static <T> Matcher<T> isA(Class<T> type) {
69        return Is.isA(type);
70    }
71
72    public static Matcher<Object> anything() {
73        return IsAnything.anything();
74    }
75
76    public static Matcher<Object> anything(String description) {
77        return IsAnything.anything(description);
78    }
79
80    public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher) {
81        return IsCollectionContaining.hasItem((Matcher) itemMatcher);
82    }
83
84    public static <T> Matcher<Iterable<? super T>> hasItem(T item) {
85        return IsCollectionContaining.hasItem((Object) item);
86    }
87
88    @SafeVarargs
89    public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers) {
90        return IsCollectionContaining.hasItems((Matcher[]) itemMatchers);
91    }
92
93    @SafeVarargs
94    public static <T> Matcher<Iterable<T>> hasItems(T... items) {
95        return IsCollectionContaining.hasItems((Object[]) items);
96    }
97
98    public static <T> Matcher<T> equalTo(T operand) {
99        return IsEqual.equalTo(operand);
100    }
101
102    public static Matcher<Object> equalToObject(Object operand) {
103        return IsEqual.equalToObject(operand);
104    }
105
106    public static <T> Matcher<T> any(Class<T> type) {
107        return IsInstanceOf.any(type);
108    }
109
110    public static <T> Matcher<T> instanceOf(Class<?> type) {
111        return IsInstanceOf.instanceOf(type);
112    }
113
114    public static <T> Matcher<T> not(Matcher<T> matcher) {
115        return IsNot.not((Matcher) matcher);
116    }
117
118    public static <T> Matcher<T> not(T value) {
119        return IsNot.not((Object) value);
120    }
121
122    public static Matcher<Object> notNullValue() {
123        return IsNull.notNullValue();
124    }
125
126    public static <T> Matcher<T> notNullValue(Class<T> type) {
127        return IsNull.notNullValue(type);
128    }
129
130    public static Matcher<Object> nullValue() {
131        return IsNull.nullValue();
132    }
133
134    public static <T> Matcher<T> nullValue(Class<T> type) {
135        return IsNull.nullValue(type);
136    }
137
138    public static <T> Matcher<T> sameInstance(T target) {
139        return IsSame.sameInstance(target);
140    }
141
142    public static <T> Matcher<T> theInstance(T target) {
143        return IsSame.theInstance(target);
144    }
145
146    public static Matcher<String> containsString(String substring) {
147        return StringContains.containsString(substring);
148    }
149
150    public static Matcher<String> containsStringIgnoringCase(String substring) {
151        return StringContains.containsStringIgnoringCase(substring);
152    }
153
154    public static Matcher<String> startsWith(String prefix) {
155        return StringStartsWith.startsWith(prefix);
156    }
157
158    public static Matcher<String> startsWithIgnoringCase(String prefix) {
159        return StringStartsWith.startsWithIgnoringCase(prefix);
160    }
161
162    public static Matcher<String> endsWith(String suffix) {
163        return StringEndsWith.endsWith(suffix);
164    }
165
166    public static Matcher<String> endsWithIgnoringCase(String suffix) {
167        return StringEndsWith.endsWithIgnoringCase(suffix);
168    }
169}
170
Full Screen
copy
1import org.hamcrest.Description;
2import org.hamcrest.TypeSafeMatcher;
3
4public class CustomMatcher extends TypeSafeMatcher<CustomException> {
5
6    public static CustomMatcher hasCode(String item) {
7        return new CustomMatcher(item);
8    }
9
10    private String foundErrorCode;
11    private final String expectedErrorCode;
12
13    private CustomMatcher(String expectedErrorCode) {
14        this.expectedErrorCode = expectedErrorCode;
15    }
16
17    @Override
18    protected boolean matchesSafely(final CustomException exception) {
19        foundErrorCode = exception.getErrorCode();
20        return foundErrorCode.equalsIgnoreCase(expectedErrorCode);
21    }
22
23    @Override
24    public void describeTo(Description description) {
25        description.appendValue(foundErrorCode)
26                .appendText(" was not found instead of ")
27                .appendValue(expectedErrorCode);
28    }
29}
30
Full Screen
copy
1import org.junit.rules.ExpectedException;
2
3public class MyObjTest {
4
5    @Rule
6    public ExpectedException thrown = ExpectedException.none();
7
8    @Test
9    public void someMethodThatThrowsCustomException() {
10        thrown.expect(CustomException.class);
11        thrown.expect(CustomMatcher.hasCode("110501"));
12
13        MyObj obj = new MyObj();
14        obj.methodThatThrowsCustomException();
15    }
16}
17
Full Screen
copy
1thrown.expect(CombinableMatcher.both(
2           CoreMatchers.is(CoreMatchers.instanceOf(MyExceptionClass.class)))
3           .and(Matchers.hasProperty("errorCode", CoreMatchers.is(123))));
4
Full Screen
copy
1thrown.expect(CoreMatchers.instanceOf(MyExceptionClass.class));
2thrown.expect(Matchers.hasProperty("errorCode", CoreMatchers.is(123));
3
Full Screen
copy
1 @Test 
2 public void whenSerialNumberIsEmpty_shouldThrowSerialNumberInvalid() throws Exception {
3  try{
4     whenRunningSomething_shouldThrowMyExceptionWithInternalErrorCode();     
5     fail("should have thrown");
6  }
7  catch (MyExceptionClass e){
8     assertThat(e.getCode(), is(MyExceptionClass.INTERNAL_ERROR_CODE));
9  }
10
Full Screen
copy
1assertThat(BigDecimal.ONE, 
2    is(both(not(nullValue()))
3        .and(not(comparesEqualTo(BigDecimal.ZERO)))));
4         ^---The method and(Matcher<? super Object>) in the type 
5             CombinableMatcher.CombinableBothMatcher<Object> is not
6             applicable for the arguments (Matcher<BigDecimal>)
7
Full Screen
copy
1assertThat(BigDecimal.ONE, 
2    is(both(not(nullValue(BigDecimal.class)))
3        .and(not(comparesEqualTo(BigDecimal.ZERO)))));
4
Full Screen
copy
1assertThat(parentDecisionGroups, hasItem(
2               both(hasProperty("id", equalTo(decisionGroup1.getId()))).
3               and(hasProperty("ownerDecision", equalTo("decision1"))));
4
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 methods in CombinableMatcher

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)