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

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

Source:DemoServiceTest.java Github

copy

Full Screen

...34import static org.hamcrest.CoreMatchers.not;35import static org.hamcrest.CoreMatchers.notNullValue;36import static org.hamcrest.CoreMatchers.nullValue;37import static org.hamcrest.CoreMatchers.sameInstance;38import static org.hamcrest.CoreMatchers.startsWith;39import static org.hamcrest.CoreMatchers.theInstance;40import static org.hamcrest.Matchers.array;41import static org.hamcrest.Matchers.arrayContaining;42import static org.hamcrest.Matchers.arrayContainingInAnyOrder;43import static org.hamcrest.Matchers.arrayWithSize;44import static org.hamcrest.Matchers.closeTo;45import static org.hamcrest.Matchers.comparesEqualTo;46import static org.hamcrest.Matchers.contains;47import static org.hamcrest.Matchers.containsInAnyOrder;48import static org.hamcrest.Matchers.empty;49import static org.hamcrest.Matchers.emptyArray;50import static org.hamcrest.Matchers.emptyCollectionOf;51import static org.hamcrest.Matchers.emptyIterable;52import static org.hamcrest.Matchers.equalToIgnoringCase;53import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;54import static org.hamcrest.Matchers.eventFrom;55import static org.hamcrest.Matchers.greaterThan;56import static org.hamcrest.Matchers.greaterThanOrEqualTo;57import static org.hamcrest.Matchers.hasEntry;58import static org.hamcrest.Matchers.hasItemInArray;59import static org.hamcrest.Matchers.hasKey;60import static org.hamcrest.Matchers.hasProperty;61import static org.hamcrest.Matchers.hasSize;62import static org.hamcrest.Matchers.hasToString;63import static org.hamcrest.Matchers.hasValue;64import static org.hamcrest.Matchers.isEmptyOrNullString;65import static org.hamcrest.Matchers.isEmptyString;66import static org.hamcrest.Matchers.isIn;67import static org.hamcrest.Matchers.isOneOf;68import static org.hamcrest.Matchers.iterableWithSize;69import static org.hamcrest.Matchers.lessThan;70import static org.hamcrest.Matchers.lessThanOrEqualTo;71import static org.hamcrest.Matchers.samePropertyValuesAs;72import static org.hamcrest.Matchers.stringContainsInOrder;73import static org.hamcrest.Matchers.typeCompatibleWith;74;75/**76 * Description: service unit test77 * Author: zhangm@seaskylight.com78 * Date: 2018/3/7 14:2379 */80public class DemoServiceTest extends BaseTest {81 @Autowired82 private DemoService demoService;83 @Test84 public void testHi() {85 String name = "test";86 Assert.assertEquals(demoService.hi(name), "hi:" + name);87 // 实际值(actual)与期望值(expected)相等88 Assert.assertThat("hi:test", is(equalTo(demoService.hi(name))));89 // 匹配给定类型的任何变量.90 Assert.assertThat("name", any(String.class));91 // 匹配任何东西。92 Assert.assertThat(5, anything());93 // 输入实际值大于指定值94 Assert.assertThat(11, greaterThan(10));95 // 输入实际值小于指定值96 Assert.assertThat(1, lessThan(2));97 // 输入测试值大于或等于指定值98 Assert.assertThat(10, greaterThanOrEqualTo(10));99 // 输入测试值小于或等于指定值100 Assert.assertThat(10, lessThanOrEqualTo(10));101 // 数组的长度必须与匹配器的数量匹配,并且它们的顺序很重要。102 String[] strings = {"why", "hello", "there"};103 Assert.assertThat(strings, arrayContaining("why", "hello", "there"));104 // vararg匹配器,如果输入String以给定的子字符串结尾,或者开头,则匹配的匹配器。以及相等。105 Assert.assertThat(strings, arrayContaining(startsWith("w"), equalTo("hello"), endsWith("re")));106 // 它们的顺序并不重要107 Assert.assertThat(strings, arrayContainingInAnyOrder("hello", "there", "why"));108 Assert.assertThat(strings, arrayWithSize(3));109 // 用于检查数组是否具有特定长度。110 ArrayList<Object> objects = new ArrayList<>();111 objects.add(0, "o");112 objects.add(1, "a");113 objects.add(2, "b");114 objects.add(3, "c");115 Assert.assertThat(objects.toArray(), arrayWithSize(greaterThan(2)));116 // 输入Iterable具有指定大小时匹配或与指定大小匹配117 Assert.assertThat(objects, iterableWithSize(4));118 // 可以与Double或BigDecimal一起使用的匹配器,以检查值是否在期望值的指定误差范围内。119 Double i = 6.5;120 BigDecimal b = BigDecimal.valueOf(i);121 Assert.assertThat(i, closeTo(6, 0.5));122 Assert.assertThat(b, closeTo(BigDecimal.valueOf(6L), BigDecimal.valueOf(0.5d)));123 Assert.assertThat(BigDecimal.valueOf(600L), closeTo(BigDecimal.valueOf(650L), BigDecimal.valueOf(50L)));124 // 输入的匹配器值返回0,则匹配器将匹配,否则将不匹配125 Assert.assertThat(i, comparesEqualTo(6.5));126 Assert.assertThat(i, equalTo(6.5));127 // 测试集合的值,数相匹配128 Assert.assertThat(Arrays.asList(strings), contains("why", "hello", "there"));129 // 测试集合的值,数相匹配,顺序可以不同130 Assert.assertThat(Arrays.asList(strings), containsInAnyOrder("hello", "why", "there"));131 // 字符串是否包含给定的子字符串132 Assert.assertThat("value", containsString("l"));133 // 匹配空集合134 ArrayList<Object> list = Lists.newArrayList();135 Assert.assertThat(list, empty());136 // 如果输入数组的长度为0,则匹配的匹配器137 Double[] doubles = new Double[0];138 Assert.assertThat(doubles, emptyArray());139 // Typesafe匹配器,如果输入集合为给定类型且为空,则匹配。140 HashSet<String> set = new HashSet<>();141 Assert.assertThat(set, emptyCollectionOf(String.class));142 // 如果输入Iterable没有值,则匹配的匹配器。143 Assert.assertThat(set, emptyIterable());144 // Typesafe Matcher,如果输入的Iterable没有值并且是给定类型,则匹配。145 Assert.assertThat(set, emptyCollectionOf(String.class));146 // 如果输入的String值等于指定的String而忽略大小写,则匹配的匹配器。147 Assert.assertThat("value", equalToIgnoringCase("VAlUe"));148 // 输入的String值等于指定的String而不匹配多余的空格149 String testValue = "this is my value ";150 Assert.assertThat(testValue, equalToIgnoringWhiteSpace(" This Is My value"));151 // 如果输入EventObject来自给定源,则匹配器。也可以接受EventObject指定子类型的。152 Object source = new Object();153 EventObject testEvent = new EventObject(source);154 Assert.assertThat(testEvent, eventFrom(source));155 // EventObject指定子类型的156 EventObject menuEvent = new MenuEvent(source);157 Assert.assertThat(menuEvent, eventFrom(MenuEvent.class, source));158 // 给定映射包含与指定键和值匹配的条目159 HashMap<Integer, String> map = new HashMap<>();160 map.put(1, "hi");161 map.put(2, "hello");162 Assert.assertThat(map, hasEntry(2, "hello"));163 Assert.assertThat(map, hasEntry(greaterThan(1), startsWith("h")));164 // 输入Map具有至少一个与指定值或匹配项匹配的键165 Assert.assertThat(map, hasKey(1));166 Assert.assertThat(map, hasKey(lessThan(2)));167 // 输入Map具有至少一个与指定值或匹配器匹配的值168 Assert.assertThat(map, hasValue(startsWith("h")));169 // 输入Iterable具有至少一个与指定值或匹配器匹配的项目170 Assert.assertThat(objects, hasItems("a", "o"));171 ArrayList<Integer> integers = Lists.newArrayList(1, 2, 3, 4, 5, 6);172 Assert.assertThat(integers, hasItem(lessThan(3)));173 // 输入数组具有至少一个与指定值或匹配器匹配的项目174 Assert.assertThat(strings, hasItemInArray("there"));175 Assert.assertThat(strings, hasItemInArray(startsWith("t")));176 // 输入对象满足Bean约定并且具有具有指定名称的属性,并且该属性的值可选地177 User user = new User();178 user.setAge(12);179 user.setName("fei");180 Assert.assertThat(user, hasProperty("age", greaterThan(10)));181 Assert.assertThat(user, hasProperty("name"));182 // 输入Collection具有指定的大小,或者它的大小与指定的匹配器匹配.183 Assert.assertThat(integers, hasSize(lessThan(10)));184 // 输入对象的toString()方法匹配指定的String或输入匹配器.185 Assert.assertThat(3.1415926, hasToString("3.1415926"));186 Assert.assertThat(3.1415926, hasToString(containsString(".")));187 // 输入对象是给定类型188 Assert.assertThat("Hello World !", instanceOf(String.class));189 // 输入字符串为空或null时匹配190 Assert.assertThat("", isEmptyOrNullString());191 Assert.assertThat(null, isEmptyOrNullString());192 // 输入字符串为空时匹配193 Assert.assertThat("", isEmptyString());194 // 空值与非空值匹配195 Assert.assertThat(null, nullValue());196 Assert.assertThat("", notNullValue());197 // 当在给定的Collection或Array中找到输入项时匹配198 Assert.assertThat(6, isIn(integers));199 // 当输入对象是给定对象之一时匹配200 Assert.assertThat("!", isOneOf("$", "#", "%", "!", "¥"));201 // 反转其匹配逻辑202 Assert.assertThat("apple", not("orange"));203 Assert.assertThat(5, not(lessThan(2)));204 // 输入对象与指定值相同的实例时匹配205 User user1 = user;206 Assert.assertThat(user, sameInstance(user1));207 Assert.assertThat(user, theInstance(user1));208 //输入Bean具有与指定Bean相同的属性值时匹配209 User user2 = new User();210 user2.setAge(12);211 user2.setName("fei");212 Assert.assertThat(user, samePropertyValuesAs(user2));213 // 输入字符串包含给定Iterable中的子字符串,则按从Iterable返回的顺序进行匹配214 String test = "Rule number one: two's company, but three's a crowd!";215 Assert.assertThat(test, stringContainsInOrder(Arrays.asList("one", "two", "three")));216 //输入类型的对象可以分配给指定基本类型的引用时进行匹配217 Assert.assertThat(User.class, typeCompatibleWith(Object.class));218 // 组合匹配器219 // 类似于逻辑AND220 Assert.assertThat(test, allOf(startsWith("R"), endsWith("!")));221 List<Matcher<? super String>> matchers = Arrays.asList(containsString("one"), startsWith("R"));222 Assert.assertThat(test, allOf(matchers));223 // 类似于逻辑或224 Assert.assertThat(test, anyOf(containsString("hello"), instanceOf(String.class)));225 Assert.assertThat(test, anyOf(matchers));226 // 输入数组的元素分别使用指定的匹配器按顺序匹配时匹配。匹配器的数量必须等于数组的大小。227 Assert.assertThat(strings, array(startsWith("w"), equalTo("hello"), notNullValue()));228 // 当与它的可组合匹配器.and()结合使用时,将在两个指定的匹配器匹配时匹配。(都)229 Assert.assertThat(test, both(startsWith("R")).and(containsString("two")));230 // 当与它的可组合匹配器.or()结合使用时,如果指定的匹配器匹配(要么)231 Assert.assertThat(test, either(notNullValue()).or(instanceOf(String.class)));232 // 当输入匹配器匹配时匹配 类似 not233 Assert.assertThat(1, Matchers.is(1));234 // 用于覆盖另一个匹配器的失败输出。当需要自定义故障输出时使用。参数是失败消息,原始Matcher,然后是将使用占位符%0,%1,%2格式化为失败消息的所有值。235 Integer actual = 7;236 Integer expected = 10;237 Assert.assertThat(actual, describedAs("input > %0", greaterThan(expected), expected));238 }239 @Test240 public void testHello(){241 // 自定义匹配器242 // 判断偶数243 int i = 4;...

Full Screen

Full Screen

Source:HamcrestCoreMatchersTest.java Github

copy

Full Screen

...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() {226227 // GIVEN228 String testString = "Hamcrest Core Matchers";229230 // ASSERT231 assertThat(testString, both(startsWith("Ham")).and(containsString("Core")));232 }233234 @Test235 public void givenTestInput_WhenUsingEitherForMatcher() {236237 // GIVEN238 String testString = "Hamcrest Core Matchers";239240 // ASSERT241 assertThat(testString, either(startsWith("Bael")).or(containsString("Core")));242 }243244245 @Test246 public void givenTestInput_WhenUsingEveryItemForMatchInCollection() {247248 // GIVEN249 List<String> testItems = Lists.newArrayList("Common", "Core", "Combinable");250251 // ASSERT252 assertThat(testItems, everyItem(startsWith("Co")));253 }254255 @Test256 public void givenTwoTestInputs_WhenUsingSameInstanceForMatch() {257258 // GIVEN259 String string1 = "hamcrest";260 String string2 = string1;261262 // ASSERT263 assertThat(string1, is(sameInstance(string2)));264 }265266 @Test ...

Full Screen

Full Screen

Source:CoreMatchers.java Github

copy

Full Screen

...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}...

Full Screen

Full Screen

Source:AssertThatTest.java Github

copy

Full Screen

...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}...

Full Screen

Full Screen

Source:AssertThatDemoTest.java Github

copy

Full Screen

...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}...

