How to use hasItems method of org.junit.matchers.JUnitMatchers class

Best junit code snippet using org.junit.matchers.JUnitMatchers.hasItems

Run junit automation tests on LambdaTest cloud grid

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

copy
1
2import org.junit.After;
3import org.junit.Assert;
4import org.junit.Before;
5import org.junit.Test;
6
7import static org.hamcrest.CoreMatchers.allOf;
8import static org.hamcrest.CoreMatchers.anyOf;
9import static org.hamcrest.CoreMatchers.equalTo;
10import static org.hamcrest.CoreMatchers.not;
11import static org.hamcrest.CoreMatchers.sameInstance;
12import static org.hamcrest.CoreMatchers.startsWith;
13import static org.junit.Assert.*;
14import static org.junit.matchers.JUnitMatchers.both;
15import static org.junit.matchers.JUnitMatchers.containsString;
16import static org.junit.matchers.JUnitMatchers.everyItem;
17import static org.junit.matchers.JUnitMatchers.hasItems;
18
19public class MyMathTest {
20    @Test
21    public void testArrayEqual(){
22        byte[] exp = "trial".getBytes();
23        byte[] actual = "trial".getBytes();
24        assertArrayEquals("test",exp,actual);
25    }
26
27    @Test
28    public void testEquals(){
29        assertEquals("test equal","test","test");
30    }
31
32    @Test
33    public void testAssertFalse(){
34        Assert.assertFalse("asd",true);
35    }
36
37    @Test
38    public void testAssetNotNull(){
39        assertNotNull(123);
40    }
41}
42
Full Screen
copy
1package com.sicongtang.junit.basic;
2
3import static org.hamcrest.CoreMatchers.allOf;
4import static org.hamcrest.CoreMatchers.anyOf;
5import static org.hamcrest.CoreMatchers.equalTo;
6import static org.hamcrest.CoreMatchers.not;
7import static org.hamcrest.CoreMatchers.sameInstance;
8import static org.hamcrest.CoreMatchers.startsWith;
9import static org.junit.matchers.JUnitMatchers.both;
10import static org.junit.matchers.JUnitMatchers.containsString;
11import static org.junit.matchers.JUnitMatchers.everyItem;
12import static org.junit.matchers.JUnitMatchers.hasItems;
13
14import java.util.Arrays;
15
16import org.hamcrest.core.CombinableMatcher;
17import org.junit.FixMethodOrder;
18import org.junit.Test;
19import org.junit.runners.MethodSorters;
20
21import static org.junit.Assert.*;
22
23//@FixMethodOrder(MethodSorters.JVM)
24// Leaves the test methods in the order returned by the JVM. This order may vary from run to run.
25@FixMethodOrder(MethodSorters.NAME_ASCENDING)
26// Sorts the test methods by method name, in lexicographic order.
27public class AssertTests {
28	@Test
29	public void testAssertArrayEquals() {
30		byte[] expected = "trial".getBytes();
31		byte[] actual = "trial".getBytes();
32		assertArrayEquals("failure - byte arrays not same", expected, actual);
33	}
34
35	@Test
36	public void testAssertEquals() {
37		assertEquals("failure - strings are not equal", "text", "text");
38	}
39
40	@Test
41	public void testAssertFalse() {
42		assertFalse("failure - should be false", false);
43	}
44
45	@Test
46	public void testAssertNotNull() {
47		assertNotNull("should not be null", new Object());
48	}
49
50	@Test
51	public void testAssertNotSame() {
52		assertNotSame("should not be same Object", new Object(), new Object());
53	}
54
55	@Test
56	public void testAssertNull() {
57		assertNull("should be null", null);
58	}
59
60	@Test
61	public void testAssertSame() {
62		Integer aNumber = Integer.valueOf(768);
63		assertSame("should be same", aNumber, aNumber);
64	}
65
66	// JUnit Matchers assertThat
67	@Test
68	public void testAssertThatBothContainsString() {
69		assertThat("albumen", both(containsString("a")).and(containsString("b")));
70	}
71
72	@Test
73	public void testAssertThathasItemsContainsString() {
74		assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
75	}
76
77	@Test
78	public void testAssertThatEveryItemContainsString() {
79		org.junit.Assert
80				.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
81	}
82
83	// Core Hamcrest Matchers with assertThat
84	@Test
85	public void testAssertThatHamcrestCoreMatchers() {
86		assertThat("good", allOf(equalTo("good"), startsWith("good")));
87		assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
88		assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
89		assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));
90		assertThat(new Object(), not(sameInstance(new Object())));
91	}
92
93	@Test
94	public void testAssertTrue() {
95		assertTrue("failure - should be true", true);
96	}
97}
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 hasItems code on LambdaTest Cloud Grid

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