How to use allOf method of org.hamcrest.CoreMatchers class

Best junit code snippet using org.hamcrest.CoreMatchers.allOf

Run junit automation tests on LambdaTest cloud grid

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

copy
1package es.polgomez.exploringunittestingfw;
2
3import org.junit.Test;
4
5import java.util.Arrays;
6import java.util.List;
7
8import static org.hamcrest.CoreMatchers.allOf;
9import static org.hamcrest.CoreMatchers.anyOf;
10import static org.hamcrest.CoreMatchers.anything;
11import static org.hamcrest.CoreMatchers.both;
12import static org.hamcrest.CoreMatchers.containsString;
13import static org.hamcrest.CoreMatchers.describedAs;
14import static org.hamcrest.CoreMatchers.either;
15import static org.hamcrest.CoreMatchers.endsWith;
16import static org.hamcrest.CoreMatchers.equalTo;
17import static org.hamcrest.CoreMatchers.everyItem;
18import static org.hamcrest.CoreMatchers.hasItem;
19import static org.hamcrest.CoreMatchers.hasItems;
20import static org.hamcrest.CoreMatchers.is;
21import static org.hamcrest.CoreMatchers.isA;
22import static org.hamcrest.CoreMatchers.not;
23import static org.hamcrest.CoreMatchers.nullValue;
24import static org.hamcrest.CoreMatchers.sameInstance;
25import static org.hamcrest.CoreMatchers.startsWith;
26import static org.hamcrest.CoreMatchers.theInstance;
27import static org.junit.Assert.assertThat;
28
29/**
30 * Exploring assertThat assertion using Harmcrest matchers
31 */
32public class AssertThatTest {
33
34    @Test
35    public void testAssertThat() {
36        String string = "String";
37        assertThat(string, is("String")); // This assertions does the same
38        assertThat(string, equalTo("String"));
39        assertThat(string, is(equalTo("String")));
40    }
41
42    @Test
43    public void testAssertThatChain() {
44        String string = "String";
45        assertThat(string, allOf(containsString("ing"), startsWith("S"), endsWith("g")));
46        assertThat(string, anyOf(containsString("tr"), startsWith("A"), endsWith("g")));
47        assertThat(string, not(allOf(containsString("tr"), startsWith("A"), endsWith("g"))));
48        assertThat(string, both(startsWith("S")).and(endsWith("g")));
49        assertThat(string, both(startsWith("S")).and(endsWith("s")).or(endsWith("g")));
50        assertThat(string, either(startsWith("X")).or(startsWith("S")).or(endsWith("g")));
51    }
52
53
54    @Test
55    public void testVerboseHamcrestMatcher() {
56        String string = "S";
57        // This assertion if fails return a better description as:
58        // java.lang.AssertionError: Expected: a String that start with "S" but: was "Foo"
59        assertThat(string, describedAs("a String that start with %0", startsWith("S"), "S"));
60    }
61
62    @Test
63    public void testSimpleHamcrestMatcher() {
64        // Creates a matcher that always matches, regardless of the examined object.
65        assertThat(null, anything());
66
67        assertThat(null, nullValue());
68        assertThat(null, is(nullValue()));
69
70        Object actual = new Object();
71        Object expected = actual;
72        assertThat(actual, isA(Object.class));
73        assertThat(actual, sameInstance(expected));
74        assertThat(actual, theInstance(expected));
75        assertThat(actual, not(sameInstance(new Object())));
76    }
77
78    @Test
79    public void testArrayHamcrestMatcher() {
80        List<String> strings = Arrays.asList("String", "Strong", "Street");
81
82        assertThat(strings, everyItem(isA(String.class)));
83        assertThat(strings, everyItem(startsWith("S")));
84        assertThat(strings, hasItem("Strong"));
85        assertThat(strings, hasItem(is("Street")));
86        assertThat(strings, hasItems("Street", "String"));
87        assertThat(strings, hasItems(endsWith("g"), endsWith("t")));
88    }
89}
90
Full Screen
copy
1/*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements.  See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21package com.manning.junitbook.ch02.hamcrest;
22
23import org.junit.jupiter.api.DisplayName;
24import org.junit.jupiter.api.Test;
25
26import static org.hamcrest.CoreMatchers.anyOf;
27import static org.hamcrest.CoreMatchers.allOf;
28import static org.hamcrest.CoreMatchers.is;
29import static org.hamcrest.CoreMatchers.notNullValue;
30import static org.hamcrest.CoreMatchers.nullValue;
31import static org.hamcrest.Matchers.*;
32import static org.hamcrest.MatcherAssert.assertThat;
33
34public class HamcrestMatchersTest {
35
36    private static String FIRST_NAME = "John";
37    private static String LAST_NAME = "Smith";
38    private static Customer customer = new Customer(FIRST_NAME, LAST_NAME);
39
40
41    @Test
42    @DisplayName("Hamcrest is, anyOf, allOf")
43    public void testHamcrestIs() {
44        int price1 = 1, price2 = 1, price3 = 2;
45
46        assertThat(1, is(price1));
47        assertThat(1, anyOf(is(price2), is(price3)));
48        assertThat(1, allOf(is(price1), is(price2)));
49    }
50
51    @Test
52    @DisplayName("Null expected")
53    void testNull() {
54        assertThat(null, nullValue());
55    }
56
57    @Test
58    @DisplayName("Object expected")
59    void testNotNull() {
60        assertThat(customer, notNullValue());
61    }
62
63    @Test
64    @DisplayName("Check correct customer properties")
65    void checkCorrectCustomerProperties() {
66        assertThat(customer, allOf(
67                hasProperty("firstName", is(FIRST_NAME)),
68                hasProperty("lastName", is(LAST_NAME))
69        ));
70    }
71
72}
73
Full Screen
copy
1package hu.blackbelt.judo.generator.parser.derived.expression.sql;
2
3import hu.blackbelt.judo.generator.parser.derived.expression.sql.model.ExpressionPart;
4import org.junit.Test;
5
6import java.util.List;
7
8import static org.hamcrest.CoreMatchers.allOf;
9import static org.hamcrest.CoreMatchers.equalTo;
10import static org.hamcrest.CoreMatchers.nullValue;
11import static org.hamcrest.Matchers.hasProperty;
12import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
13import static org.hamcrest.junit.MatcherAssert.assertThat;
14
15public class DerivedAttributeExpressionToRdbmsModelParserTest {
16
17
18    public void testRet(List<ExpressionPart> ret) {
19        assertThat(ret, hasSize(4));
20
21        assertThat(ret.get(0), allOf(
22                hasProperty("labelExpression",
23                        hasProperty("text", equalTo("Part 1:"))),
24                hasProperty("relationExpression", nullValue())
25        ));
26
27        assertThat(ret.get(1), allOf(
28                hasProperty("labelExpression", nullValue()),
29                hasProperty("relationExpression", allOf(
30                        hasProperty("relationName", equalTo("a")),
31                        hasProperty("relationExpression", allOf(
32                                hasProperty("relationName", equalTo("b")),
33                                hasProperty("relationExpression", allOf(
34                                        hasProperty("relationName", equalTo("c")),
35                                        hasProperty("relationExpression", allOf(
36                                                hasProperty("relationName", equalTo("d")),
37                                                hasProperty("relationExpression", nullValue())
38                                        ))
39                                ))
40                        ))
41                ))
42        ));
43
44        assertThat(ret.get(2), allOf(
45                hasProperty("labelExpression",
46                        hasProperty("text", equalTo("Part 2:"))),
47                hasProperty("relationExpression", nullValue())
48        ));
49
50        assertThat(ret.get(3), allOf(
51                hasProperty("labelExpression", nullValue()),
52                hasProperty("relationExpression", allOf(
53                        hasProperty("relationName", equalTo("g")),
54                        hasProperty("relationExpression", allOf(
55                                hasProperty("relationName", equalTo("h")),
56                                hasProperty("relationExpression", nullValue())
57                        ))
58                ))
59        ));
60
61    }
62    @Test
63    public void testParse() {
64        testRet(new DerivedAttributeExpressionToRdbmsModelParser().parseExpression("'Part 1:' {self.a.b.c.d} 'Part 2:' {self.g.h}"));
65        testRet(new DerivedAttributeExpressionToRdbmsModelParser().parseExpression("'Part 1:' self.a.b.c.d 'Part 2:' self.g.h"));
66        new DerivedAttributeExpressionToRdbmsModelParser().parseExpression("{self.reqDataTypeDef.name} - {self.firstOccurence}, {self.repetitionRate}, {self.repetitionUnit} \"");
67    }
68
69}
70
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

Trigger allOf code on LambdaTest Cloud Grid

Execute automation tests with allOf 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)