Full Screen

Full Screen

Source:AssertTests.java Github

copy

Full Screen

...7import static org.hamcrest.CoreMatchers.everyItem;8import static org.hamcrest.CoreMatchers.hasItems;9import static org.hamcrest.CoreMatchers.not;10import static org.hamcrest.CoreMatchers.sameInstance;11import static org.hamcrest.CoreMatchers.startsWith;12import static org.junit.Assert.assertArrayEquals;13import static org.junit.Assert.assertEquals;14import static org.junit.Assert.assertFalse;15import static org.junit.Assert.assertNotNull;16import static org.junit.Assert.assertNotSame;17import static org.junit.Assert.assertNull;18import static org.junit.Assert.assertSame;19import static org.junit.Assert.assertThat;20import static org.junit.Assert.assertTrue;21import java.util.Arrays;22import org.hamcrest.core.CombinableMatcher;23import org.junit.Ignore;24import org.junit.Test;25public class AssertTests {26 @Test27 public void testAssertArrayEquals() {28 byte[] expected = "trial".getBytes();29 byte[] actual = "trial".getBytes();30 assertArrayEquals("failure - byte arrays not same", expected, actual);31 }32 @Test33 public void testAssertEquals() {34 assertEquals("failure - strings are not equal", "text", "text");35 }36 @Test37 public void testAssertFalse() {38 assertFalse("failure - should be false", false);39 }40 @Test41 public void testAssertNotNull() {42 assertNotNull("should not be null", new Object());43 }44 @Test45 public void testAssertNotSame() {46 assertNotSame("should not be same Object", new Object(), new Object());47 }48 @Test49 public void testAssertNull() {50 assertNull("should be null", null);51 }52 @Test53 public void testAssertSame() {54 Integer aNumber = 768;55 assertSame("should be same", aNumber, aNumber);56 }57 // JUnit Matchers assertThat58 @Test59 public void testAssertThatBothContainsString() {60 assertThat("albumen", both(containsString("a")).and(containsString("b")));61 }62 @Test63 public void testAssertThatHasItems() {64 assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));65 }66 @Test67 public void testAssertThatEveryItemContainsString() {68 assertThat(Arrays.asList("fun", "ban", "net"), everyItem(containsString("n")));69 }70 // Core Hamcrest Matchers with assertThat71 @Test72 public void testAssertThatHamcrestCoreMatchers() {73 assertThat("good", allOf(equalTo("good"), startsWith("good")));74 assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));75 assertThat("good", anyOf(equalTo("bad"), equalTo("good")));76 assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));77 assertThat(new Object(), not(sameInstance(new Object())));78 }79 @Test80 public void testAssertTrue() {81 assertTrue("failure - should be true", true);82 }83}...

Full Screen

Full Screen

Source:combinational_collection_assert.java Github

copy

Full Screen

...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}...

