How to use describeMismatch method of org.hamcrest.BaseMatcher class

Best junit code snippet using org.hamcrest.BaseMatcher.describeMismatch

Source:FileMatchers.java Github

copy

Full Screen

...49 public void describeTo(Description description) {50 description.appendText("file that exists");51 }52 @Override53 public void describeMismatch(Object item, Description description) {54 if (item instanceof File) {55 description.appendValue(item);56 description.appendText(" does not exist");57 } else {58 description.appendValue(item);59 description.appendText(" was not a File object");60 }61 }62 };63 }64 /** Matcher for a regular (non-directory) file */65 public static Matcher<File> file() {66 return new BaseMatcher<File>() {67 @Override68 public boolean matches(Object item) {69 if (item instanceof File) {70 return ((File) item).isFile();71 } else {72 return false;73 }74 }75 @Override76 public void describeTo(Description description) {77 description.appendText("file that is a file (Not a directory)");78 }79 @Override80 public void describeMismatch(Object item, Description description) {81 if (item instanceof File) {82 if (((File) item).exists()) {83 description.appendValue(item);84 description.appendText(" is a directory");85 } else {86 description.appendValue(item);87 description.appendText(" does not exist");88 }89 } else {90 description.appendValue(item);91 description.appendText(" was not a File object");92 }93 }94 };95 }96 /** Matcher for a directory */97 public static Matcher<File> directory() {98 return new BaseMatcher<File>() {99 @Override100 public boolean matches(Object item) {101 if (item instanceof File) {102 return ((File) item).isDirectory();103 } else {104 return false;105 }106 }107 @Override108 public void describeTo(Description description) {109 description.appendText("file that is a directory");110 }111 @Override112 public void describeMismatch(Object item, Description description) {113 if (item instanceof File) {114 if (((File) item).exists()) {115 description.appendValue(item);116 description.appendText(" is not a directory");117 } else {118 description.appendValue(item);119 description.appendText(" does not exist");120 }121 } else {122 description.appendValue(item);123 description.appendText(" was not a File object");124 }125 }126 };127 }128 /** Matcher for a directory's contents */129 public static Matcher<File> directoryContaining(Matcher<Iterable<File>> filesMatcher) {130 return new BaseMatcher<File>() {131 @Override132 public boolean matches(Object item) {133 if (item instanceof File) {134 return ((File) item).isDirectory()135 && filesMatcher.matches(Arrays.asList(((File) item).listFiles()));136 } else {137 return false;138 }139 }140 @Override141 public void describeTo(Description description) {142 description.appendText("directory that contains ");143 description.appendDescriptionOf(filesMatcher);144 }145 @Override146 public void describeMismatch(Object item, Description description) {147 if (!(item instanceof File)) {148 description.appendValue(item);149 description.appendText(" was not a File object");150 } else if (!((File) item).exists()) {151 description.appendValue(item);152 description.appendText(" does not exist");153 } else if (!((File) item).isDirectory()) {154 description.appendValue(item);155 description.appendText(" is not a directory");156 } else {157 description.appendValue(item);158 description.appendText(" had files ");159 filesMatcher.describeMismatch(((File) item).listFiles(), description);160 }161 }162 };163 }164 public static Matcher<File> directoryEmpty() {165 return directoryContaining(Matchers.emptyIterableOf(File.class));166 }167 /** Matcher for last modified time */168 public static Matcher<File> lastModified(final Matcher<Long> timeMatcher) {169 return new BaseMatcher<File>() {170 @Override171 public boolean matches(Object item) {172 if (item instanceof File) {173 return timeMatcher.matches(((File) item).lastModified());174 } else {175 return false;176 }177 }178 @Override179 public void describeTo(Description description) {180 description.appendText("file last modified ");181 description.appendDescriptionOf(timeMatcher);182 }183 @Override184 public void describeMismatch(Object item, Description description) {185 if (item instanceof File) {186 if (((File) item).exists()) {187 description.appendValue(item);188 description.appendText(" had modification time ");189 timeMatcher.describeMismatch(((File) item).lastModified(), description);190 } else {191 description.appendValue(item);192 description.appendText(" does not exist");193 }194 } else {195 description.appendValue(item);196 description.appendText(" was not a File object");197 }198 }199 };200 }201 /** Matcher for file with a particular name */202 @SuppressWarnings("unchecked")203 public static Matcher<File> named(String name) {204 return both(instanceOf(File.class)).and((Matcher) hasProperty("name", equalTo(name)));205 }206 /**207 * Executes the given {@link Callable} and then returns a matcher for values of {@link208 * System#currentTimeMillis()} during the execution.209 */210 public static Matcher<Long> whileRunning(Callable<Void> stuffToDo) throws Exception {211 final long start = System.currentTimeMillis();212 stuffToDo.call();213 final long end = System.currentTimeMillis();214 return both(greaterThan(start)).and(lessThan(end));215 }216 public static Matcher<Resource> resource(final Resource expected) {217 return new BaseMatcher<Resource>() {218 @Override219 public boolean matches(Object item) {220 if (item instanceof Resource) {221 try (InputStream itemStream = ((Resource) item).getInputStream();222 InputStream expectedStream = expected.getInputStream(); ) {223 return IOUtils.contentEquals(itemStream, expectedStream);224 } catch (IOException e) {225 return false;226 }227 } else {228 return false;229 }230 }231 @Override232 public void describeTo(Description description) {233 description.appendText("Resource with content equal to that given");234 }235 @Override236 public void describeMismatch(Object item, Description description) {237 if (item instanceof Resource) {238 description.appendText("did not match given Resource");239 } else {240 description.appendText("was not a Resource");241 }242 }243 };244 }245}...

