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

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

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
1package Sample;
2
3import static org.hamcrest.CoreMatchers.allOf;
4import static org.hamcrest.CoreMatchers.anything;
5import static org.hamcrest.CoreMatchers.endsWith;
6import static org.hamcrest.CoreMatchers.hasItem;
7import static org.hamcrest.CoreMatchers.is;
8import static org.hamcrest.CoreMatchers.not;
9import static org.hamcrest.CoreMatchers.notNullValue;
10import static org.hamcrest.CoreMatchers.nullValue;
11import static org.hamcrest.CoreMatchers.startsWith;
12import static org.junit.Assert.*;
13
14import java.util.Arrays;
15import java.util.Collection;
16import java.util.HashSet;
17import java.util.Set;
18
19import org.junit.Test;
20
21public class combinational_collection_assert {
22
23	@Test
24	public void a_StringMatchers() {  
25		String tested="TEKSYSTEMS";
26		String check ="EKS";
27		assertThat("MATCHING",tested, anything(check));
28	}
29	@Test
30	public void b_NullMatchers() {  
31		String tested=null;
32		assertThat(" Null MATCHING",tested,nullValue());
33	}
34	@Test
35	public void c_NotNullMatchers() {  
36		String tested="";
37		assertThat(" Null MATCHING",tested,notNullValue());
38	}
39	@Test
40	public void d_STRINGSameMatchers() {  
41		String tested="jharna";
42		String check="jharna";
43		assertThat(" MATCHING",tested,is(check));
44	}
45	@Test
46	public void e_STRINGNotSameMatchers() {  
47		String tested="jharna";
48		String check="jharna1234";
49		assertThat(" MATCHING",tested,not(check));
50	}
51	//--------------------------------combination assert---------------------------------
52	@Test
53	public void f_CombinationSTRINGNotSameMatchers() {  
54		String tested="[email protected]";
55			assertThat(" MATCHING",tested,allOf(startsWith("!"),endsWith("@")));
56					
57	}
58	//-------------------------------Collection Assert---------------------
59	
60	@Test
61	public void f_CoLLECTIONSTRINGSameMatchers() {  
62	String[] testArray= {"a","b","c"};
63	Collection<String> tested=Arrays.asList(testArray);
64	assertThat("MATCHES" ,tested, hasItem("b"));
65}
66	@Test
67	public void g_CoLLECTIONSTRINGSameMatchers() {
68	String[] testArray={"mate","rate"};
69	Set<String> tested=new HashSet<String>(Arrays.asList(testArray));
70	assertThat(tested,hasItem(endsWith("ate")));
71	}
72	
73	@Test
74	public void h_CoLLECTIONSTRINGSameMatchers() {
75	String[] testArray={"teksystems","tekstsav"};
76	Set<String> tested=new HashSet<String>(Arrays.asList(testArray));
77	assertThat(tested,hasItem(startsWith("tek")));
78	}
79
80}
81
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 not code on LambdaTest Cloud Grid

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