How to use matchesSafely method of org.hamcrest.TypeSafeMatcher class

Best junit code snippet using org.hamcrest.TypeSafeMatcher.matchesSafely

Source:ExpressiveObjectsMatchers.java Github

copy

Full Screen

...44 public static Matcher<String> containsStripNewLines(final String expected) {45 final String strippedExpected = StringUtils.stripNewLines(expected);46 return new StringContains(strippedExpected) {47 @Override48 public boolean matchesSafely(final String actual) {49 return super.matchesSafely(StringUtils.stripNewLines(actual));50 }51 @Override52 public void describeTo(final Description description) {53 description.appendText("a string (ignoring new lines) containing").appendValue(strippedExpected);54 }55 };56 }57 @Factory58 public static Matcher<String> equalToStripNewLines(final String expected) {59 final String strippedExpected = StringUtils.stripNewLines(expected);60 return new IsEqual<String>(strippedExpected) {61 @Override62 public boolean matches(final Object actualObj) {63 final String actual = (String) actualObj;64 return super.matches(StringUtils.stripNewLines(actual));65 }66 @Override67 public void describeTo(final Description description) {68 description.appendText("a string (ignoring new lines) equal to").appendValue(strippedExpected);69 }70 };71 }72 @Factory73 public static StringStartsWith startsWithStripNewLines(final String expected) {74 final String strippedExpected = StringUtils.stripNewLines(expected);75 return new StringStartsWith(strippedExpected) {76 @Override77 public boolean matchesSafely(final String actual) {78 return super.matchesSafely(StringUtils.stripNewLines(actual));79 }80 @Override81 public void describeTo(final Description description) {82 description.appendText("a string (ignoring new lines) starting with").appendValue(strippedExpected);83 }84 };85 }86 @Factory87 public static Matcher<String> endsWithStripNewLines(final String expected) {88 final String strippedExpected = StringUtils.stripNewLines(expected);89 return new StringEndsWith(strippedExpected) {90 @Override91 public boolean matchesSafely(final String actual) {92 return super.matchesSafely(StringUtils.stripNewLines(actual));93 }94 @Override95 public void describeTo(final Description description) {96 description.appendText("a string (ignoring new lines) ending with").appendValue(strippedExpected);97 }98 };99 }100 @Factory101 public static <T> Matcher<T> anInstanceOf(final Class<T> expected) {102 return new TypeSafeMatcher<T>() {103 @Override104 public boolean matchesSafely(final T actual) {105 return expected.isAssignableFrom(actual.getClass());106 }107 @Override108 public void describeTo(final Description description) {109 description.appendText("an instance of ").appendValue(expected);110 }111 };112 }113 @Factory114 public static Matcher<String> nonEmptyString() {115 return new TypeSafeMatcher<String>() {116 @Override117 public boolean matchesSafely(final String str) {118 return str != null && str.length() > 0;119 }120 @Override121 public void describeTo(final Description description) {122 description.appendText("a non empty string");123 }124 };125 }126 @Factory127 @SuppressWarnings("unchecked")128 public static Matcher<String> nonEmptyStringOrNull() {129 return CoreMatchers.anyOf(nullValue(String.class), nonEmptyString());130 }131 @Factory132 public static Matcher<List<?>> containsElementThat(final Matcher<?> elementMatcher) {133 return new TypeSafeMatcher<List<?>>() {134 @Override135 public boolean matchesSafely(final List<?> list) {136 for (final Object o : list) {137 if (elementMatcher.matches(o)) {138 return true;139 }140 }141 return false;142 }143 @Override144 public void describeTo(final Description description) {145 description.appendText("contains element that ").appendDescriptionOf(elementMatcher);146 }147 };148 }149 @Factory150 public static <T extends Comparable<T>> Matcher<T> greaterThan(final T c) {151 return Matchers.greaterThan(c);152 }153 @Factory154 public static Matcher<Class<?>> classEqualTo(final Class<?> operand) {155 class ClassEqualsMatcher extends TypeSafeMatcher<Class<?>> {156 private final Class<?> clazz;157 public ClassEqualsMatcher(final Class<?> clazz) {158 this.clazz = clazz;159 }160 @Override161 public boolean matchesSafely(final Class<?> arg) {162 return clazz == arg;163 }164 @Override165 public void describeTo(final Description description) {166 description.appendValue(clazz);167 }168 }169 return new ClassEqualsMatcher(operand);170 }171 @Factory172 public static Matcher<File> existsAndNotEmpty() {173 return new TypeSafeMatcher<File>() {174 @Override175 public void describeTo(final Description arg0) {176 arg0.appendText("exists and is not empty");177 }178 @Override179 public boolean matchesSafely(final File f) {180 return f.exists() && f.length() > 0;181 }182 };183 }184 @Factory185 public static Matcher<String> matches(final String regex) {186 return new TypeSafeMatcher<String>() {187 @Override188 public void describeTo(final Description description) {189 description.appendText("string matching " + regex);190 }191 @Override192 public boolean matchesSafely(final String str) {193 return str.matches(regex);194 }195 };196 }197 @Factory198 public static <X> Matcher<Class<X>> anySubclassOf(final Class<X> cls) {199 return new TypeSafeMatcher<Class<X>>() {200 @Override201 public void describeTo(final Description arg0) {202 arg0.appendText("is subclass of ").appendText(cls.getName());203 }204 @Override205 public boolean matchesSafely(final Class<X> item) {206 return cls.isAssignableFrom(item);207 }208 };209 }210 @Factory211 public static <T> Matcher<List<T>> sameContentsAs(final List<T> expected) {212 return new TypeSafeMatcher<List<T>>() {213 @Override214 public void describeTo(final Description description) {215 description.appendText("same sequence as " + expected);216 }217 @Override218 public boolean matchesSafely(final List<T> actual) {219 return actual.containsAll(expected) && expected.containsAll(actual);220 }221 };222 }223 @Factory224 public static <T> Matcher<List<T>> listContaining(final T t) {225 return new TypeSafeMatcher<List<T>>() {226 227 @Override228 public void describeTo(Description arg0) {229 arg0.appendText("list containing ").appendValue(t);230 }231 232 @Override233 public boolean matchesSafely(List<T> arg0) {234 return arg0.contains(t);235 }236 };237 }238 @Factory239 public static <T> Matcher<List<T>> listContainingAll(final T... items) {240 return new TypeSafeMatcher<List<T>>() {241 @Override242 public void describeTo(Description arg0) {243 arg0.appendText("has items ").appendValue(items);244 245 }246 @Override247 public boolean matchesSafely(List<T> arg0) {248 return arg0.containsAll(Arrays.asList(items));249 }250 };251 }252 @Factory253 public static Matcher<List<Object>> containsObjectOfType(final Class<?> cls) {254 return new TypeSafeMatcher<List<Object>>() {255 @Override256 public void describeTo(final Description desc) {257 desc.appendText("contains instance of type " + cls.getName());258 }259 @Override260 public boolean matchesSafely(final List<Object> items) {261 for (final Object object : items) {262 if (cls.isAssignableFrom(object.getClass())) {263 return true;264 }265 }266 return false;267 }268 };269 }270 @Factory271 public static Matcher<String> startsWith(final String expected) {272 return new TypeSafeMatcher<String>() {273 @Override274 public void describeTo(Description description) {275 description.appendText(" starts with '" + expected + "'");276 }277 @Override278 public boolean matchesSafely(String actual) {279 return actual.startsWith(expected);280 }281 };282 }283 @Factory284 public static Matcher<String> contains(final String expected) {285 return new TypeSafeMatcher<String>() {286 @Override287 public void describeTo(Description description) {288 description.appendText(" contains '" + expected + "'");289 }290 @Override291 public boolean matchesSafely(String actual) {292 return actual.contains(expected);293 }294 };295 }296 297 @Factory298 public static Matcher<File> equalsFile(final File file) throws IOException {299 final String canonicalPath = file.getCanonicalPath();300 return new TypeSafeMatcher<File>() {301 @Override302 public void describeTo(Description arg0) {303 arg0.appendText("file '" + canonicalPath + "'");304 }305 @Override306 public boolean matchesSafely(File arg0) {307 try {308 return arg0.getCanonicalPath().equals(canonicalPath);309 } catch (IOException e) {310 return false;311 }312 }313 };314 }315}...