Full Screen

Full Screen

Source:MatcherGenericTypeExtractorTest.java Github

copy

Full Screen

...46 private class IntMatcherFromInterface extends BaseMatcher<Integer> {47 public boolean matches(Object o) {48 return true;49 }50 public void describeMismatch(Object item, Description mismatchDescription) {}51 public void describeTo(Description description) {}52 }53 //Static Matcher interface implementation (instead of the BaseMatcher)54 private static class StaticIntMatcherFromInterface extends BaseMatcher<Integer> {55 public boolean matches(Object o) {56 return true;57 }58 public void describeMismatch(Object item, Description mismatchDescription) {}59 public void describeTo(Description description) {}60 }61 //non-generic matcher implementing the interface62 @SuppressWarnings("rawtypes")63 private static class NonGenericMatcherFromInterface extends BaseMatcher {64 public boolean matches(Object o) {65 return true;66 }67 public void describeMismatch(Object item, Description mismatchDescription) {}68 public void describeTo(Description description) {}69 }70 private interface IMatcher extends Matcher<Integer> {}71 //non-generic matcher implementing the interface72 private static class SubclassGenericMatcherFromInterface extends BaseMatcher<Integer> implements Serializable, Cloneable, IMatcher {73 public boolean matches(Object o) {74 return true;75 }76 public void describeMismatch(Object item, Description mismatchDescription) {}77 public void describeTo(Description description) {}78 }79 //I refuse to comment on the sanity of this case80 private static class InsaneEdgeCase extends SubclassGenericMatcherFromInterface {}81 @Test82 public void findsGenericType() {83 assertEquals(Integer.class, genericTypeOfMatcher(IntMatcher.class));84 assertEquals(Integer.class, genericTypeOfMatcher(StaticIntMatcher.class));85 assertEquals(Integer.class, genericTypeOfMatcher(IntMatcherFromInterface.class));86 assertEquals(Integer.class, genericTypeOfMatcher(StaticIntMatcherSubclass.class));87 assertEquals(Integer.class, genericTypeOfMatcher(IntMatcherFromInterface.class));88 assertEquals(Integer.class, genericTypeOfMatcher(StaticIntMatcherFromInterface.class));89 assertEquals(Integer.class, genericTypeOfMatcher(SubclassGenericMatcherFromInterface.class));90 assertEquals(Integer.class, genericTypeOfMatcher(InsaneEdgeCase.class));91 assertEquals(Integer.class, genericTypeOfMatcher(new BaseMatcher<Integer>() {92 public void describeTo(Description description) {93 }94 public boolean matches(Object o) {95 return false;96 }97 }.getClass()));98 assertEquals(Integer.class, genericTypeOfMatcher(new BaseMatcher<Integer>() {99 public void describeTo(Description description) {100 }101 public boolean matches(Object o) {102 return false;103 }104 public void describeMismatch(Object item, Description mismatchDescription) {105 }106 }.getClass()));107 assertEquals(Object.class, genericTypeOfMatcher(Object.class));108 assertEquals(Object.class, genericTypeOfMatcher(String.class));109 assertEquals(Object.class, genericTypeOfMatcher(HashMap.class));110 assertEquals(Object.class, genericTypeOfMatcher(new HashMap<String, String>() {111 }.getClass()));112 assertEquals(Object.class, genericTypeOfMatcher(NonGenericMatcher.class));113 assertEquals(Object.class, genericTypeOfMatcher(NonGenericMatcherFromInterface.class));114 }115}...

Full Screen

Full Screen

Source:Matchers.java Github

copy

Full Screen

...47 description.appendText(currentTime.toString());48 }4950 @Override51 public void describeMismatch(Object item, Description description) {52 Date date = (Date) item;53 description54 .appendText(Long.toString(date.getTime()))55 .appendText(" (").appendText(Long.toString(abs(date.getTime() - currentTime)))56 .appendText(" ms)");57 }58 };59 }6061 public static org.hamcrest.Matcher<Long> equalToWithin(long expectedValue, long precision) {62 return new BaseMatcher<Long>() {6364 @Override65 public boolean matches(Object item) {66 Long value = (Long) item;67 long difference = value - expectedValue;68 return abs(difference) <= precision;69 }7071 @Override72 public void describeTo(Description description) {73 description.appendText(Long.toString(expectedValue));74 }7576 @Override77 public void describeMismatch(Object item, Description description) {78 Long value = (Long) item;79 description80 .appendText(Long.toString(value))81 .appendText(" (").appendText(Long.toString(abs(value - expectedValue)))82 .appendText(")");83 }84 };85 }8687 public static <T> org.hamcrest.Matcher<Observable<T>> hasOnlyOneItem() {88 return hasOnlyOneItem(null, item -> true);89 }9091 @SuppressWarnings("WeakerAccess")92 public static <T> org.hamcrest.Matcher<Observable<T>> hasOnlyOneItem(String descr, Func1<? super T, Boolean> predicate) {93 return new BaseMatcher<Observable<T>>() {94 @Override95 public boolean matches(Object item) {96 List<T> list = getList(item);97 System.out.println(list);98 return list.size() == 1;99 }100101 @Override102 public void describeTo(Description description) {103 description.appendText("only one item");104 if (descr != null)105 description.appendText(" ").appendText(descr);106 }107108 @Override109 public void describeMismatch(Object item, Description description) {110 List<T> list = getList(item);111 description.appendText(list.toString());112 }113114 private List<T> getList(Object item) {115 //noinspection unchecked116 Observable<T> observable = (Observable<T>) item;117 return observable.filter(predicate).toList().toBlocking().single();118 }119 };120 }121} ...

Full Screen

Full Screen

Source:StartMatchers.java Github

copy

Full Screen

...39 description.appendText("Path should exist");40 }41 42 @Override43 public void describeMismatch(Object item, Description description)44 {45 description.appendText("Path did not exist ").appendValue(item);46 }47 };48 }49 50 public static Matcher<Path> notPathExists()51 {52 return new BaseMatcher<Path>()53 {54 @Override55 public boolean matches(Object item)56 {57 final Path path = (Path)item;58 return !Files.exists(path);59 }60 @Override61 public void describeTo(Description description)62 {63 description.appendText("Path should not exist");64 }65 66 @Override67 public void describeMismatch(Object item, Description description)68 {69 description.appendText("Path exists ").appendValue(item);70 }71 };72 }73 74 public static Matcher<Path> fileExists()75 {76 return new BaseMatcher<Path>()77 {78 @Override79 public boolean matches(Object item)80 {81 final Path path = (Path)item;82 return Files.exists(path) && Files.isRegularFile(path);83 }84 @Override85 public void describeTo(Description description)86 {87 description.appendText("File should exist");88 }89 90 @Override91 public void describeMismatch(Object item, Description description)92 {93 description.appendText("File did not exist ").appendValue(item);94 }95 };96 }97}

Full Screen

Full Screen

Source:DescribedAs.java Github

copy

Full Screen

...30 description.appendText(this.descriptionTemplate.substring(textStart));31 }32 }33 @Override // org.hamcrest.BaseMatcher, org.hamcrest.Matcher34 public void describeMismatch(Object item, Description description) {35 this.matcher.describeMismatch(item, description);36 }37 public static <T> Matcher<T> describedAs(String description, Matcher<T> matcher2, Object... values2) {38 return new DescribedAs(description, matcher2, values2);39 }40}...

Full Screen

Full Screen

Source:TypeSafeMatcher.java Github

copy

Full Screen

...14 protected TypeSafeMatcher(ReflectiveTypeFinder typeFinder) {15 this.expectedType = typeFinder.findExpectedType(getClass());16 }17 /* access modifiers changed from: protected */18 public void describeMismatchSafely(T item, Description mismatchDescription) {19 super.describeMismatch(item, mismatchDescription);20 }21 @Override // org.hamcrest.Matcher22 public final boolean matches(Object item) {23 return item != null && this.expectedType.isInstance(item) && matchesSafely(item);24 }25 @Override // org.hamcrest.BaseMatcher, org.hamcrest.Matcher26 public final void describeMismatch(Object item, Description description) {27 if (item == null) {28 super.describeMismatch(null, description);29 } else if (!this.expectedType.isInstance(item)) {30 description.appendText("was a ").appendText(item.getClass().getName()).appendText(" (").appendValue(item).appendText(")");31 } else {32 describeMismatchSafely(item, description);33 }34 }35}...

Full Screen

Full Screen

Source:SrpMatchers.java Github

copy

Full Screen