Full Screen

Full Screen

Source:HamcrestMatchersTest.java Github

copy

Full Screen

1package com.mhuffers.unittesting.unittesting.business;2import static org.hamcrest.CoreMatchers.containsString;3import static org.hamcrest.CoreMatchers.everyItem;4import static org.hamcrest.CoreMatchers.hasItems;5import static org.hamcrest.CoreMatchers.startsWith;6import static org.hamcrest.MatcherAssert.assertThat;7import static org.hamcrest.Matchers.greaterThan;8import static org.hamcrest.Matchers.hasSize;9import static org.hamcrest.Matchers.isEmptyString;10import static org.hamcrest.Matchers.lessThan;11import java.util.Arrays;12import java.util.List;13import org.junit.Test;14public class HamcrestMatchersTest {15 @Test16 public void assertsUsingHamcrest() {17 List<Integer> list = Arrays.asList(1,2,3,4);18 19 assertThat(list,hasSize(4));20 assertThat(list,hasItems(3,1));21 assertThat(list,everyItem(greaterThan(0)));22 assertThat(list,everyItem(lessThan(5)));23 24 assertThat("",isEmptyString());25 assertThat("ABCD",containsString("BC"));26 assertThat("ABCD",startsWith("AB"));27 }28}...

Full Screen