Full Screen

Full Screen

Source:IsisMatchers.java Github

copy

Full Screen

...26 public static Matcher<String> containsStripNewLines(final String expected) {27 final String strippedExpected = StringExtensions.stripNewLines(expected);28 return new StringContains(strippedExpected) {29 @Override30 public boolean matchesSafely(final String actual) {31 return super.matchesSafely(StringExtensions.stripNewLines(actual));32 }33 @Override34 public void describeTo(final Description description) {35 description.appendText("a string (ignoring new lines) containing").appendValue(strippedExpected);36 }37 };38 }39 @Factory40 public static Matcher<String> equalToStripNewLines(final String expected) {41 final String strippedExpected = StringExtensions.stripNewLines(expected);42 return new IsEqual<String>(strippedExpected) {43 @Override44 public boolean matches(final Object actualObj) {45 final String actual = (String) actualObj;46 return super.matches(StringExtensions.stripNewLines(actual));47 }48 @Override49 public void describeTo(final Description description) {50 description.appendText("a string (ignoring new lines) equal to").appendValue(strippedExpected);51 }52 };53 }54 @Factory55 public static StringStartsWith startsWithStripNewLines(final String expected) {56 final String strippedExpected = StringExtensions.stripNewLines(expected);57 return new StringStartsWith(strippedExpected) {58 @Override59 public boolean matchesSafely(final String actual) {60 return super.matchesSafely(StringExtensions.stripNewLines(actual));61 }62 @Override63 public void describeTo(final Description description) {64 description.appendText("a string (ignoring new lines) starting with").appendValue(strippedExpected);65 }66 };67 }68 @Factory69 public static Matcher<String> endsWithStripNewLines(final String expected) {70 final String strippedExpected = StringExtensions.stripNewLines(expected);71 return new StringEndsWith(strippedExpected) {72 @Override73 public boolean matchesSafely(final String actual) {74 return super.matchesSafely(StringExtensions.stripNewLines(actual));75 }76 @Override77 public void describeTo(final Description description) {78 description.appendText("a string (ignoring new lines) ending with").appendValue(strippedExpected);79 }80 };81 }82 @Factory83 public static <T> Matcher<T> anInstanceOf(final Class<T> expected) {84 return new TypeSafeMatcher<T>() {85 @Override86 public boolean matchesSafely(final T actual) {87 return expected.isAssignableFrom(actual.getClass());88 }89 @Override90 public void describeTo(final Description description) {91 description.appendText("an instance of ").appendValue(expected);92 }93 };94 }95 @Factory96 public static Matcher<String> nonEmptyString() {97 return new TypeSafeMatcher<String>() {98 @Override99 public boolean matchesSafely(final String str) {100 return str != null && str.length() > 0;101 }102 @Override103 public void describeTo(final Description description) {104 description.appendText("a non empty string");105 }106 };107 }108 @Factory109 @SuppressWarnings("unchecked")110 public static Matcher<String> nonEmptyStringOrNull() {111 return CoreMatchers.anyOf(nullValue(String.class), nonEmptyString());112 }113 @Factory114 public static Matcher<List<?>> containsElementThat(final Matcher<?> elementMatcher) {115 return new TypeSafeMatcher<List<?>>() {116 @Override117 public boolean matchesSafely(final List<?> list) {118 for (final Object o : list) {119 if (elementMatcher.matches(o)) {120 return true;121 }122 }123 return false;124 }125 @Override126 public void describeTo(final Description description) {127 description.appendText("contains element that ").appendDescriptionOf(elementMatcher);128 }129 };130 }131 @Factory132 public static <T extends Comparable<T>> Matcher<T> greaterThan(final T c) {133 return Matchers.greaterThan(c);134 }135 @Factory136 public static Matcher<Class<?>> classEqualTo(final Class<?> operand) {137 class ClassEqualsMatcher extends TypeSafeMatcher<Class<?>> {138 private final Class<?> clazz;139 public ClassEqualsMatcher(final Class<?> clazz) {140 this.clazz = clazz;141 }142 @Override143 public boolean matchesSafely(final Class<?> arg) {144 return clazz == arg;145 }146 @Override147 public void describeTo(final Description description) {148 description.appendValue(clazz);149 }150 }151 return new ClassEqualsMatcher(operand);152 }153 @Factory154 public static Matcher<File> existsAndNotEmpty() {155 return new TypeSafeMatcher<File>() {156 @Override157 public void describeTo(final Description arg0) {158 arg0.appendText("exists and is not empty");159 }160 @Override161 public boolean matchesSafely(final File f) {162 return f.exists() && f.length() > 0;163 }164 };165 }166 @Factory167 public static Matcher<String> matches(final String regex) {168 return new TypeSafeMatcher<String>() {169 @Override170 public void describeTo(final Description description) {171 description.appendText("string matching " + regex);172 }173 @Override174 public boolean matchesSafely(final String str) {175 return str.matches(regex);176 }177 };178 }179 @Factory180 public static <X> Matcher<Class<X>> anySubclassOf(final Class<X> cls) {181 return new TypeSafeMatcher<Class<X>>() {182 @Override183 public void describeTo(final Description arg0) {184 arg0.appendText("is subclass of ").appendText(cls.getName());185 }186 @Override187 public boolean matchesSafely(final Class<X> item) {188 return cls.isAssignableFrom(item);189 }190 };191 }192 @Factory193 public static <T> Matcher<List<T>> sameContentsAs(final List<T> expected) {194 return new TypeSafeMatcher<List<T>>() {195 @Override196 public void describeTo(final Description description) {197 description.appendText("same sequence as " + expected);198 }199 @Override200 public boolean matchesSafely(final List<T> actual) {201 return actual.containsAll(expected) && expected.containsAll(actual);202 }203 };204 }205 @Factory206 public static <T> Matcher<List<T>> listContaining(final T t) {207 return new TypeSafeMatcher<List<T>>() {208 209 @Override210 public void describeTo(Description arg0) {211 arg0.appendText("list containing ").appendValue(t);212 }213 214 @Override215 public boolean matchesSafely(List<T> arg0) {216 return arg0.contains(t);217 }218 };219 }220 @Factory221 public static <T> Matcher<List<T>> listContainingAll(final T... items) {222 return new TypeSafeMatcher<List<T>>() {223 @Override224 public void describeTo(Description arg0) {225 arg0.appendText("has items ").appendValue(items);226 227 }228 @Override229 public boolean matchesSafely(List<T> arg0) {230 return arg0.containsAll(Arrays.asList(items));231 }232 };233 }234 @Factory235 public static Matcher<List<Object>> containsObjectOfType(final Class<?> cls) {236 return new TypeSafeMatcher<List<Object>>() {237 @Override238 public void describeTo(final Description desc) {239 desc.appendText("contains instance of type " + cls.getName());240 }241 @Override242 public boolean matchesSafely(final List<Object> items) {243 for (final Object object : items) {244 if (cls.isAssignableFrom(object.getClass())) {245 return true;246 }247 }248 return false;249 }250 };251 }252 @Factory253 public static Matcher<String> startsWith(final String expected) {254 return new TypeSafeMatcher<String>() {255 @Override256 public void describeTo(Description description) {257 description.appendText(" starts with '" + expected + "'");258 }259 @Override260 public boolean matchesSafely(String actual) {261 return actual.startsWith(expected);262 }263 };264 }265 @Factory266 public static Matcher<String> contains(final String expected) {267 return new TypeSafeMatcher<String>() {268 @Override269 public void describeTo(Description description) {270 description.appendText(" contains '" + expected + "'");271 }272 @Override273 public boolean matchesSafely(String actual) {274 return actual.contains(expected);275 }276 };277 }278 279 @Factory280 public static Matcher<File> equalsFile(final File file) throws IOException {281 final String canonicalPath = file.getCanonicalPath();282 return new TypeSafeMatcher<File>() {283 @Override284 public void describeTo(Description arg0) {285 arg0.appendText("file '" + canonicalPath + "'");286 }287 @Override288 public boolean matchesSafely(File arg0) {289 try {290 return arg0.getCanonicalPath().equals(canonicalPath);291 } catch (IOException e) {292 return false;293 }294 }295 };296 }297}...