...12 public boolean matches(Object o) {13 return ((Map) o).isEmpty();14 }15 @Override16 public void describeMismatch(Object item, Description description) {17 super.describeMismatch(mapSizeMessage(item), description);18 }19 @Override20 public void describeTo(Description description) {21 description.appendText("Empty Map");22 }23 };24 }25 public static <K, V> org.hamcrest.Matcher<Map<K, V>> mapWithSize(final int size) {26 return new BaseMatcher<Map<K, V>>() {27 @Override28 public boolean matches(Object o) {29 return ((Map) o).size() == size;30 }31 @Override32 public void describeMismatch(Object item, Description description) {33 super.describeMismatch(mapSizeMessage(item), description);34 }35 @Override36 public void describeTo(Description description) {37 description.appendText("Map with size " + size);38 }39 };40 }41 private static String mapSizeMessage(Object item) {42 return "Map with size " + ((Map) item).size() + ": " + item;43 }44}...

Full Screen

Full Screen

Source:Is.java Github

copy

Full Screen

...15 public void describeTo(Description description) {16 description.appendText("is ").appendDescriptionOf(this.matcher);17 }18 @Override // org.hamcrest.BaseMatcher, org.hamcrest.Matcher19 public void describeMismatch(Object item, Description mismatchDescription) {20 this.matcher.describeMismatch(item, mismatchDescription);21 }22 public static <T> Matcher<T> is(Matcher<T> matcher2) {23 return new Is(matcher2);24 }25 public static <T> Matcher<T> is(T value) {26 return is(IsEqual.equalTo(value));27 }28 public static <T> Matcher<T> isA(Class<T> type) {29 return is((Matcher) IsInstanceOf.instanceOf(type));30 }31}...

Full Screen

Full Screen

describeMismatch

Using AI Code Generation

copy

Full Screen

1org.hamcrest.BaseMatcher.describeMismatch(Object item, Description description)2org.hamcrest.BaseMatcher.describeTo(Description description)3org.hamcrest.BaseMatcher.matches(Object item)4org.hamcrest.BaseMatcher.toString()5org.hamcrest.BaseMatcher.wrapMatcher(org.hamcrest.Matcher matcher)6org.hamcrest.BaseMatcher.wrapMatcher(java.lang.String description, org.hamcrest.Matcher matcher)7org.hamcrest.BaseMatcher.wrapMatcher(java.lang.String description, org.hamcrest.SelfDescribing value, org.hamcrest.Matcher matcher)8org.hamcrest.BaseMatcher.wrapMismatchDescription(java.lang.String description, org.hamcrest.SelfDescribing value, org.hamcrest.SelfDescribing mismatchDescription)9org.hamcrest.BaseMatcher.wrapValueDescription(java.lang.String description, org.hamcrest.SelfDescribing value)10org.hamcrest.BaseMatcher.wrapValueWithIsEqualTo(java.lang.Object value)11org.hamcrest.BaseMatcher.wrapValueWithIsSame(java.lang.Object value)12org.hamcrest.BaseMatcher.wrapWithIsEqual(java.lang.Object value)13org.hamcrest.BaseMatcher.wrapWithIsSame(java.lang.Object value)14org.hamcrest.BaseMatcher.wrapWithIs(java.lang.String description, org.hamcrest.SelfDescribing value)15org.hamcrest.BaseMatcher.wrapWithIs(java.lang.String description, org.hamcrest.SelfDescribing value, org.hamcrest.Matcher matcher)16org.hamcrest.BaseMatcher.wrapWithIsNot(java.lang.String description, org.hamcrest.SelfDescribing value, org.hamcrest.Matcher matcher)17org.hamcrest.BaseMatcher.wrapWithNot(org.hamcrest.Matcher matcher)

Full Screen

Full Screen

describeMismatch

Using AI Code Generation

copy

Full Screen

1package com.zetcode;2import org.hamcrest.BaseMatcher;3import org.hamcrest.Description;4import org.hamcrest.Matcher;5public class JavaMatcher extends BaseMatcher {6 private final int expected;7 public JavaMatcher(int expected) {8 this.expected = expected;9 }10 public boolean matches(Object actual) {11 return actual.equals(expected);12 }13 public void describeTo(Description desc) {14 desc.appendText(String.valueOf(expected));15 }16 public void describeMismatch(Object actual, Description desc) {17 desc.appendText("was ").appendText(String.valueOf(actual));18 }19 public static Matcher<Integer> isJava(int expected) {20 return new JavaMatcher(expected);21 }22}23package com.zetcode;24import org.junit.Test;25import static org.junit.Assert.*;26public class JavaMatcherTest {27 public void testJavaMatcher() {28 int actual = 8;29 int expected = 8;30 assertThat(actual, JavaMatcher.isJava(expected));31 }32}

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