Best junit code snippet using org.hamcrest.CoreMatchers.hasItem
Source:JUnitMatchers.java
...12 */13public class JUnitMatchers {14 /**15 * @return A matcher matching any collection containing element16 * @deprecated Please use {@link CoreMatchers#hasItem(Object)} instead.17 */18 @Deprecated19 public static <T> Matcher<Iterable<? super T>> hasItem(T element) {20 return CoreMatchers.hasItem(element);21 }22 /**23 * @return A matcher matching any collection containing an element matching elementMatcher24 * @deprecated Please use {@link CoreMatchers#hasItem(Matcher)} instead.25 */26 @Deprecated27 public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> elementMatcher) {28 return CoreMatchers.<T>hasItem(elementMatcher);29 }30 /**31 * @return A matcher matching any collection containing every element in elements32 * @deprecated Please use {@link CoreMatchers#hasItems(Object...)} instead.33 */34 @Deprecated35 public static <T> Matcher<Iterable<T>> hasItems(T... elements) {36 return CoreMatchers.hasItems(elements);37 }38 /**39 * @return A matcher matching any collection containing at least one element that matches40 * each matcher in elementMatcher (this may be one element matching all matchers,41 * or different elements matching each matcher)42 * @deprecated Please use {@link CoreMatchers#hasItems(Matcher...)} instead.43 */44 @Deprecated45 public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMatchers) {46 return CoreMatchers.hasItems(elementMatchers);47 }48 /**49 * @return A matcher matching any collection in which every element matches elementMatcher50 * @deprecated Please use {@link CoreMatchers#everyItem(Matcher)} instead.51 */52 @Deprecated53 public static <T> Matcher<Iterable<T>> everyItem(final Matcher<T> elementMatcher) {54 return CoreMatchers.everyItem(elementMatcher);55 }56 /**57 * @return a matcher matching any string that contains substring58 * @deprecated Please use {@link CoreMatchers#containsString(String)} instead.59 */60 @Deprecated...
Source:AssertThatTest.java
...11import static org.hamcrest.CoreMatchers.either;12import static org.hamcrest.CoreMatchers.endsWith;13import static org.hamcrest.CoreMatchers.equalTo;14import static org.hamcrest.CoreMatchers.everyItem;15import static org.hamcrest.CoreMatchers.hasItem;16import static org.hamcrest.CoreMatchers.hasItems;17import static org.hamcrest.CoreMatchers.is;18import static org.hamcrest.CoreMatchers.isA;19import static org.hamcrest.CoreMatchers.not;20import static org.hamcrest.CoreMatchers.nullValue;21import static org.hamcrest.CoreMatchers.sameInstance;22import static org.hamcrest.CoreMatchers.startsWith;23import static org.hamcrest.CoreMatchers.theInstance;24import static org.junit.Assert.assertThat;25/**26 * Exploring assertThat assertion using Harmcrest matchers27 */28public class AssertThatTest {29 @Test30 public void testAssertThat() {31 String string = "String";32 assertThat(string, is("String")); // This assertions does the same33 assertThat(string, equalTo("String"));34 assertThat(string, is(equalTo("String")));35 }36 @Test37 public void testAssertThatChain() {38 String string = "String";39 assertThat(string, allOf(containsString("ing"), startsWith("S"), endsWith("g")));40 assertThat(string, anyOf(containsString("tr"), startsWith("A"), endsWith("g")));41 assertThat(string, not(allOf(containsString("tr"), startsWith("A"), endsWith("g"))));42 assertThat(string, both(startsWith("S")).and(endsWith("g")));43 assertThat(string, both(startsWith("S")).and(endsWith("s")).or(endsWith("g")));44 assertThat(string, either(startsWith("X")).or(startsWith("S")).or(endsWith("g")));45 }46 @Test47 public void testVerboseHamcrestMatcher() {48 String string = "S";49 // This assertion if fails return a better description as:50 // java.lang.AssertionError: Expected: a String that start with "S" but: was "Foo"51 assertThat(string, describedAs("a String that start with %0", startsWith("S"), "S"));52 }53 @Test54 public void testSimpleHamcrestMatcher() {55 // Creates a matcher that always matches, regardless of the examined object.56 assertThat(null, anything());57 assertThat(null, nullValue());58 assertThat(null, is(nullValue()));59 Object actual = new Object();60 Object expected = actual;61 assertThat(actual, isA(Object.class));62 assertThat(actual, sameInstance(expected));63 assertThat(actual, theInstance(expected));64 assertThat(actual, not(sameInstance(new Object())));65 }66 @Test67 public void testArrayHamcrestMatcher() {68 List<String> strings = Arrays.asList("String", "Strong", "Street");69 assertThat(strings, everyItem(isA(String.class)));70 assertThat(strings, everyItem(startsWith("S")));71 assertThat(strings, hasItem("Strong"));72 assertThat(strings, hasItem(is("Street")));73 assertThat(strings, hasItems("Street", "String"));74 assertThat(strings, hasItems(endsWith("g"), endsWith("t")));75 }76}...
Source:MultivaluedMapContextTests.java
...17 * under the License.18 *******************************************************************************/19package org.apache.ofbiz.base.collections;20import static org.hamcrest.CoreMatchers.both;21import static org.hamcrest.CoreMatchers.hasItem;22import static org.hamcrest.CoreMatchers.hasItems;23import static org.hamcrest.CoreMatchers.is;24import static org.hamcrest.CoreMatchers.not;25import static org.hamcrest.CoreMatchers.nullValue;26import static org.junit.Assert.assertThat;27import org.apache.ofbiz.base.util.collections.MultivaluedMapContext;28import org.junit.Before;29import org.junit.Test;30public class MultivaluedMapContextTests {31 private MultivaluedMapContext<String, Integer> m;32 @Before33 public void setUp() throws Exception {34 m = new MultivaluedMapContext<>();35 }36 @Test37 public void getEmpty() {38 assertThat(m.get("foo"), is(nullValue()));39 }40 @Test41 public void putSingleBasic() {42 m.putSingle("foo", 0);43 assertThat(m.get("foo"), hasItem(0));44 m.putSingle("foo", 1);45 assertThat(m.get("foo"), both(hasItem(1)).and(not(hasItem(0))));46 }47 @Test48 public void addBasic() {49 m.add("foo", 0);50 assertThat(m.get("foo"), hasItem(0));51 m.add("foo", 1);52 assertThat(m.get("foo"), hasItems(0, 1));53 }54 @Test55 public void addWithPreviousContext() {56 m.add("foo", 0);57 m.push();58 assertThat(m.get("foo"), hasItem(0));59 m.add("foo", 1);60 assertThat(m.get("foo"), hasItems(0, 1));61 }62 @Test63 public void getFirstBasic() {64 m.add("foo", 0);65 m.add("foo", 1);66 assertThat(m.getFirst("foo"), is(0));67 }68 @Test69 public void getFirstEmpty() {70 assertThat(m.getFirst("foo"), is(nullValue()));71 }72}...
Source:SearchHotelsParameterizedTest.java
1package com.luxoft.test;23import static org.hamcrest.CoreMatchers.hasItem;4import static org.hamcrest.CoreMatchers.allOf;5import static org.hamcrest.CoreMatchers.is;6import static org.hamcrest.MatcherAssert.assertThat;7import static org.hamcrest.CoreMatchers.hasItems;8import static org.hamcrest.CoreMatchers.equalTo;9import static org.hamcrest.CoreMatchers.nullValue;1011import java.util.ArrayList;12import java.util.Arrays;13import java.util.Collection;1415import org.hamcrest.Matcher;16import org.hamcrest.core.IsNull;17import org.junit.BeforeClass;18import org.junit.Test;19import org.junit.runner.RunWith;20import org.junit.runners.Parameterized;21import org.junit.runners.Parameterized.Parameters;2223import com.luxoft.Hotel;24import com.luxoft.SearchService;25import com.luxoft.SearchService;2627@RunWith(Parameterized.class)28public class SearchHotelsParameterizedTest {2930 private static ArrayList<Hotel> hotels;3132 private static SearchService searchService;3334 private String searchQuery;3536 @SuppressWarnings("unchecked")37 private Matcher expectedHotels;3839 @SuppressWarnings("unchecked")40 public SearchHotelsParameterizedTest(String searchQuery, Matcher expectedHotels) {41 this.searchQuery = searchQuery;42 this.expectedHotels = expectedHotels;43 }4445 @Parameters46 public static Collection<Object[]> data() {47 48 ArrayList<Hotel> hotel = new ArrayList<Hotel>();4950 Object[][] data = new Object[][] { 51 { "Hilton Hotel", hasItem(new Hotel("Hilton Hotel")) },52 { "hilton hotel", hasItem(is(new Hotel("Hilton Hotel"))) }, 53 { "Hilton", hasItem(new Hotel("Hilton Hotel")) },54 { "Hotel", allOf(hasItem(new Hotel("Hilton Hotel")), hasItem(new Hotel("Radisson Hotel")))},55 { "Premier Palace", nullValue()},56 };57 return Arrays.asList(data);58 }5960 @BeforeClass61 public static void setup() {62 searchService = new SearchService();6364 hotels = new ArrayList<Hotel>();65 hotels.add(new Hotel("Hilton Hotel"));66 hotels.add(new Hotel("Radisson Hotel"));67 hotels.add(new Hotel("Hayatt"));68 hotels.add(new Hotel("Holiday Inn"));
...
Source:AssertionsShowTest.java
...6import static org.hamcrest.CoreMatchers.both;7import static org.hamcrest.CoreMatchers.containsString;8import static org.hamcrest.CoreMatchers.either;9import static org.hamcrest.CoreMatchers.everyItem;10import static org.hamcrest.CoreMatchers.hasItem;11import static org.hamcrest.CoreMatchers.hasItems;12import static org.hamcrest.CoreMatchers.not;13import org.hamcrest.CustomMatcher;14import org.hamcrest.Matcher;15import static org.junit.Assert.assertThat;16import org.junit.Before;17import org.junit.Test;18/**19 *20 * @author airhacks.com21 */22public class AssertionsShowTest {23 private List<String> stringList;24 @Before25 public void init() {26 this.stringList = Arrays.asList("java", "javaee", "joker");27 }28 @Test29 public void lists() {30 assertThat(stringList, hasItem("java"));31 assertThat(stringList, hasItem("javaee"));32 assertThat(stringList, hasItems("javaee", "joker"));33 assertThat(stringList, everyItem(containsString("j")));34 }35 @Test36 public void combinableMathers() {37 assertThat(stringList, both(hasItem("java")).and(hasItem("javaee")));38 assertThat(stringList, either(hasItem("java")).or(hasItem("javascript")));39 assertThat(stringList, anyOf(hasItem("javascript"), hasItem("javaee")));40 assertThat(stringList, allOf(hasItem("java"), not(hasItem("erlang"))));41 }42 @Test43 public void customMatcher() {44 Matcher<String> containsJ = new CustomMatcher<String>("contains j") {45 @Override46 public boolean matches(Object item) {47 if (!(item instanceof String)) {48 return false;49 }50 String content = (String) item;51 return content.contains("j");52 }53 };54 assertThat("java", containsJ);...
Source:SampleTest.java
...3import static org.hamcrest.CoreMatchers.both;4import static org.hamcrest.CoreMatchers.containsString;5import static org.hamcrest.CoreMatchers.either;6import static org.hamcrest.CoreMatchers.everyItem;7import static org.hamcrest.CoreMatchers.hasItem;8import static org.hamcrest.CoreMatchers.hasItems;9import static org.hamcrest.CoreMatchers.is;10import static org.junit.Assert.assertThat;1112import java.util.Arrays;13import java.util.List;1415import org.hamcrest.CustomMatcher;16import org.hamcrest.Matcher;17import org.junit.Ignore;18import org.junit.Rule;19import org.junit.Test;20import org.junit.rules.Timeout;2122public class SampleTest {2324 @Rule25 public SystemOutRule rule = new SystemOutRule();2627 @Rule28 public Timeout timeout = Timeout.seconds(1);2930 @Test31 public void testLists() {32 List<String> stringList = Arrays.asList("java", "pythona", "dukea");33 assertThat(stringList, hasItem("java"));34 assertThat(stringList, hasItems("java", "pythona"));35 assertThat(stringList, everyItem(containsString("a")));36 assertThat(stringList, both(hasItem("java")).and(hasItem("dukea")));37 assertThat(stringList, either(hasItem("java1")).or(hasItem("dukea")));38 }3940 @Test41 public void testWithHamcrest() {42 String result = "HelloWorld!";43 assertThat(result, is("HelloWorld!"));44 }4546 @Test(timeout = 20000)47 @Ignore48 public void tooSlow() throws InterruptedException {49 Thread.sleep(1000);50 }51
...
Source:JUnitTest.java
...3import static org.hamcrest.CoreMatchers.not;4import static org.hamcrest.CoreMatchers.nullValue;5import static org.junit.Assert.assertThat;6import static org.junit.Assert.assertTrue;7//import static org.junit.matchers.JUnitMatchers.hasItem;8import static org.hamcrest.CoreMatchers.either;9import static org.hamcrest.CoreMatchers.hasItem;10import java.util.HashSet;11import java.util.Set;12import org.junit.Test;13import org.junit.runner.RunWith;14import org.springframework.beans.factory.annotation.Autowired;15import org.springframework.context.ApplicationContext;16import org.springframework.test.context.ContextConfiguration;17import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;18@RunWith(SpringJUnit4ClassRunner.class)19@ContextConfiguration("/junit.xml")20public class JUnitTest {21 @Autowired ApplicationContext context;22 23 static Set<JUnitTest> testObjects = new HashSet<JUnitTest>();24 static ApplicationContext contextObject = null;25 26 @Test public void test1(){27 assertThat(testObjects, not(hasItem(this)));28 testObjects.add(this);29 assertThat(contextObject == null || contextObject == this.context, is(true));30 contextObject = this.context;31 }32 @Test public void test2(){33 assertThat(testObjects, not(hasItem(this)));34 testObjects.add(this);35 assertTrue(contextObject == null || contextObject == this.context);36 contextObject = this.context;37 }38 39 @Test public void test3(){40 assertThat(testObjects, not(hasItem(this)));41 testObjects.add(this);42 43 44 assertThat(45 contextObject,46 either(is(nullValue(ApplicationContext.class))).or(47 is(this.context)));48 49 contextObject = this.context;50 }51}...
hasItem
Using AI Code Generation
1@Grab(group='org.hamcrest', module='hamcrest-all', version='1.3')2import org.hamcrest.CoreMatchers.*3assertThat([1,2,3], hasItem(2))4assertThat([1,2,3], hasItem(4))5import org.hamcrest.collection.IsCollectionWithSize.*6assertThat([1,2,3], hasItem(2))7assertThat([1,2,3], hasItem(4))8import org.hamcrest.collection.IsCollectionWithSize.*9assertThat([1,2,3], hasItem(2))10assertThat([1,2,3], hasItem(4))11import org.hamcrest.collection.IsCollectionWithSize.*12assertThat([1,2,3], hasItem(2))13assertThat([1,2,3], hasItem(4))14import org.hamcrest.collection.IsCollectionWithSize.*15assertThat([1,2,3], hasItem(2))16assertThat([1,2,3], hasItem(4))17import org.hamcrest.collection.IsCollectionWithSize.*18assertThat([1,2,3], hasItem(2))19assertThat([1,2,3], hasItem(4))20import org.hamcrest.collection.IsCollectionWithSize.*21assertThat([1,2,3], hasItem(2))22assertThat([1,2,3], hasItem(4))23import org.hamcrest.collection.IsCollectionWithSize.*24assertThat([1,2,3], hasItem(2))25assertThat([1,2,3], hasItem(4))26import org.hamcrest.collection.IsCollectionWithSize.*27assertThat([1,2,3], hasItem(2))28assertThat([1,2,3], hasItem(4))
hasItem
Using AI Code Generation
1import static org.hamcrest.CoreMatchers.hasItem;2import static org.hamcrest.MatcherAssert.assertThat;3import java.util.ArrayList;4import java.util.List;5import org.junit.Test;6public class HamcrestMatcherTest {7 public void testHasItem() {8 List<String> list = new ArrayList<>();9 list.add("A");10 list.add("B");11 list.add("C");12 assertThat(list, hasItem("B"));13 }14}15If you want to use the hasItems method of org.hamcrest.CoreMatchers class, you have to import static org.hamcrest.CoreMatchers.hasItems;16import static org.hamcrest.CoreMatchers.hasItems;17import java.util.ArrayList;18import java.util.List;19import org.junit.Test;20public class HamcrestMatcherTest {21 public void testHasItems() {22 List<String> list = new ArrayList<>();23 list.add("A");24 list.add("B");25 list.add("C");26 assertThat(list, hasItems("B", "C"));27 }28}29import static org.hamcrest.CoreMatchers.is;30import static org.hamcrest.MatcherAssert.assertThat;31import org.junit.Test;32public class HamcrestMatcherTest {33 public void testIs() {34 String str1 = "A";35 String str2 = "A";36 assertThat(str1, is(str2));37 }38}39import
hasItem
Using AI Code Generation
1assertThat("Hello", hasItem("H"))2assertThat("Hello", hasItem("o"))3assertThat("Hello", hasItem("l"))4assertThat("Hello", hasItem("e"))5assertThat("Hello", hasItem("H"))6assertThat("Hello", hasItem("H"))7assertThat("Hello", hasItems("H", "e", "l", "o"))8assertThat("Hello", hasItems("H", "H", "e", "l", "o"))9assertThat("Hello", hasItems("H", "H", "e", "l", "o", "H"))10assertThat("Hello", contains("H", "e", "l", "l", "o"))11assertThat("Hello", contains("H", "H", "e", "l", "l", "o"))12assertThat("Hello", containsInAnyOrder("H", "e", "l", "l", "o"))13assertThat("Hello", containsInAnyOrder("H", "H", "e", "l", "l", "o"))14assertThat("Hello", containsInRelativeOrder("H", "e", "l", "l", "o"))15assertThat("Hello", containsInRelativeOrder("H", "H", "e", "l", "l", "o"))16assertThat("Hello", startsWith("H"))17assertThat("Hello", startsWith("H"))18assertThat("Hello", startsWith("H"))19assertThat("Hello", startsWith("H"))20assertThat("Hello", startsWith("H"))21assertThat("Hello", startsWith("H"))22assertThat("Hello", endsWith("o"))23assertThat("Hello", endsWith("o"))24assertThat("Hello", endsWith("o"))25assertThat("Hello", endsWith("o"))26assertThat("Hello", endsWith("o"))27assertThat("Hello", endsWith("o"))28assertThat("
hasItem
Using AI Code Generation
1import org.hamcrest.CoreMatchers.*2assertThat(1, is(1))3assertThat(1, is(not(2)))4assertThat([1,2,3], hasItem(3))5assertThat([1,2,3], not(hasItem(4)))6assertThat([1,2,3], hasItems(3, 1))7assertThat('Groovy', containsString('ovy'))8assertThat('Groovy', not(containsString('groovy')))9assertThat('Groovy', endsWith('vy'))10assertThat('Groovy', not(endsWith('groovy')))11assertThat('Groovy', startsWith('Gro'))12assertThat('Groovy', not(startsWith('groovy')))13assertThat(['a','b','c','d'], containsInAnyOrder('b','d','c','a'))14assertThat(['a','b','c','d'], not(containsInAnyOrder('b','d','c','a','e')))15assertThat(['a','b','c','d'], containsInRelativeOrder('a','b','c','d'))16assertThat(['a','b','c','d'], not(containsInRelativeOrder('a','b','c','d','e')))17assertThat(['a','b','c','d'], contains('a','b','c','d'))18assertThat(['a','b','c','d'], not(contains('a','b','c','d','e')))19assertThat(['a','b','c','d'], containsInRelativeOrder('a','b','c','d'))20assertThat(['a','b','c','d'], not(containsInRelativeOrder('a','b','c','d','e')))21assertThat(['a','b','c','d'], everyItem(startsWith('a')))22assertThat(['a','b','c','d
hasItem
Using AI Code Generation
1assertThat(books, hasItem("Java 8 in Action"));2assertThat(books, hasItems("Java 8 in Action", "Java 8 Lambdas"));3assertThat(books, hasItems("Java 8 in Action", "Java 8 Lambdas"));4assertThat(books, hasItems("Java 8 in Action", "Java 8 Lambdas"));5assertThat(books, hasItem("Java 8 in Action"));6assertThat(books, hasItems("Java 8 in Action", "Java 8 Lambdas"));7assertThat(books, hasItems("Java 8 in Action", "Java 8 Lambdas"));8assertThat(books, hasItems("Java 8 in Action", "Java 8 Lambdas"));9assertThat(books, hasItem("Java 8 in Action"));10assertThat(books, hasItems("Java 8 in Action", "Java 8 Lambdas"));11assertThat(books, hasItems("Java 8 in Action", "Java 8 Lambdas"));12assertThat(books, hasItems("Java 8 in Action", "Java 8 Lambdas"));13assertThat(books, hasItem("Java 8 in Action"));14assertThat(books, hasItems("Java 8 in Action", "Java 8 Lambdas"));15assertThat(books, hasItems("Java 8 in Action", "Java 8 Lambdas"));16assertThat(books,
hasItem
Using AI Code Generation
1import org.hamcrest.CoreMatchers.hasItem2import org.junit.Assert.assertThat3assertThat list, hasItem(2)4import org.hamcrest.Matchers.contains5import org.junit.Assert.assertThat6assertThat list, contains(1, 2, 3)7import org.hamcrest.Matchers.containsInAnyOrder8import org.junit.Assert.assertThat9assertThat list, containsInAnyOrder(3, 2, 1)10import org.hamcrest.Matchers.containsInRelativeOrder11import org.junit.Assert.assertThat12assertThat list, containsInRelativeOrder(1, 2, 3)13import org.hamcrest.Matchers.containsInRelativeOrder14import org.junit.Assert.assertThat15assertThat list, containsInRelativeOrder(1, 3, 2)16import org.hamcrest.Matchers.containsInRelativeOrder17import org.junit.Assert.assertThat18assertThat list, containsInRelativeOrder(2, 1, 3)19import org.hamcrest.Matchers.containsInRelativeOrder20import org.junit.Assert.assertThat21assertThat list, containsInRelativeOrder(2, 3, 1)22import org.hamcrest.Matchers.containsInRelativeOrder23import org.junit.Assert.assertThat24assertThat list, containsInRelativeOrder(3, 1, 2)
hasItem
Using AI Code Generation
1import static org.hamcrest.CoreMatchers.*;2import static org.hamcrest.MatcherAssert.*;3public class HamcrestTest {4 public static void main(String[] args) {5 assertThat(Arrays.asList("one", "two", "three"), hasItem("two"));6 assertThat(Arrays.asList("one", "two", "three"), hasItems("two", "three"));7 }8}9import static org.hamcrest.CoreMatchers.hasItem;10import static org.hamcrest.CoreMatchers.hasItems;11import static org.hamcrest.MatcherAssert.assertThat;12public class HamcrestTest {13 public static void main(String[] args) {14 assertThat(Arrays.asList("one", "two", "three"), hasItem("two"));15 assertThat(Arrays.asList("one", "two", "three"), hasItems("two", "three"));16 }17}18import static org.hamcrest.CoreMatchers.hasItem;19import static org.hamcrest.CoreMatchers.hasItems;20import static org.hamcrest.CoreMatchers.is;21import static org.hamcrest.MatcherAssert.assertThat;22public class HamcrestTest {23 public static void main(String[] args) {24 assertThat(Arrays.asList("one", "two", "three"), hasItem(is("two")));25 assertThat(Arrays.asList("one", "two", "three"), hasItems(is("two"), is("three")));26 }27}28import static org.hamcrest.CoreMatchers.*;29import static org.hamcrest.MatcherAssert.assertThat;30public class HamcrestTest {31 public static void main(String[] args) {32 assertThat(Arrays.asList("one", "two", "three"), hasItem(is("two")));33 assertThat(Arrays.asList("one", "two", "three"), hasItems(is("two"), is("three")));34 }35}
LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed JUnit testing chapters to help you get started:
You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.
Get 100 minutes of automation test minutes FREE!!