Full Screen

Full Screen

Source:MatcherUtils.java Github

copy

Full Screen

...17 public void describeTo(Description description) {18 description.appendText("throws " + type.getSimpleName());19 }20 @Override21 protected boolean matchesSafely(Runnable function) {22 try {23 function.run();24 return false;25 } catch (Throwable throwable) {26 return type.isAssignableFrom(throwable.getClass());27 }28 }29 };30 }31 /**32 * <pre>33 * URL url = new URL("http://api.com/user/42");34 * assertThat(url, with(URL::toString, containsString("user/42")));35 * </pre>36 */37 public static <MAIN, SUB> Matcher<MAIN> with(Function<MAIN, SUB> mapper, Matcher<? super SUB> matcher) {38 return new TypeSafeMatcher<MAIN>() {39 private SUB actual;40 @Override41 public void describeTo(Description description) {42 description.appendValue(actual).appendDescriptionOf(matcher);43 }44 @Override45 protected boolean matchesSafely(MAIN mainValue) {46 actual = mapper.apply(mainValue);47 return matcher.matches(actual);48 }49 };50 }51 /**52 * Should be called only once by assertion,53 * because {@link MockWebServer#takeRequest()} pop 1 request by call.54 */55 public static Matcher<MockWebServer> takingRequest(Matcher<RecordedRequest> matcher) {56 return new TypeSafeMatcher<>() {57 @Override58 public void describeTo(Description description) {59 description.appendDescriptionOf(matcher);60 }61 @Override62 protected boolean matchesSafely(MockWebServer server) {63 try {64 RecordedRequest request = server.takeRequest();65 return matcher.matches(request);66 } catch (InterruptedException e) { // avoid "server" blocking when assertion fails.67 throw new RuntimeException(e);68 }69 }70 };71 }72 public static Matcher<RecordedRequest> header(String key, String value) {73 return header(key, is(value));74 }75 public static Matcher<RecordedRequest> header(String key, Matcher<? super String> matcher) {76 return new TypeSafeMatcher<>() {77 @Override78 public void describeTo(Description description) {79 description.appendText("headers with key " + key + " ").appendDescriptionOf(matcher);80 }81 @Override82 protected boolean matchesSafely(RecordedRequest request) {83 String actual = request.getHeader(key);84 return matcher.matches(actual);85 }86 };87 }88 @Deprecated(forRemoval = true)89 public static Matcher<RecordedRequest> url(Matcher<? super HttpUrl> matcher) {90 return new TypeSafeMatcher<>() {91 private HttpUrl actual;92 @Override93 public void describeTo(Description description) {94 description.appendValue(actual).appendDescriptionOf(matcher);95 }96 @Override97 protected boolean matchesSafely(RecordedRequest request) {98 actual = request.getRequestUrl();99 return matcher.matches(actual);100 }101 };102 }103 public static Matcher<RecordedRequest> uri(Matcher<? super URI> matcher) {104 return new TypeSafeMatcher<>() {105 private URI actual;106 @Override107 public void describeTo(Description description) {108 description.appendValue(actual).appendDescriptionOf(matcher);109 }110 @Override111 protected boolean matchesSafely(RecordedRequest request) {112 actual = request.getRequestUrl().uri();113 return matcher.matches(actual);114 }115 };116 }117 public static Matcher<URI> path(Matcher<String> matcher) {118 return new TypeSafeMatcher<>() {119 private String actual;120 @Override121 public void describeTo(Description description) {122 description.appendValue(actual).appendDescriptionOf(matcher);123 }124 @Override125 protected boolean matchesSafely(URI uri) {126 actual = uri.getPath();127 return matcher.matches(actual);128 }129 };130 }131 public static Matcher<URI> query(String key, Matcher<? super String> matcher) {132 return new TypeSafeMatcher<>() {133 private String actual;134 @Override135 public void describeTo(Description description) {136 description.appendValue(actual).appendDescriptionOf(matcher);137 }138 @Override139 protected boolean matchesSafely(URI uri) {140 actual = UriComponentsBuilder.fromUri(uri).build().getQueryParams().getFirst(key);141 return matcher.matches(actual);142 }143 };144 }145 @Deprecated(forRemoval = true)146 public static Matcher<RecordedRequest> method(Matcher<String> matcher) {147 return new TypeSafeMatcher<>() {148 private String actual;149 @Override150 public void describeTo(Description description) {151 description.appendValue(actual).appendDescriptionOf(matcher);152 }153 @Override154 protected boolean matchesSafely(RecordedRequest request) {155 actual = request.getMethod();156 return matcher.matches(actual);157 }158 };159 }160 public static Matcher<RecordedRequest> methodSpring(Matcher<HttpMethod> matcher) {161 return new TypeSafeMatcher<>() {162 private HttpMethod actual;163 @Override164 public void describeTo(Description description) {165 description.appendValue(actual).appendDescriptionOf(matcher);166 }167 @Override168 protected boolean matchesSafely(RecordedRequest request) {169 actual = HttpMethod.resolve(request.getMethod());170 return matcher.matches(actual);171 }172 };173 }174 public static Matcher<RecordedRequest> body(Matcher<? super String> matcher) {175 return new TypeSafeMatcher<>() {176 private String actual;177 @Override178 public void describeTo(Description description) {179 description.appendValue(actual).appendDescriptionOf(matcher);180 }181 @Override182 protected boolean matchesSafely(RecordedRequest request) {183 actual = request.getBody().readUtf8();184 return matcher.matches(actual);185 }186 };187 }188}...

Full Screen

Full Screen

Source:CommonMatchers.java Github

copy

Full Screen

...34public class CommonMatchers {35 public static Matcher<String> endsWith(final String suffix) {36 return new TypeSafeMatcher<String>() {37 @Override38 public boolean matchesSafely(String s) {39 return s.endsWith(suffix);40 }41 @Override42 public void describeTo(Description description) {43 description.appendText("a string ending with " + suffix);44 }45 };46 }47 public static Matcher<String> startsWith(final String prefix) {48 return new TypeSafeMatcher<String>() {49 @Override50 public boolean matchesSafely(String s) {51 return s.startsWith(prefix);52 }53 @Override54 public void describeTo(Description description) {55 description.appendText("a string starting with " + prefix);56 }57 };58 }59 public static Matcher<String> anEmptyString() {60 return new TypeSafeMatcher<String>() {61 @Override62 public boolean matchesSafely(String s) {63 return s.isEmpty();64 }65 @Override66 public void describeTo(Description description) {67 description.appendText("an empty string");68 }69 };70 }71 public static Matcher<Object[]> anEmptyArray() {72 return new TypeSafeMatcher<Object[]>() {73 @Override74 public boolean matchesSafely(Object[] objects) {75 return objects != null && objects.length == 0;76 }77 @Override78 public void describeTo(Description description) {79 description.appendText("an empty array");80 }81 };82 }83 public static Matcher<Collection> anEmptyCollection() {84 return new TypeSafeMatcher<Collection>() {85 @Override86 public boolean matchesSafely(Collection collection) {87 return collection.isEmpty();88 }89 @Override90 public void describeTo(Description description) {91 description.appendText("an empty collection");92 }93 };94 }95 public static Matcher<Map> anEmptyMap() {96 return new TypeSafeMatcher<Map>() {97 @Override98 public boolean matchesSafely(Map collection) {99 return collection.isEmpty();100 }101 @Override102 public void describeTo(Description description) {103 description.appendText("an empty map");104 }105 };106 }107 public static Matcher<Collection> aCollectionWithSize(final int size) {108 return new TypeSafeMatcher<Collection>() {109 @Override110 public boolean matchesSafely(Collection collection) {111 return collection != null && collection.size() == size;112 }113 @Override114 public void describeTo(Description description) {115 description.appendText("a collection with " + size + " elements");116 }117 };118 }119 public static Matcher<Node> compliantWithSchema(final String schemaPath) {120 return new TypeSafeMatcher<Node>() {121 @Override122 public boolean matchesSafely(Node node) {123 URL schemaURL = CommonMatchers.class.getResource(schemaPath);124 if (schemaURL == null) {125 throw new IllegalArgumentException("No schema found at " + schemaPath);126 }127 SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);128 Schema schema;129 try {130 schema = sf.newSchema(schemaURL);131 } catch (SAXException e) {132 throw new IllegalArgumentException("The file at " + schemaURL + " does not contain a valid XML schema", e);133 }134 try {135 schema.newValidator().validate(new DOMSource(node));136 return true;137 } catch (SAXException e) {138 return false;139 } catch (Exception e) {140 throw new RuntimeException("Unexpected exception", e);141 }142 }143 @Override144 public void describeTo(Description description) {145 description.appendText("an XML node compliant with the XML schema at " + schemaPath);146 }147 };148 }149 public static Matcher<ContentType> sameBaseContentType(final String contentType) {150 return new TypeSafeMatcher<ContentType>() {151 @Override152 public boolean matchesSafely(ContentType item) {153 return contentType.equals(item.getBaseType());154 }155 @Override156 public void describeTo(Description description) {157 description.appendText("the content type " + contentType);158 }159 };160 }161 public static Matcher<File> exists() {162 return new TypeSafeMatcher<File>() {163 @Override164 public boolean matchesSafely(File file) {165 return file.exists();166 }167 @Override168 public void describeTo(Description description) {169 description.appendText("an existing file");170 }171 };172 }173 public static Matcher<Object> aNumber() {174 return new org.hamcrest.TypeSafeMatcher<Object>() {175 @Override176 protected boolean matchesSafely(Object o) {177 return o instanceof Number;178 }179 @Override180 public void describeTo(Description description) {181 description.appendText("a number");182 }183 };184 }185}...

Full Screen

Full Screen

Source:FileMatchers.java Github

copy

Full Screen

...26public class FileMatchers {27 public static Matcher<File> isDirectory() {28 return new TypeSafeMatcher<File>() {29 File fileTested;30 public boolean matchesSafely(File item) {31 fileTested = item;32 return item.isDirectory();33 }34 public void describeTo(Description description) {35 description.appendText(" that ");36 description.appendValue(fileTested);37 description.appendText("is a directory");38 }39 };40 }41 public static Matcher<File> exists() {42 return new TypeSafeMatcher<File>() {43 File fileTested;44 public boolean matchesSafely(File item) {45 fileTested = item;46 return item.exists();47 }48 public void describeTo(Description description) {49 description.appendText(" that file ");50 description.appendValue(fileTested);51 description.appendText(" exists");52 }53 };54 }55 public static Matcher<File> isFile() {56 return new TypeSafeMatcher<File>() {57 File fileTested;58 public boolean matchesSafely(File item) {59 fileTested = item;60 return item.isFile();61 }62 public void describeTo(Description description) {63 description.appendText(" that ");64 description.appendValue(fileTested);65 description.appendText("is a file");66 }67 };68 }69 public static Matcher<File> readable() {70 return new TypeSafeMatcher<File>() {71 File fileTested;72 public boolean matchesSafely(File item) {73 fileTested = item;74 return item.canRead();75 }76 public void describeTo(Description description) {77 description.appendText(" that file ");78 description.appendValue(fileTested);79 description.appendText("is readable");80 }81 };82 }83 public static Matcher<File> writable() {84 return new TypeSafeMatcher<File>() {85 File fileTested;86 public boolean matchesSafely(File item) {87 fileTested = item;88 return item.canWrite();89 }90 public void describeTo(Description description) {91 description.appendText(" that file ");92 description.appendValue(fileTested);93 description.appendText("is writable");94 }95 };96 }97 public static Matcher<File> sized(long size) {98 return sized(Matchers.equalTo(size));99 }100 public static Matcher<File> sized(final Matcher<Long> size) {101 return new TypeSafeMatcher<File>() {102 File fileTested;103 long length;104 public boolean matchesSafely(File item) {105 fileTested = item;106 length = item.length();107 return size.matches(length);108 }109 public void describeTo(Description description) {110 description.appendText(" that file ");111 description.appendValue(fileTested);112 description.appendText(" is sized ");113 description.appendDescriptionOf(size);114 description.appendText(", not " + length);115 }116 };117 }118 public static Matcher<File> named(final Matcher<String> name) {119 return new TypeSafeMatcher<File>() {120 File fileTested;121 public boolean matchesSafely(File item) {122 fileTested = item;123 return name.matches(item.getName());124 }125 public void describeTo(Description description) {126 description.appendText(" that file ");127 description.appendValue(fileTested);128 description.appendText(" is named");129 description.appendDescriptionOf(name);130 description.appendText(" not ");131 description.appendValue(fileTested.getName());132 }133 };134 }135 public static Matcher<File> withCanonicalPath(final Matcher<String> path) {136 return new TypeSafeMatcher<File>() {137 public boolean matchesSafely(File item) {138 try {139 return path.matches(item.getCanonicalPath());140 }141 catch (IOException e) {142 return false;143 }144 }145 public void describeTo(Description description) {146 description.appendText("with canonical path '");147 description.appendDescriptionOf(path);148 description.appendText("'");149 }150 };151 }152 public static Matcher<File> withAbsolutePath(final Matcher<String> path) {153 return new TypeSafeMatcher<File>() {154 public boolean matchesSafely(File item) {155 return path.matches(item.getAbsolutePath());156 }157 public void describeTo(Description description) {158 description.appendText("with absolute path '");159 description.appendDescriptionOf(path);160 description.appendText("'");161 }162 };163 }164}...

Full Screen

Full Screen

Source:NofMatchers.java Github

copy

Full Screen

...29 public static Matcher<String> containsStripNewLines(final String expected) {30 final String strippedExpected = StringUtils.stripNewLines(expected);31 return new StringContains(strippedExpected) {32 @Override33 public boolean matchesSafely(final String actual) {34 return super.matchesSafely(StringUtils.stripNewLines(actual));35 }3637 @Override38 public void describeTo(final Description description) {39 description.appendText("a string (ignoring new lines) containing").appendValue(strippedExpected);40 }41 };42 }4344 @Factory45 public static Matcher<String> equalToStripNewLines(final String expected) {46 final String strippedExpected = StringUtils.stripNewLines(expected);47 return new IsEqual<String>(strippedExpected) {48 @Override49 public boolean matches(final Object actualObj) {50 final String actual = (String) actualObj;51 return super.matches(StringUtils.stripNewLines(actual));52 }5354 @Override55 public void describeTo(final Description description) {56 description.appendText("a string (ignoring new lines) equal to").appendValue(strippedExpected);57 }58 };59 }6061 @Factory62 public static Matcher<String> startsWithStripNewLines(final String expected) {63 final String strippedExpected = StringUtils.stripNewLines(expected);64 return new StringStartsWith(strippedExpected) {65 @Override66 public boolean matchesSafely(final String actual) {67 return super.matchesSafely(StringUtils.stripNewLines(actual));68 }6970 @Override71 public void describeTo(final Description description) {72 description.appendText("a string (ignoring new lines) starting with").appendValue(strippedExpected);73 }74 };75 }7677 @Factory78 public static Matcher<String> endsWithStripNewLines(final String expected) {79 final String strippedExpected = StringUtils.stripNewLines(expected);80 return new StringEndsWith(strippedExpected) {81 @Override82 public boolean matchesSafely(final String actual) {83 return super.matchesSafely(StringUtils.stripNewLines(actual));84 }8586 @Override87 public void describeTo(final Description description) {88 description.appendText("a string (ignoring new lines) ending with").appendValue(strippedExpected);89 }90 };91 }9293 public static <T> Matcher<T> anInstanceOf(final Class<T> expected) {94 return new TypeSafeMatcher<T>() {95 @Override96 public boolean matchesSafely(final T actual) {97 return expected.isAssignableFrom(actual.getClass());98 }99100 public void describeTo(final Description description) {101 description.appendText("an instance of ").appendValue(expected);102 }103 };104 }105106 @Factory107 public static Matcher<String> nonEmptyString() {108 return new TypeSafeMatcher<String>() {109 @Override110 public boolean matchesSafely(String str) {111 return str != null && str.length() > 0;112 }113114 public void describeTo(Description description) {115 description.appendText("a non empty string");116 }117 118 };119 }120121 @Factory122 @SuppressWarnings("unchecked")123 public static Matcher<String> nonEmptyStringOrNull() {124 return CoreMatchers.anyOf(nullValue(String.class), nonEmptyString());125 }126127 128 public static Matcher<List<?>> containsElementThat(final Matcher<?> elementMatcher) {129 return new TypeSafeMatcher<List<?>>() {130 public boolean matchesSafely(List<?> list) {131 for(Object o: list) {132 if (elementMatcher.matches(o)) {133 return true;134 }135 }136 return false;137 }138139 public void describeTo(Description description) {140 description.appendText("contains element that ").appendDescriptionOf(elementMatcher);141 }142 };143 }144145146 @Factory147 public static <T extends Comparable<T>> Matcher<T> greaterThan(T c) {148 return new IsGreaterThan<T>(c);149 }150151152 @Factory153 public static Matcher<Class<?>> classEqualTo(final Class<?> operand) {154155 class ClassEqualsMatcher extends TypeSafeMatcher<Class<?>> {156 private final Class<?> clazz;157 public ClassEqualsMatcher(final Class<?> clazz) {158 this.clazz = clazz;159 }160161 @Override162 public boolean matchesSafely(final Class<?> arg) {163 return clazz == arg;164 }165166 public void describeTo(final Description description) {167 description.appendValue(clazz);168 }169 }170171 return new ClassEqualsMatcher(operand);172 }173 174} ...

Full Screen

Full Screen

Source:OwnersMatchers.java Github

copy

Full Screen

...17 }18 public static Matcher<Page> forOwnerName(final String ownerName) {19 return new TypeSafeMatcher<Page>() {20 @Override21 protected boolean matchesSafely(Page item) {22 return item instanceof OwnerPage &&23 ((OwnerPage)item).getName().equals(ownerName);24 }25 @Override26 public void describeTo(Description description) {27 description.appendText("owner name ").appendValue(ownerName);28 }29 };30 }31 public static Matcher<Page> withAddress(final String address) {32 return new TypeSafeMatcher<Page>() {33 @Override34 protected boolean matchesSafely(Page item) {35 return item instanceof OwnerPage &&36 ((OwnerPage)item).getAddress().equals(address);37 }38 @Override39 public void describeTo(Description description) {40 description.appendText("owner address ").appendValue(address);41 }42 };43 }44 public static Matcher<Page> withCity(final String city) {45 return new TypeSafeMatcher<Page>() {46 @Override47 protected boolean matchesSafely(Page item) {48 return item instanceof OwnerPage &&49 ((OwnerPage)item).getCity().equals(city);50 }51 @Override52 public void describeTo(Description description) {53 description.appendText("owner city ").appendValue(city);54 }55 };56 }57 public static Matcher<Page> withTelephone(final String telephone) {58 return new TypeSafeMatcher<Page>() {59 @Override60 protected boolean matchesSafely(Page item) {61 return item instanceof OwnerPage &&62 ((OwnerPage)item).getTelephone().equals(telephone);63 }64 @Override65 public void describeTo(Description description) {66 description.appendText("owner telephone ").appendValue(telephone);67 }68 };69 }70 public static Matcher<Page> theFindOwnersPage() {71 return pageNameMatcher("Find Owners");72 }73 public static Matcher<Page> theAddOwnerPage() {74 return pageNameMatcher("Add Owner");75 }76 public static TypeSafeMatcher<Page> pageNameMatcher(final String name) {77 return new TypeSafeMatcher<Page>() {78 @Override79 protected boolean matchesSafely(Page item) {80 return item.getInternalPageName().equals(name);81 }82 @Override83 public void describeTo(Description description) {84 description.appendText("page with name of ").appendValue(name);85 }86 };87 }88}...

Full Screen

Full Screen

Source:CustomMatchers.java Github

copy

Full Screen

...9 return new TypeSafeMatcher<T>() {10 public void describeTo(Description desc) {11 desc.appendText(String.format("Attribute of type %s named '%s'", type, name));12 }13 public boolean matchesSafely(T attribute) {14 return attribute.getName().toString().equals(name);15 }16 17 };18 }19 public static Matcher<WithMethod> named(final String name) {20 return new TypeSafeMatcher<WithMethod>() {21 public void describeTo(Description desc) {22 desc.appendText("with method named " + name);23 24 }25 public boolean matchesSafely(WithMethod withMethod) {26 return withMethod.getName().equals(name);27 }28 29 };30 }31 32 public static Matcher<WithMethod> returnType(final String returnType) {33 return new TypeSafeMatcher<WithMethod>() {34 public void describeTo(Description desc) {35 desc.appendText("with method with return type " + returnType);36 37 }38 public boolean matchesSafely(WithMethod withMethod) {39 return withMethod.getReturnType().equals(returnType);40 }41 42 };43 }44 45 public static Matcher<WithMethod> argumentType(final String argumentType) {46 return new TypeSafeMatcher<WithMethod>() {47 public void describeTo(Description desc) {48 desc.appendText("with argument type " + argumentType);49 50 }51 public boolean matchesSafely(WithMethod withMethod) {52 return withMethod.getArgumentType().equals(argumentType);53 }54 55 };56 }57 58 public static Matcher<WithMethod> argumentName(final String argumentName) {59 return new TypeSafeMatcher<WithMethod>() {60 public void describeTo(Description desc) {61 desc.appendText("with argument name " + argumentName);62 63 }64 public boolean matchesSafely(WithMethod withMethod) {65 return withMethod.getArgumentName().equals(argumentName);66 }67 68 };69 }70}...

Full Screen

Full Screen

matchesSafely

Using AI Code Generation

copy

Full Screen

1package com.example.android.testing.espresso.BasicSample;2import android.support.test.espresso.Espresso;3import android.support.test.espresso.ViewInteraction;4import android.support.test.espresso.matcher.ViewMatchers;5import android.support.test.rule.ActivityTestRule;6import android.support.test.runner.AndroidJUnit4;7import android.view.View;8import android.widget.TextView;9import org.hamcrest.Description;10import org.hamcrest.Matcher;11import org.junit.Rule;12import org.junit.Test;13import org.junit.runner.RunWith;14import static android.support.test.espresso.action.ViewActions.click;15import static android.support.test.espresso.action.ViewActions.typeText;16import static android.support.test.espresso.assertion.ViewAssertions.matches;17import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;18import static android.support.test.espresso.matcher.ViewMatchers.withId;19import static org.hamcrest.Matchers.is;20@RunWith(AndroidJUnit4.class)21public class ChangeTextBehaviorTest {22 private static final String STRING_TO_BE_TYPED = "Espresso";23 public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(24 MainActivity.class);25 public void changeText_sameActivity() {26 Espresso.onView(withId(R.id.editTextUserInput))27 .perform(typeText(STRING_TO_BE_TYPED), click());28 Espresso.onView(withId(R.id.show_text_view))29 .check(matches(withText(STRING_TO_BE_TYPED)));30 }31 public void changeText_newActivity() {32 Espresso.onView(withId(R.id.editTextUserInput))33 .perform(typeText(STRING_TO_BE_TYPED), click());34 Espresso.onView(withId(R.id.textToBeChanged))35 .check(matches(withText(STRING_TO_BE_TYPED)));36 }37}38The main thing to notice here is the use of the withText() method. This method is used to verify the text in the TextView. The withText() method is defined in the org.hamcrest.Matchers class. The org.hamcrest.Matchers class is imported in the code. The withText() method is

Full Screen

Full Screen

matchesSafely

Using AI Code Generation

copy

Full Screen

1import org.hamcrest.Description;2import org.hamcrest.Matcher;3import org.hamcrest.TypeSafeMatcher;4import org.junit.Test;5import static org.hamcrest.CoreMatchers.is;6import static org.hamcrest.MatcherAssert.assertThat;7public class TypeSafeMatcherTest {8 public void test() {9 assertThat(123, is(new TypeSafeMatcher<Integer>() {10 protected boolean matchesSafely(Integer item) {11 return item % 2 == 0;12 }13 public void describeTo(Description description) {14 description.appendText("is even");15 }16 }));17 }18}

Full Screen

Full Screen

matchesSafely

Using AI Code Generation

copy

Full Screen

1import org.hamcrest.TypeSafeMatcher;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5public class IsElementPresent extends TypeSafeMatcher<WebDriver> {6 private By locator;7 public IsElementPresent(By locator) {8 this.locator = locator;9 }10 public boolean matchesSafely(WebDriver driver) {11 try {12 driver.findElement(locator);13 return true;14 } catch (Exception e) {15 return false;16 }17 }18 public void describeTo(org.hamcrest.Description description) {19 description.appendText("Element was not found with locator: " + locator);20 }21 public static IsElementPresent isElementPresent(By locator) {22 return new IsElementPresent(locator);23 }24}25import static com.google.common.base.Preconditions.checkArgument;26import static com.google.common.base.Strings.isNullOrEmpty;27import static org.hamcrest.CoreMatchers.is;28import static org.hamcrest.MatcherAssert.assertThat;29import static org.hamcrest.Matchers.containsString;30import static org.hamcrest.Matchers.equalTo;31import static org.hamcrest.Matchers.not;32import static org.hamcrest.Matchers.nullValue;33import static org.hamcrest.core.Is.is;34import static org.hamcrest.core.IsNot.not;35import static org.junit.Assert.assertEquals;36import static org.junit.Assert.assertNotNull;37import static org.junit.Assert.assertTrue;38import static org.junit.Assert.fail;39import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable;40import static org.openqa.selenium.support.ui.ExpectedConditions.invisibilityOfElementLocated;41import static org.openqa.selenium.support.ui.ExpectedConditions.not;42import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;43import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;44import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;45import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;46import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;47import java.util.List;48import java.util.NoSuchElementException;49import java.util.concurrent.TimeUnit;50import org.hamcrest.TypeSafeMatcher;51import org.junit.Before;52import org.junit.Test;53import org.junit.runner.RunWith;54import org.openqa.selenium.By;55import org.openqa.selenium.WebDriver;56import org.openqa.selenium.WebElement;57import org.openqa.selenium.support.ui.ExpectedCondition;58import org.openqa.selenium.support.ui.ExpectedConditions;59import org.openqa.selenium.support.ui.FluentWait;60import org.openqa.selenium.support.ui.Wait;61import

Full Screen

Full Screen

matchesSafely

Using AI Code Generation

copy

Full Screen

1import org.hamcrest.TypeSafeMatcher;2import org.hamcrest.Description;3import org.hamcrest.Matcher;4public class IsNumber extends TypeSafeMatcher<String> {5 public boolean matchesSafely(String item) {6 return item.matches("^-?\\d+$");7 }8 public void describeTo(Description description) {9 description.appendText("is a number");10 }11 public static Matcher<String> isNumber() {12 return new IsNumber();13 }14}15import org.hamcrest.TypeSafeMatcher;16import org.hamcrest.Description;17import org.hamcrest.Matcher;18public class IsNumber extends TypeSafeMatcher<String> {19 public boolean matchesSafely(String item) {20 return item.matches("^-?\\d+$");21 }22 public void describeTo(Description description) {23 description.appendText("is a number");24 }25 public static Matcher<String> isNumber() {26 return new IsNumber();27 }28}29import org.hamcrest.TypeSafeMatcher;30import org.hamcrest.Description;31import org.hamcrest.Matcher;32public class IsNumber extends TypeSafeMatcher<String> {33 public boolean matchesSafely(String item) {34 return item.matches("^-?\\d+$");35 }36 public void describeTo(Description description) {37 description.appendText("is a number");38 }39 public static Matcher<String> isNumber() {40 return new IsNumber();41 }42}43import org.hamcrest.TypeSafeMatcher;44import org.hamcrest.Description;45import org.hamcrest.Matcher;46public class IsNumber extends TypeSafeMatcher<String> {47 public boolean matchesSafely(String item) {48 return item.matches("^-?\\d+$");49 }50 public void describeTo(Description description) {51 description.appendText("is a number");

Full Screen

Full Screen

matchesSafely

Using AI Code Generation

copy

Full Screen

1import org.hamcrest.Description;2import org.hamcrest.TypeSafeMatcher;3import java.io.File;4import java.io.IOException;5import java.nio.file.Files;6import java.nio.file.Path;7import java.nio.file.Paths;8public class NotEmptyFileMatcher extends TypeSafeMatcher<File> {9 public void describeTo(Description description) {10 description.appendText("file is not empty");11 }12 protected boolean matchesSafely(File file) {13 try {14 Path path = Paths.get(file.getAbsolutePath());15 return Files.size(path) > 0;16 } catch (IOException e) {17 return false;18 }19 }20}21import org.hamcrest.MatcherAssert;22import org.hamcrest.Matchers;23import org.junit.jupiter.api.Test;24import java.io.File;25import java.io.IOException;26import java.nio.file.Files;27import java.nio.file.Path;28import java.nio.file.Paths;29public class NotEmptyFileMatcherTest {30 public void test() throws IOException {31 File file = new File("src/test/resources/empty.txt");32 file.createNewFile();33 MatcherAssert.assertThat(file, Matchers.not(new NotEmptyFileMatcher()));34 }35}36import org.hamcrest.BaseMatcher;37import org.hamcrest.Description;38import java.io.File;39import java.io.IOException;40import java.nio.file.Files;41import java.nio.file.Path;42import java.nio.file.Paths;43public class NotEmptyFileMatcher2 extends BaseMatcher<File> {44 public void describeTo(Description description) {45 description.appendText("file is not empty");46 }47 public boolean matches(Object item) {48 File file = (File) item;49 try {50 Path path = Paths.get(file.getAbsolutePath());51 return Files.size(path) > 0;52 } catch (IOException e) {

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.

Most used method in TypeSafeMatcher

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful