Best junit code snippet using org.hamcrest.CoreMatchers.endsWith
Source:HamcrestCoreMatchersTest.java
...6import static org.hamcrest.CoreMatchers.both;7import static org.hamcrest.CoreMatchers.containsString;8import static org.hamcrest.CoreMatchers.containsStringIgnoringCase;9import static org.hamcrest.CoreMatchers.either;10import static org.hamcrest.CoreMatchers.endsWith;11import static org.hamcrest.CoreMatchers.endsWithIgnoringCase;12import static org.hamcrest.CoreMatchers.equalTo;13import static org.hamcrest.CoreMatchers.equalToObject;14import static org.hamcrest.CoreMatchers.everyItem;15import static org.hamcrest.CoreMatchers.hasItem;16import static org.hamcrest.CoreMatchers.hasItems;17import static org.hamcrest.CoreMatchers.instanceOf;18import static org.hamcrest.CoreMatchers.is;19import static org.hamcrest.CoreMatchers.isA;20import static org.hamcrest.CoreMatchers.not;21import static org.hamcrest.CoreMatchers.notNullValue;22import static org.hamcrest.CoreMatchers.nullValue;23import static org.hamcrest.CoreMatchers.sameInstance;24import static org.hamcrest.CoreMatchers.startsWith;25import static org.hamcrest.CoreMatchers.startsWithIgnoringCase;26import static org.hamcrest.CoreMatchers.theInstance;27import static org.hamcrest.MatcherAssert.assertThat;28import java.util.List;29import org.junit.jupiter.api.Test;30import com.google.common.collect.Lists;3132public class HamcrestCoreMatchersTest {3334 @Test35 public void givenTestInput_WhenUsingIsForMatch() {3637 // GIVEN38 String testString = "hamcrest core";3940 // ASSERT41 assertThat(testString, is("hamcrest core"));42 assertThat(testString, is(equalTo("hamcrest core")));43 }4445 @Test46 public void givenDifferentStaticTypeTestInput_WhenUsingEqualToObject_ThenCorrect() {4748 // GIVEN49 Object original = 100;5051 // ASSERT52 assertThat(original, equalToObject(100));53 }5455 @Test56 public void givenTestInput_WhenUsingInstanceOfForClassTypeCheck() {5758 assertThat("hamcrest", is(instanceOf(String.class)));59 }6061 @Test62 public void givenTestInput_WhenUsingIsA_ThenAssertType() {6364 assertThat("hamcrest core", isA(String.class));65 }6667 @Test68 public void givenTestInput_WhenUsingEqualToMatcherForEquality() {6970 // GIVEN71 String actualString = "Hamcrest Core";72 List<String> actualList = Lists.newArrayList("hamcrest", "core");7374 // ASSERT75 assertThat(actualString, is(equalTo("Hamcrest Core")));76 assertThat(actualList, is(equalTo(Lists.newArrayList("hamcrest", "core"))));77 }7879 @Test80 public void givenTestInput_WhenUsingNotForMatch() {8182 // GIVEN83 String testString = "hamcrest";8485 // ASSERT86 assertThat(testString, not("hamcrest core"));87 assertThat(testString, is(not(equalTo("hamcrest core"))));88 assertThat(testString, is(not(instanceOf(Integer.class))));89 }9091 @Test92 public void givenTestInput_WhenUsingNullValueForNullCheck() {9394 // GIVEN95 Integer nullObject = null;9697 // ASSERT98 assertThat(nullObject, is(nullValue()));99 assertThat(nullObject, is(nullValue(Integer.class)));100 }101102 @Test103 public void givenTestInput_WhenUsingNotNullValueForNotNullCheck() {104105 // GIVEN106 Integer testNumber = 123;107108 // ASSERT109 assertThat(testNumber, is(notNullValue()));110 assertThat(testNumber, is(notNullValue(Integer.class)));111 }112113 @Test114 public void givenString_WhenStartsWith_ThenCorrect() {115116 // GIVEN117 String testString = "hamcrest core";118119 // ASSERT120 assertThat(testString, startsWith("hamcrest"));121 }122123 @Test124 public void giveString_WhenStartsWithIgnoringCase_ThenCorrect() {125126 // GIVEN127 String testString = "hamcrest core";128129 // ASSERT130 assertThat(testString, startsWithIgnoringCase("HAMCREST"));131 }132133 @Test134 public void givenString_WhenEndsWith_ThenCorrect() {135136 // GIVEN137 String testString = "hamcrest core";138139 // ASSERT140 assertThat(testString, endsWith("core"));141 }142143 @Test144 public void givenString_WhenEndsWithIgnoringCase_ThenCorrect() {145146 // GIVEN147 String testString = "hamcrest core";148149 // ASSERT150 assertThat(testString, endsWithIgnoringCase("CORE"));151 }152153 @Test154 public void givenString_WhenContainsString_ThenCorrect() {155156 // GIVEN157 String testString = "hamcrest core";158159 // ASSERT160 assertThat(testString, containsString("co"));161 }162163 @Test164 public void givenString_WhenContainsStringIgnoringCase_ThenCorrect() {165166167 // GIVEN168 String testString = "hamcrest core";169170 // ASSERT171 assertThat(testString, containsStringIgnoringCase("CO"));172 }173174 @Test175 public void givenTestInput_WhenUsingHasItemInCollection() {176177 // GIVEN178 List<String> list = Lists.newArrayList("java", "spring", "baeldung");179180 // ASSERT181 assertThat(list, hasItem("java"));182 assertThat(list, hasItem(isA(String.class)));183 }184185186 @Test187 public void givenTestInput_WhenUsingHasItemsInCollection() {188189 // GIVEN190 List<String> list = Lists.newArrayList("java", "spring", "baeldung");191192 // ASSERT193 assertThat(list, hasItems("java", "baeldung"));194 assertThat(list, hasItems(isA(String.class), endsWith("ing")));195 }196197 @Test198 public void givenTestInput_WhenUsingAnyForClassType() {199200 assertThat("hamcrest", is(any(String.class)));201 assertThat("hamcrest", is(any(Object.class)));202 }203204 @Test205 public void givenTestInput_WhenUsingAllOfForAllMatchers() {206207 // GIVEN208 String testString = "Hamcrest Core";209210 // ASSERT211 assertThat(testString, allOf(startsWith("Ham"), endsWith("ore"), containsString("Core")));212 }213214 @Test215 public void givenTestInput_WhenUsingAnyOfForAnyMatcher() {216217 // GIVEN218 String testString = "Hamcrest Core";219220 // ASSERT221 assertThat(testString, anyOf(startsWith("Ham"), containsString("baeldung")));222 }223224 @Test225 public void givenTestInput_WhenUsingBothForMatcher() {
...
Source:CoreMatchers.java
...4import static org.hamcrest.CoreMatchers.anything;5import static org.hamcrest.CoreMatchers.both;6import static org.hamcrest.CoreMatchers.containsString;7import static org.hamcrest.CoreMatchers.describedAs;8import static org.hamcrest.CoreMatchers.endsWith;9import static org.hamcrest.CoreMatchers.equalTo;10import static org.hamcrest.CoreMatchers.everyItem;11import static org.hamcrest.CoreMatchers.hasItems;12import static org.hamcrest.CoreMatchers.instanceOf;13import static org.hamcrest.CoreMatchers.is;14import static org.hamcrest.CoreMatchers.isA;15import static org.hamcrest.CoreMatchers.notNullValue;16import static org.hamcrest.CoreMatchers.nullValue;17import static org.hamcrest.CoreMatchers.startsWith;18import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo;19import static org.junit.Assert.assertThat;20import java.math.BigDecimal;21import java.util.ArrayList;22import java.util.Calendar;23import java.util.HashMap;24import java.util.HashSet;25import java.util.List;26import java.util.Map;27import java.util.Set;28import org.hamcrest.core.IsSame;29import org.junit.Test;30import com.google.common.collect.Lists;31/**32 * This java example will demonstrate hamcrest33 * core matchers.34 * 35 * @author Justin Musgrove36 * @see <a href='http://www.leveluplunch.com/java/examples/hamcrest-core-matchers-junit-testing/'>Core matchers</a>37 * 38 */39public class CoreMatchers {40 41 @Test42 public void hamcrest_core_allof () {43 String microBrew = "Lake Louie Brewery Company";44 45 assertThat(microBrew, allOf(startsWith("Lake"), containsString("Brew")));46 }47 48 @Test49 public void hamcrest_core_anyOf () {50 51 String microBrew = "Grumpy Troll Brewery";52 53 assertThat(microBrew, anyOf(startsWith("brew"), containsString("Troll")));54 }55 @Test56 public void hamcrest_core_combinableMatcher () {57 58 String isLite = "Miller Lite";59 60 assertThat(isLite, both(containsString("Miller")).and(containsString("Lite")));61 }62 @Test63 public void hamcrest_core_describedAs () {64 65 BigDecimal myBigDecimal = new BigDecimal("0");66 67 assertThat(myBigDecimal, 68 describedAs("a big decimal equal to %0", 69 equalTo(myBigDecimal), 70 myBigDecimal.toPlainString()));71 }72 73 @Test74 public void hamcrest_core_every () {75 76 List<Integer> ages = Lists.newArrayList(21, 25, 30, 18);77 assertThat(ages, everyItem(greaterThanOrEqualTo(18)));78 }79 80 81 @Test82 public void hamcrest_core_is() {83 84 String isLite = "Miller Brewing Company";85 86 assertThat("Miller Brewing Company", is(equalTo(isLite)));87 }88 89 @Test90 public void hamcrest_core_isA() {91 92 Map<Integer, String> map = new HashMap<Integer, String>();93 94 assertThat(map, isA(Map.class));95 }96 97 98 @Test99 public void hamcrest_core_anything () {100 101 assertThat("", anything());102 }103 104 @Test105 @SuppressWarnings("unchecked")106 public void hamcrest_core_hasItems_matchers () {107 108 List<String> regionalBreweries = Lists.newArrayList(109 "Capital Brewery", 110 "City Brewing Company ", 111 "Jacob Leinenkugel Brewing Company",112 "Lakefront Brewery", 113 "New Glarus Brewing Company", 114 "Stevens Point Brewery"); 115 116 assertThat(regionalBreweries, hasItems(117 containsString("Brew"), 118 endsWith("y")));119 }120 121 @Test122 public void hamcrest_core_hasItems () {123 124 List<String> regionalBreweries = Lists.newArrayList(125 "Capital Brewery", 126 "City Brewing Company ", 127 "Jacob Leinenkugel Brewing Company",128 "Lakefront Brewery, Inc.", 129 "New Glarus Brewing Company", 130 "Stevens Point Brewery"); 131 132 assertThat(regionalBreweries, hasItems(133 "Capital Brewery", 134 "City Brewing Company ", 135 "Jacob Leinenkugel Brewing Company",136 "Lakefront Brewery, Inc.", 137 "New Glarus Brewing Company", 138 "Stevens Point Brewery"));139 }140 141 @Test142 public void hamcrest_core_isEqual () {143 144 String spottedCreator = "New Glarus Brewing Company";145 146 assertThat(spottedCreator, equalTo("New Glarus Brewing Company"));147 }148 149 150 @Test151 public void hamcrest_core_isInstanceOf () {152 Calendar cal = Calendar.getInstance();153 154 assertThat(cal, instanceOf(Calendar.class));155 }156 157 @Test158 public void hamcrest_core_is_notNullValue () {159 Set<String> daNull = new HashSet<String>();160 161 assertThat(daNull, is(notNullValue()));162 }163 164 165 @Test166 public void hamcrest_core_is_nullValue () {167 168 Set<String> daNull = null;169 170 assertThat(daNull, is(nullValue()));171 }172 173 @Test174 public void hamcrest_core_isSame_string () {175 176 String wiBrewery = "Capital Brewery";177 String wiRegionalBrewery = "Capital Brewery";178 179 assertThat(wiRegionalBrewery, IsSame.<String>sameInstance(wiBrewery));180 }181 182 @Test183 public void hamcrest_core_is_same_list () {184 185 List<String> someList = new ArrayList<String>();186 187 assertThat(someList, IsSame.<List<String>>sameInstance(someList));188 }189 @Test190 public void hamcrest_core_containsString () {191 192 String brewery = "Pabst Brewing Company";193 194 assertThat(brewery, containsString("Brew"));195 }196 197 198 @Test199 public void hamcrest_core_string_startsWith () {200 String highSchool = "Tarpon spring spongers";201 202 assertThat(highSchool, startsWith("Tarpon"));203 }204 205 @Test206 public void hamcrest_core_string_endsWith () {207 208 String baseBallTeam = "Milwaukee brewers";209 210 assertThat(baseBallTeam, endsWith("brewers"));211 }212 213 214}...
Source:AssertThatTest.java
...8import static org.hamcrest.CoreMatchers.both;9import static org.hamcrest.CoreMatchers.containsString;10import static org.hamcrest.CoreMatchers.describedAs;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:AssertThatDemoTest.java
...7import static org.hamcrest.CoreMatchers.anything;8import static org.hamcrest.CoreMatchers.both;9import static org.hamcrest.CoreMatchers.containsString;10import static org.hamcrest.CoreMatchers.either;11import static org.hamcrest.CoreMatchers.endsWith;12import static org.hamcrest.CoreMatchers.equalTo;13import static org.hamcrest.CoreMatchers.everyItem;14import static org.hamcrest.CoreMatchers.hasItem;15import static org.hamcrest.CoreMatchers.instanceOf;16import static org.hamcrest.CoreMatchers.is;17import static org.hamcrest.CoreMatchers.isA;18import static org.hamcrest.CoreMatchers.not;19import static org.hamcrest.CoreMatchers.sameInstance;20import static org.hamcrest.CoreMatchers.startsWith;21import static org.junit.Assert.assertThat;22public class AssertThatDemoTest {23 @Test24 public void testAssertThat() {25 // assertThatçåºæ¬åçæ¯ï¼ç¨æå®çå¹é
å¨å¹é
æå®çå®é
å¼ãå¦æä¸å¹é
ï¼åçæé误信æ¯ï¼å¹¶æåºAssertionErrorå¼å¸¸ã26 // public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {27 // if (!matcher.matches(actual)) {28 // Description description = new StringDescription();29 // description.appendText(reason)30 // .appendText("\nExpected: ")31 // .appendDescriptionOf(matcher)32 // .appendText("\n but: ");33 // matcher.describeMismatch(actual, description);34 //35 // throw new AssertionError(description.toString());36 // }37 // }38 assertThat("Hello, World!", is("Hello, World!"));39 assertThat("Hello, World!", equalTo("Hello, World!"));40 assertThat("Hello, World!", startsWith("Hello"));41 assertThat("Hello, World!", endsWith("World!"));42 assertThat("Hello, World!", containsString("llo, Wor"));43 assertThat(6, not(8));44 assertThat("Hello, World!", anyOf(startsWith("Hello"), endsWith("Welcome")));45 assertThat("Hello, World!", either(startsWith("Hello")).or(endsWith("Welcome")));46 assertThat("Hello, World!", allOf(startsWith("Hello"), endsWith("World!")));47 assertThat("Hello, World!", both(startsWith("Hello")).and(endsWith("World!")));48 assertThat("Hello, World!", isA(String.class));49 assertThat("Hello, World!", anything());50 assertThat("Hello, World!", instanceOf(String.class));51 Object o = new Object();52 assertThat(o, sameInstance(o));53 assertThat(asList(1, 2, 3), hasItem(3));54 assertThat(asList(1, 2, 3), everyItem(not(8)));55 // èªå®ä¹MatcheråèassertThat("Is fails", "Hello, World!", is("Hi, World!"))çå®ä¹å失败æ¥åã56 // å¯ä»¥å¨ä¸ä¸ªç±»æ¯org.hamcrest.CoreMatchersçå·¥å
·ç±»ä¸æä¾éææ¹æ³ï¼è¯¥æ¹æ³è¿åèªå®ä¹Matcherã57 assertThat(Set.of(1, 2, 3), new HasElement<>(2));58 }59}...
Source:combinational_collection_assert.java
1package Sample;2import static org.hamcrest.CoreMatchers.allOf;3import static org.hamcrest.CoreMatchers.anything;4import static org.hamcrest.CoreMatchers.endsWith;5import static org.hamcrest.CoreMatchers.hasItem;6import static org.hamcrest.CoreMatchers.is;7import static org.hamcrest.CoreMatchers.not;8import static org.hamcrest.CoreMatchers.notNullValue;9import static org.hamcrest.CoreMatchers.nullValue;10import static org.hamcrest.CoreMatchers.startsWith;11import static org.junit.Assert.*;12import java.util.Arrays;13import java.util.Collection;14import java.util.HashSet;15import java.util.Set;16import org.junit.Test;17public class combinational_collection_assert {18 @Test19 public void a_StringMatchers() { 20 String tested="TEKSYSTEMS";21 String check ="EKS";22 assertThat("MATCHING",tested, anything(check));23 }24 @Test25 public void b_NullMatchers() { 26 String tested=null;27 assertThat(" Null MATCHING",tested,nullValue());28 }29 @Test30 public void c_NotNullMatchers() { 31 String tested="";32 assertThat(" Null MATCHING",tested,notNullValue());33 }34 @Test35 public void d_STRINGSameMatchers() { 36 String tested="jharna";37 String check="jharna";38 assertThat(" MATCHING",tested,is(check));39 }40 @Test41 public void e_STRINGNotSameMatchers() { 42 String tested="jharna";43 String check="jharna1234";44 assertThat(" MATCHING",tested,not(check));45 }46 //--------------------------------combination assert---------------------------------47 @Test48 public void f_CombinationSTRINGNotSameMatchers() { 49 String tested="!!!!jharna@";50 assertThat(" MATCHING",tested,allOf(startsWith("!"),endsWith("@")));51 52 }53 //-------------------------------Collection Assert---------------------54 55 @Test56 public void f_CoLLECTIONSTRINGSameMatchers() { 57 String[] testArray= {"a","b","c"};58 Collection<String> tested=Arrays.asList(testArray);59 assertThat("MATCHES" ,tested, hasItem("b"));60}61 @Test62 public void g_CoLLECTIONSTRINGSameMatchers() {63 String[] testArray={"mate","rate"};64 Set<String> tested=new HashSet<String>(Arrays.asList(testArray));65 assertThat(tested,hasItem(endsWith("ate")));66 }67 68 @Test69 public void h_CoLLECTIONSTRINGSameMatchers() {70 String[] testArray={"teksystems","tekstsav"};71 Set<String> tested=new HashSet<String>(Arrays.asList(testArray));72 assertThat(tested,hasItem(startsWith("tek")));73 }74}...
Source:MatcherExample.java
1package com.onlyfullstack.unittesting;2import org.junit.Test;3import static org.hamcrest.CoreMatchers.containsString;4import static org.hamcrest.CoreMatchers.endsWith;5import static org.hamcrest.CoreMatchers.instanceOf;6import static org.hamcrest.CoreMatchers.is;7import static org.hamcrest.CoreMatchers.not;8import static org.hamcrest.CoreMatchers.notNullValue;9import static org.hamcrest.CoreMatchers.sameInstance;10import static org.hamcrest.CoreMatchers.startsWith;11import static org.junit.Assert.assertThat;12/**13 * TODO: Javadoc14 */15public final class MatcherExample {16 @Test17 public void compare2Strings_with_is_matcher() {18 String name = "Saurabh";19 String newName = "Saurabh";20 assertThat(name, is(newName));21 }22 @Test23 public void compare2Strings_notEquals_with_not_Matcher() {24 String name = "Saurabh";25 String newName = "OnlyFullstack";26 assertThat(name, not(newName));27 }28 @Test29 public void startsWith_example() {30 String name = "OnlyFullstack";31 String newName = "Only";32 assertThat(name, startsWith(newName));33 }34 @Test35 public void endsWith_example() {36 String name = "OnlyFullstack";37 String newName = "stack";38 assertThat(name, endsWith(newName));39 }40 @Test41 public void containsString_example() {42 String name = "OnlyFullstack";43 String newName = "Full";44 assertThat(name, containsString(newName));45 }46 @Test47 public void notNullValue_example() {48 String name = "OnlyFullstack";49 assertThat(name, notNullValue());50 }51 @Test52 public void sameInstance_instanceOf_example() {...
Source:HamcrestTest.java
...3import static org.hamcrest.CoreMatchers.anyOf;4import static org.hamcrest.CoreMatchers.anything;5import static org.hamcrest.CoreMatchers.containsString;6import static org.hamcrest.CoreMatchers.either;7import static org.hamcrest.CoreMatchers.endsWith;8import static org.hamcrest.CoreMatchers.hasItem;9import static org.hamcrest.CoreMatchers.hasItems;10import static org.hamcrest.CoreMatchers.is;11import static org.hamcrest.CoreMatchers.startsWith;12import java.util.Arrays;13import java.util.List;14import org.junit.Assert;15import org.junit.Test;16/**17 *18 * @author ThirupathiReddy V19 *20 */21public class HamcrestTest {22 @Test23 public void customMatcher() {24 Assert.assertThat(10, lessThanOrEqual(2));25 }26 @Test27 public void allOfTest() {28 Assert.assertThat("Some", anything());29 }30 @Test31 public void thatTest() {32 Assert.assertThat(99.9, either(is(100.0)).or(is(99.9)));33 Assert.assertThat(99.0, anyOf(is(99.0), is(2929.00)));34 }35 @Test36 public void collectionMatcherTest() {37 final List<Double> list = Arrays.asList(200.0, 222.0, 88.00, 12.59);38 Assert.assertThat(list, hasItem(200.0));39 Assert.assertThat(list, hasItems(12.59, 88.00));40 }41 @Test42 public void stringMatcherTest() {43 Assert.assertThat("Hello world", containsString("world"));44 Assert.assertThat("Hello world", endsWith("world"));45 Assert.assertThat("Hello world", startsWith("Hello"));46 }47}...
Source:RunnerHamcrestTest.java
2import static org.hamcrest.CoreMatchers.anyOf;3import static org.hamcrest.CoreMatchers.anything;4import static org.hamcrest.CoreMatchers.containsString;5import static org.hamcrest.CoreMatchers.either;6import static org.hamcrest.CoreMatchers.endsWith;7import static org.hamcrest.CoreMatchers.hasItem;8import static org.hamcrest.CoreMatchers.hasItems;9import static org.hamcrest.CoreMatchers.is;10import static org.hamcrest.CoreMatchers.startsWith;11import java.util.Arrays;12import java.util.List;13import org.junit.Assert;14import org.junit.Test;15/**16 *17 * @author ThirupathiReddy V18 *19 */20public class RunnerHamcrestTest {21 @Test22 public void allOfTest() {23 Assert.assertThat("Some", anything());24 }25 @Test26 public void thatTest() {27 Assert.assertThat(99.9, either(is(100.0)).or(is(99.9)));28 Assert.assertThat(99.0, anyOf(is(99.0), is(2929.00)));29 }30 @Test31 public void collectionMatcherTest() {32 final List<Double> list = Arrays.asList(200.0, 222.0, 88.00, 12.59);33 Assert.assertThat(list, hasItem(200.0));34 Assert.assertThat(list, hasItems(12.59, 88.00));35 }36 @Test37 public void stringMatcherTest() {38 Assert.assertThat("Hello world", containsString("world"));39 Assert.assertThat("Hello world", endsWith("world"));40 Assert.assertThat("Hello world", startsWith("Hello"));41 }42}...
endsWith
Using AI Code Generation
1assertThat("Hello World", endsWith("World"));2assertThat("Hello World", startsWith("Hello"));3assertThat("Hello World", containsString("Hello"));4assertThat("Hello World", is("Hello World"));5assertThat("Hello World", equalTo("Hello World"));6assertThat("Hello World", not("Hello World"));7assertThat("Hello World", endsWith("World"));8assertThat("Hello World", startsWith("Hello"));9assertThat("Hello World", containsString("Hello"));10assertThat("Hello World", is("Hello World"));11assertThat("Hello World", equalTo("Hello World"));12assertThat("Hello World", not("Hello World"));13assertThat("Hello World").is("Hello World");14assertThat("Hello World").isEqualTo("Hello World");15assertThat("Hello World").isNotEqualTo("Hello World");16assertThat("Hello World").isNotNull();17assertThat("Hello World").isNull();18assertThat("Hello World").isInstanceOf(String.class);
endsWith
Using AI Code Generation
1assertThat("foo", endsWith("oo"));2assertThat("foo", startsWith("fo"));3assertThat("foo", containsString("o"));4assertThat("foo", equalTo("foo"));5assertThat("foo", not("bar"));6assertThat("foo", is("foo"));7assertThat("foo", instanceOf(String.class));8assertThat(null, nullValue());9assertThat("foo", notNullValue());10assertThat("foo", equalToIgnoringCase("FOO"));11assertThat("foo", equalToIgnoringWhiteSpace("foo"));12assertThat(1.0, closeTo(1.1, 0.2));13assertThat(1, greaterThan(0));14assertThat(0, lessThan(1));15assertThat(1, greaterThanOrEqualTo(1));16assertThat(1, lessThanOrEqualTo(1));17assertThat(new String[]{"foo", "bar"}, arrayContaining("foo"));18assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder("bar", "foo"));19assertThat(Arrays.asList("foo", "bar"), hasItem("foo"));
endsWith
Using AI Code Generation
1assertThat("foo", endsWith("o"))2assertThat("foo", containsString("oo"))3assertThat("foo", startsWith("f"))4assertThat("foo", equalTo("foo"))5assertThat("foo", not("bar"))6assertThat("foo", is("foo"))7assertThat("foo", anyOf(startsWith("f"), endsWith("o")))8assertThat("foo", allOf(startsWith("f"), endsWith("o")))9assertThat("foo", both(containsString("o")).and(containsString("f")))10assertThat("foo", either(containsString("o")).or(containsString("f")))11assertThat(Arrays.asList("foo", "bar"), hasItem("foo"))12assertThat(Arrays.asList("foo", "bar"), hasItems("foo", "bar"))13assertThat(new String[]{"foo", "bar"}, hasItemInArray("foo"))14assertThat(new String[]{"foo", "bar"}, hasItemsInArray("foo", "bar"))15assertThat(new HashMap<String, String>() {{ put("foo", "bar"); }}, hasEntry("foo", "bar"))16assertThat(new HashMap<String, String>() {{ put("foo", "bar"); }}, hasKey("foo"))17assertThat(new HashMap<String, String>() {{ put("foo", "bar"); }}, hasValue("bar"))
endsWith
Using AI Code Generation
1import org.hamcrest.CoreMatchers.endsWith;2import org.junit.Test;3import static org.hamcrest.CoreMatchers.endsWith;4import static org.junit.Assert.assertThat;5public class EndsWithTest {6 public void testEndsWith() {7 assertThat("Hello World", endsWith("World"));8 }9}10 at org.junit.Assert.assertEquals(Assert.java:115)11 at org.junit.Assert.assertEquals(Assert.java:144)12 at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)13 at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)14 at EndsWithTest.testEndsWith(EndsWithTest.java:11)15import org.hamcrest.CoreMatchers.containsStringInOrder;16import org.junit.Test;17import static org.hamcrest.CoreMatchers.containsStringInOrder;18import static org.junit.Assert.assertThat;19import java.util.Arrays;20import java.util.List;21public class StringContainsInOrderTest {22 public void testStringContainsInOrder() {23 List<String> list = Arrays.asList("Hello", "World");24 assertThat("Hello World", containsStringInOrder(list));25 }26}27 at org.junit.Assert.assertEquals(Assert.java:115)28 at org.junit.Assert.assertEquals(Assert.java:144)29 at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)30 at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)31 at StringContainsInOrderTest.testStringContainsInOrder(StringContainsInOrderTest.java:13)
endsWith
Using AI Code Generation
1assertThat("foo", endsWith("oo"));2assertThat("foo", containsString("oo"));3assertThat("foo", allOf(endsWith("oo"), containsString("oo")));4assertThat("foo", anyOf(endsWith("oo"), containsString("oo")));5assertThat("foo", not(endsWith("oo")));6assertThat("foo", both(endsWith("oo")).and(containsString("oo")));7assertThat("foo", either(endsWith("oo")).or(containsString("oo")));8assertThat("foo", is(not(endsWith("oo"))));9assertThat("foo", is(both(endsWith("oo")).and(containsString("oo"))));10assertThat("foo", is(either(endsWith("oo")).or(containsString("oo"))));11assertThat("foo", is(not(endsWith("oo"))));12assertThat("foo", is(both(endsWith("oo")).and(containsString("oo"))));13assertThat("foo", is(either(endsWith("oo")).or(containsString("oo"))));14assertThat("foo", is(not(endsWith("oo"))));15assertThat("foo", is(both(endsWith("oo")).and(containsString("oo"))));
endsWith
Using AI Code Generation
1assertThat("Hello", endsWith("lo"));2assertThat("Hello", not(endsWith("world")));3assertThat("Hello", containsString("ell"));4assertThat("Hello", not(containsString("world")));5assertThat("Hello", startsWith("He"));6assertThat("Hello", not(startsWith("world")));7assertThat("Hello", equalTo("Hello"));8assertThat("Hello", not(equalTo("world")));9assertThat("Hello", is("Hello"));10assertThat("Hello", not(is("world")));11assertThat("Hello", not("world"));12assertThat("Hello", notNullValue());13assertThat(null, not(notNullValue()));14assertThat(null, nullValue());15assertThat("Hello", not(nullValue()));16assertThat("Hello", any(String.class));17assertThat("Hello", not(any(Integer.class)));18assertThat("Hello", isA(String.class));19assertThat("Hello", not(isA(Integer.class)));20assertThat("Hello", allOf(startsWith("He"), endsWith("lo")));21assertThat("Hello", not(allOf(startsWith("world"), endsWith("lo"))));22assertThat("Hello", anyOf(startsWith("He"), endsWith("lo")));23assertThat("Hello", not(anyOf(startsWith("world"), endsWith("lo"))));24assertThat("Hello", instanceOf(String.class));25assertThat("Hello", not(instanceOf(Integer.class)));26assertThat(Arrays.asList("Hello", "world"), hasItem("Hello"));27assertThat(Arrays.asList("Hello", "world"), not(hasItem("world")));
endsWith
Using AI Code Generation
1assertThat("foo", endsWith("oo"));2assertThat("foo", not(endsWith("ff")));3assertThat("foo", org.hamcrest.text.EndsWith.endsWith("oo"));4assertThat("foo", not(org.hamcrest.text.EndsWith.endsWith("ff")));5assertThat("foo", endsWith("oo"));6assertThat("foo", not(endsWith("ff")));7assertThat("foo", org.hamcrest.text.EndsWith.endsWith("oo"));8assertThat("foo", not(org.hamcrest.text.EndsWith.endsWith("ff")));9assertThat("foo", endsWith("oo"));10assertThat("foo", not(endsWith("ff")));11assertThat("foo", org.hamcrest.text.EndsWith.endsWith("oo"));12assertThat("foo", not(org.hamcrest.text.EndsWith.endsWith("ff")));13assertThat("foo", endsWith("oo"));14assertThat("foo", not(endsWith("ff")));15assertThat("foo", org.hamcrest.text.EndsWith.endsWith("oo"));16assertThat("foo", not(org.hamcrest.text.EndsWith.endsWith("ff")));17assertThat("foo", endsWith("oo"));18assertThat("foo", not(endsWith("ff")));19assertThat("foo", org.hamcrest.text.EndsWith.endsWith("oo"));20assertThat("foo", not(org.hamcrest.text.EndsWith.endsWith("ff")));21assertThat("foo", endsWith("oo"));22assertThat("foo", not(endsWith("ff")));23assertThat("foo", org.hamcrest.text.EndsWith.endsWith("oo"));24assertThat("foo", not(org.hamcrest.text.EndsWith.endsWith("ff")));25assertThat("foo", endsWith("oo"));26assertThat("foo", not(endsWith("ff")));27assertThat("foo", org.hamcrest.text.EndsWith.endsWith("oo"));28assertThat("foo", not(org.hamcrest.text.EndsWith.endsWith("ff")));29assertThat("foo", endsWith("oo"));30assertThat("foo", not(endsWith("ff")));31assertThat("foo", org.hamcrest.text.EndsWith.endsWith("oo"));32assertThat("foo", not(org.hamcrest.text.EndsWith.endsWith("ff")));33assertThat("foo", ends
endsWith
Using AI Code Generation
1import org.hamcrest.CoreMatchers.endsWith2import org.hamcrest.MatcherAssert.assertThat3import org.junit.Test4class StringTest {5 fun testEndsWith() {6 assertThat("Hello World", endsWith("World"))7 }8}9import org.hamcrest.CoreMatchers.endsWith10import org.hamcrest.MatcherAssert.assertThat11import org.junit.Test12class StringTest {13 fun testEndsWith() {14 assertThat("Hello World", endsWith("World"))15 }16}17import org.assertj.core.api.Assertions.assertThat18import org.junit.Test19class StringTest {20 fun testEndsWith() {21 assertThat("Hello World").endsWith("World")22 }23}
endsWith
Using AI Code Generation
1assertThat("abc", endsWith("c"));2assertThat("abc", endsWithIgnoringCase("C"));3org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)4org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)5org.example.App.main(App.java:12)6org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)7org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)8org.example.App.main(App.java:14)9org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)10org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)11org.example.App.main(App.java:16)12org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)13org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)14org.example.App.main(App.java:18)15org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)16org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)17org.example.App.main(App.java:20)18org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)19org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)20org.example.App.main(App.java:22)21org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)22org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)23org.example.App.main(App.java:24)24org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)25org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)26org.example.App.main(App.java:26)27org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)28org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)29org.example.App.main(App.java:28)30org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)31org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)32org.example.App.main(App.java:30)33org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)34org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)35org.example.App.main(App.java:32)36org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)37org.hamcrest.MatcherAssert.assertThat(Matcher
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!!