Full Screen

startsWith

Using AI Code Generation

copy

Full Screen

1import static org.hamcrest.CoreMatchers.startsWith;2import static org.hamcrest.MatcherAssert.assertThat;3import org.hamcrest.core.StringStartsWith;4public class StringStartsWithExample {5 public static void main(String[] args) {6 StringStartsWith startsWith = new StringStartsWith("java");7 assertThat("java is a programming language", startsWith);8 }9}

Full Screen

Full Screen

startsWith

Using AI Code Generation

copy

Full Screen

1import static org.hamcrest.CoreMatchers.startsWith;2import static org.junit.Assert.assertThat;3import org.junit.Test;4public class HamcrestTest {5 public void test() {6 assertThat("Hello World", startsWith("Hello"));7 }8}9[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HamcrestTest ---10[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HamcrestTest ---11[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HamcrestTest ---12[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ HamcrestTest ---13[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HamcrestTest ---

Full Screen

Full Screen

startsWith

Using AI Code Generation

copy

Full Screen

1import org.hamcrest.Matchers.startsWith2import org.hamcrest.Matchers.endsWith3def 'startsWith'() {4}5def 'endsWith'() {6 str.endsWith('bcd')

Full Screen

Full Screen

startsWith

Using AI Code Generation

copy

Full Screen

1import org.hamcrest.CoreMatchers.startsWith2import org.junit.Test3import static org.hamcrest.CoreMatchers.startsWith4import static org.junit.Assert.assertThat5class HamcrestTest {6 void test() {7 assertThat(str, startsWith("Hello"))8 }9}10import org.hamcrest.CoreMatchers.startsWith11import org.junit.Test12import static org.hamcrest.CoreMatchers.startsWith13import static org.junit.Assert.assertThat14class HamcrestTest {15 void test() {16 assertThat(str, startsWith("Hello"))17 }18}19import org.hamcrest.CoreMatchers.startsWith20import org.junit.Test21import static org.hamcrest.CoreMatchers.startsWith22import static org.junit.Assert.assertThat23class HamcrestTest {24 void test() {25 assertThat(str, startsWith("Hello"))26 }27}28import org.hamcrest.CoreMatchers.startsWith29import org.junit.Test30import static org.hamcrest.CoreMatchers.startsWith31import static org.junit.Assert.assertThat32class HamcrestTest {33 void test() {34 assertThat(str, startsWith("Hello"))35 }36}37import org.hamcrest.CoreMatchers.startsWith38import org.junit.Test39import static org.hamcrest.CoreMatchers.startsWith40import static org.junit.Assert.assertThat41class HamcrestTest {42 void test() {43 assertThat(str, startsWith("Hello"))44 }45}46import org.hamcrest.CoreMatchers.startsWith47import org.junit.Test48import static org.hamcrest.CoreMatchers.startsWith49import static org.junit.Assert.assertThat50class HamcrestTest {51 void test() {52 assertThat(str, startsWith("Hello"))53 }54}55import org.hamcrest.CoreMatchers.startsWith56import org.junit.Test57import static org.hamcrest.CoreMatchers.startsWith58import static org.junit.Assert.assertThat59class HamcrestTest {60 void test() {

Full Screen

Full Screen

startsWith

Using AI Code Generation

copy

Full Screen

1import static org.hamcrest.CoreMatchers.startsWith;2import static org.hamcrest.MatcherAssert.assertThat;3import static org.hamcrest.Matchers.*;4import java.util.Arrays;5import java.util.List;6import org.hamcrest.Matcher;7import org.hamcrest.Matchers;8import org.junit.Test;9public class HamcrestExample {10 public void test() {11 assertThat("Hello World", startsWith("Hello"));12 assertThat("Hello World", containsString("World"));13 assertThat("Hello World", endsWith("World"));14 assertThat("Hello World", equalTo("Hello World"));15 assertThat("Hello World", is("Hello World"));16 assertThat("Hello World", is(equalTo("Hello World")));17 assertThat("Hello World", is(equalToIgnoringCase("hello world")));18 assertThat("Hello World", is(not("Hello World!")));19 assertThat("Hello World", is(notNullValue()));20 assertThat("Hello World", is(notNullValue(String.class)));21 assertThat("Hello World", is(notNullValue()));22 assertThat("Hello World", is(notNullValue(String.class)));23 assertThat("Hello World", is(notNullValue()));24 assertThat("Hello World", is(notNullValue(String.class)));25 assertThat("Hello World", is(notNullValue()));26 assertThat("Hello World", is(notNullValue(String.class)));27 assertThat(1, is(1));28 assertThat(1, is(not(2)));29 assertThat(1, is(lessThan(2)));30 assertThat(1, is(greaterThan(0)));31 assertThat(1, is(lessThanOrEqualTo(1)));32 assertThat(1, is(greaterThanOrEqualTo(1)));33 assertThat(1, is(both(greaterThan(0)).and(lessThan(2))));34 assertThat(1, is(both(greaterThan(0)).and(lessThan(2))));35 assertThat(1, is(both(greaterThan(0)).and(lessThan(2))));36 assertThat(1, is(both(greaterThan(0)).and(lessThan(2))));37 assertThat(1, is(both(greaterThan(0)).and(lessThan(2))));38 assertThat(1, is(both(greaterThan(0)).and(lessThan(2))));39 assertThat(1, is(not(both(greaterThan(0)).and(lessThan(2)))));40 assertThat(1, is(both(greaterThan(0)).and

Full Screen

Full Screen

JUnit Tutorial:

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.

JUnit Tutorial Chapters:

Here are the detailed JUnit testing chapters to help you get started:

  • Importance of Unit testing - Learn why Unit testing is essential during the development phase to identify bugs and errors.
  • Top Java Unit testing frameworks - Here are the upcoming JUnit automation testing frameworks that you can use in 2023 to boost your unit testing.
  • What is the JUnit framework
  • Why is JUnit testing important - Learn the importance and numerous benefits of using the JUnit testing framework.
  • Features of JUnit - Learn about the numerous features of JUnit and why developers prefer it.
  • JUnit 5 vs. JUnit 4: Differences - Here is a complete comparison between JUnit 5 and JUnit 4 testing frameworks.
  • Setting up the JUnit environment - Learn how to set up your JUnit testing environment.
  • Getting started with JUnit testing - After successfully setting up your JUnit environment, this chapter will help you get started with JUnit testing in no time.
  • Parallel testing with JUnit - Parallel Testing can be used to reduce test execution time and improve test efficiency. Learn how to perform parallel testing with JUnit.
  • Annotations in JUnit - When writing automation scripts with JUnit, we can use JUnit annotations to specify the type of methods in our test code. This helps us identify those methods when we run JUnit tests using Selenium WebDriver. Learn in detail what annotations are in JUnit.
  • Assertions in JUnit - Assertions are used to validate or test that the result of an action/functionality is the same as expected. Learn in detail what assertions are and how to use them while performing JUnit testing.
  • Parameterization in JUnit - Parameterized Test enables you to run the same automated test scripts with different variables. By collecting data on each method's test parameters, you can minimize time spent on writing tests. Learn how to use parameterization in JUnit.
  • Nested Tests In JUnit 5 - A nested class is a non-static class contained within another class in a hierarchical structure. It can share the state and setup of the outer class. Learn about nested annotations in JUnit 5 with examples.
  • Best practices for JUnit testing - Learn about the best practices, such as always testing key methods and classes, integrating JUnit tests with your build, and more to get the best possible results.
  • Advanced Use Cases for JUnit testing - Take a deep dive into the advanced use cases, such as how to run JUnit tests in Jupiter, how to use JUnit 5 Mockito for Unit testing, and more for JUnit testing.

JUnit Certification:

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.

Run junit automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful