Best junit code snippet using org.hamcrest.TypeSafeDiagnosingMatcher.matches
Source:NextMatchers.java
...30 private NextMatchers() {31 // prevent instantiation32 }33 /**34 * Returns a matcher that matches {@link Next} instances without a model.35 *36 * @param <M> the model type37 * @param <F> the effect type38 */39 public static <M, F> Matcher<Next<M, F>> hasNoModel() {40 return new TypeSafeDiagnosingMatcher<Next<M, F>>() {41 @Override42 protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {43 if (item.hasModel()) {44 mismatchDescription.appendText("it had a model: " + item.modelUnsafe());45 return false;46 }47 return true;48 }49 @Override50 public void describeTo(Description description) {51 description.appendText("Next without model");52 }53 };54 }55 /**56 * Returns a matcher that matches {@link Next} instances with a model.57 *58 * @param <M> the model type59 * @param <F> the effect type60 */61 public static <M, F> Matcher<Next<M, F>> hasModel() {62 return new TypeSafeDiagnosingMatcher<Next<M, F>>() {63 @Override64 protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {65 if (!item.hasModel()) {66 mismatchDescription.appendText("it had no model");67 return false;68 }69 return true;70 }71 @Override72 public void describeTo(Description description) {73 description.appendText("Next with any model");74 }75 };76 }77 /**78 * Returns a matcher that matches {@link Next} instances with a model that is equal to the79 * supplied one.80 *81 * @param expected the expected model82 * @param <M> the model type83 * @param <F> the effect type84 */85 public static <M, F> Matcher<Next<M, F>> hasModel(M expected) {86 return hasModel(equalTo(expected));87 }88 /**89 * Returns a matcher that matches {@link Next} instances with a model that matches the supplied90 * model matcher.91 *92 * @param matcher the matcher to apply to the model93 * @param <M> the model type94 * @param <F> the effect type95 */96 public static <M, F> Matcher<Next<M, F>> hasModel(Matcher<M> matcher) {97 return new TypeSafeDiagnosingMatcher<Next<M, F>>() {98 @Override99 protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {100 if (!item.hasModel()) {101 mismatchDescription.appendText("it had no model");102 return false;103 }104 if (!matcher.matches(item.modelUnsafe())) {105 mismatchDescription.appendText("the model ");106 matcher.describeMismatch(item.modelUnsafe(), mismatchDescription);107 return false;108 }109 return true;110 }111 @Override112 public void describeTo(Description description) {113 description.appendText("Next with model ").appendDescriptionOf(matcher);114 }115 };116 }117 /**118 * Returns a matcher that matches {@link Next} instances with no effects.119 *120 * @param <M> the model type121 * @param <F> the effect type122 */123 public static <M, F> Matcher<Next<M, F>> hasNoEffects() {124 return new TypeSafeDiagnosingMatcher<Next<M, F>>() {125 @Override126 protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {127 if (item.hasEffects()) {128 mismatchDescription.appendText("it had effects: " + item.effects());129 return false;130 }131 return true;132 }133 @Override134 public void describeTo(Description description) {135 description.appendText("Next without effects");136 }137 };138 }139 /**140 * Returns a matcher that matches {@link Next} instances whose effects match the supplied effect141 * matcher.142 *143 * @param matcher the matcher to apply to the effects144 * @param <M> the model type145 * @param <F> the effect type146 */147 public static <M, F> Matcher<Next<M, F>> hasEffects(Matcher<Iterable<F>> matcher) {148 return new TypeSafeDiagnosingMatcher<Next<M, F>>() {149 @Override150 protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) {151 if (!item.hasEffects()) {152 mismatchDescription.appendText("it had no effects");153 return false;154 }155 if (!matcher.matches(item.effects())) {156 mismatchDescription.appendText("the effects were ");157 matcher.describeMismatch(item.effects(), mismatchDescription);158 return false;159 }160 return true;161 }162 @Override163 public void describeTo(Description description) {164 description.appendText("Next with effects ").appendDescriptionOf(matcher);165 }166 };167 }168 /**169 * Returns a matcher that matches if all the supplied effects are present in the supplied {@link170 * Next}, in any order. The {@link Next} may have more effects than the ones included.171 *172 * @param effects the effects to match (possibly empty)173 * @param <M> the model type174 * @param <F> the effect type175 * @return a matcher that matches {@link Next} instances that include all the supplied effects176 */177 @SafeVarargs178 public static <M, F> Matcher<Next<M, F>> hasEffects(F... effects) {179 return hasEffects(hasItems(effects));180 }181 /**182 * Returns a matcher that matches {@link Next} instances with no model and no effects.183 *184 * @param <M> the model type185 * @param <F> the effect type186 */187 public static <M, F> Matcher<Next<M, F>> hasNothing() {188 return allOf(hasNoModel(), hasNoEffects());189 }190}...
Source:ValidationErrorMatchers.java
...17 description18 .appendText("Validation error which ").appendDescriptionOf(matcher);19 }20 @Override21 protected boolean matchesSafely(JsonObject representation, Description description) {22 final Matcher<Iterable<? super JsonObject>> iterableMatcher = IsCollectionContaining.hasItem(matcher);23 final List<JsonObject> errors = JsonArrayHelper.toList(representation, "errors");24 iterableMatcher.describeMismatch(errors, description);25 return iterableMatcher.matches(errors);26 }27 };28 }29 public static TypeSafeDiagnosingMatcher<JsonObject> hasParameter(String key, String value) {30 return new TypeSafeDiagnosingMatcher<JsonObject>() {31 @Override32 public void describeTo(Description description) {33 description.appendText("has parameter with key ").appendValue(key)34 .appendText(" and value ").appendValue(value);35 }36 @Override37 protected boolean matchesSafely(JsonObject error, Description description) {38 final List<JsonObject> parameters = JsonArrayHelper.toList(error, "parameters");39 final boolean hasParameter = hasParameter(parameters, key, value);40 if(!hasParameter) {41 if(!hasParameter(parameters, key)) {42 description.appendText("does not have parameter ")43 .appendValue(key);44 }45 else {46 description.appendText("parameter has value ")47 .appendValue(getParameter(parameters, key));48 }49 }50 return hasParameter;51 }52 };53 }54 private static String getParameter(List<JsonObject> parameters, String key) {55 return parameters.stream().filter(parameter ->56 Objects.equals(parameter.getString("key"), key))57 .findFirst()58 .map(parameter -> parameter.getString("value"))59 .orElse(null);60 }61 private static boolean hasParameter(List<JsonObject> parameters, String key) {62 return parameters.stream().anyMatch(parameter ->63 Objects.equals(parameter.getString("key"), key));64 }65 private static boolean hasParameter(List<JsonObject> parameters, String key, String value) {66 return parameters.stream().anyMatch(parameter ->67 Objects.equals(parameter.getString("key"), key)68 && Objects.equals(parameter.getString("value"), value));69 }70 public static TypeSafeDiagnosingMatcher<JsonObject> hasMessage(String message) {71 return new TypeSafeDiagnosingMatcher<JsonObject>() {72 @Override73 public void describeTo(Description description) {74 description.appendText("has message ").appendValue(message);75 }76 @Override77 protected boolean matchesSafely(JsonObject error, Description description) {78 final Matcher<String> matcher = is(message);79 matcher.describeMismatch(error, description);80 return matcher.matches(error.getString("message"));81 }82 };83 }84 public static TypeSafeDiagnosingMatcher<JsonObject> hasMessageContaining(String message) {85 return new TypeSafeDiagnosingMatcher<JsonObject>() {86 @Override87 public void describeTo(Description description) {88 description.appendText("has message containing ").appendValue(message);89 }90 @Override91 protected boolean matchesSafely(JsonObject error, Description description) {92 final Matcher<String> matcher = containsString(message);93 matcher.describeMismatch(error, description);94 return matcher.matches(error.getString("message"));95 }96 };97 }98}...
Source:OkapiResponseStatusCodeMatchers.java
...40 public static Matcher<Integer> isInternalServerError() {41 return is(HTTP_INTERNAL_SERVER_ERROR.toInt());42 }43 /** HTTP Status-Code 200: Ok. */44 public static TypeSafeDiagnosingMatcher<TextResponse> matchesOk() {45 return hasStatusCode(HTTP_OK.toInt());46 }47 /** HTTP Status-Code 201: Created. */48 public static TypeSafeDiagnosingMatcher<TextResponse> matchesCreated() {49 return hasStatusCode(HTTP_CREATED.toInt());50 }51 /** HTTP Status-Code 204: No content. */52 public static TypeSafeDiagnosingMatcher<TextResponse> matchesNoContent() {53 return hasStatusCode(HTTP_NO_CONTENT.toInt());54 }55 /** HTTP Status-Code 400: Bad request. */56 public static TypeSafeDiagnosingMatcher<TextResponse> matchesBadRequest() {57 return hasStatusCode(HTTP_BAD_REQUEST.toInt());58 }59 /** HTTP Status-Code 404: Not Found. */60 public static TypeSafeDiagnosingMatcher<TextResponse> matchesNotFound() {61 return hasStatusCode(HTTP_NOT_FOUND.toInt());62 }63 /** HTTP Status-Code 422: Unprocessable entity. */64 public static TypeSafeDiagnosingMatcher<TextResponse> matchesUnprocessableEntity() {65 return hasStatusCode(HTTP_UNPROCESSABLE_ENTITY.toInt());66 }67 /** HTTP Status-Code 500: Internal server error. */68 public static TypeSafeDiagnosingMatcher<TextResponse> matchInternalServerError() {69 return hasStatusCode(HTTP_INTERNAL_SERVER_ERROR.toInt());70 }71 private static TypeSafeDiagnosingMatcher<TextResponse> hasStatusCode(Integer statusCode) {72 return new TypeSafeDiagnosingMatcher<TextResponse>() {73 @Override74 public void describeTo(Description description) {75 description.appendText("a response with status code ")76 .appendValue(statusCode);77 }78 @Override79 protected boolean matchesSafely(TextResponse response, Description description) {80 final Matcher<Integer> statusCodeMatcher = is(statusCode);81 statusCodeMatcher.describeMismatch(response.getStatusCode(), description);82 final boolean matches = statusCodeMatcher.matches(response.getStatusCode());83 if(!matches) {84 description.appendText("\nReceived body: ").appendValue(response.getBody());85 }86 return matches;87 }88 };89 }90}...
Source:RequestItemMatcher.java
...14 description15 .appendText("Location which ").appendDescriptionOf(matcher);16 }17 @Override18 protected boolean matchesSafely(JsonObject representation, Description description) {19 JsonObject item = representation.getJsonObject("item").getJsonObject("location");20 Map<String, Object> itemMap = item.getMap();21 matcher.describeMismatch(itemMap, description);22 return matcher.matches(itemMap);23 }24 };25 }26 public static TypeSafeDiagnosingMatcher<Object> hasLibraryName(String value) {27 return new TypeSafeDiagnosingMatcher<Object>() {28 @Override29 public void describeTo(Description description) {30 description.appendText("has library name ").appendValue(value);31 }32 @Override33 protected boolean matchesSafely(Object map, Description description) {34 Matcher<Map<? extends String, ? extends String>> matcher = IsMapContaining.hasEntry("libraryName", value);35 matcher.describeMismatch(map, description);36 return matcher.matches(map);37 }38 };39 }40 public static TypeSafeDiagnosingMatcher<Object> hasLocationCode(String value) {41 return new TypeSafeDiagnosingMatcher<Object>() {42 @Override43 public void describeTo(Description description) {44 description.appendText("has location code ").appendValue(value);45 }46 @Override47 protected boolean matchesSafely(Object map, Description description) {48 Matcher<Map<? extends String, ? extends String>> matcher = IsMapContaining.hasEntry("code", value);49 matcher.describeMismatch(map, description);50 return matcher.matches(map);51 }52 };53 }54 public static TypeSafeDiagnosingMatcher<Object> hasLocationName(String value) {55 return new TypeSafeDiagnosingMatcher<Object>() {56 @Override57 public void describeTo(Description description) {58 description.appendText("has location name ").appendValue(value);59 }60 @Override61 protected boolean matchesSafely(Object map, Description description) {62 Matcher<Map<? extends String, ? extends String>> matcher = IsMapContaining.hasEntry("name", value);63 matcher.describeMismatch(map, description);64 return matcher.matches(map);65 }66 };67 }68}...
Source:HttpResponseStatusCodeMatchers.java
...47 description.appendText("a response with status code ")48 .appendValue(statusCode);49 }50 @Override51 protected boolean matchesSafely(TextResponse response, Description description) {52 final Matcher<Integer> statusCodeMatcher = is(statusCode);53 statusCodeMatcher.describeMismatch(response.getStatusCode(), description);54 final boolean matches = statusCodeMatcher.matches(response.getStatusCode());55 if(!matches) {56 description.appendText("\nReceived body: ").appendValue(response.getBody());57 }58 return matches;59 }60 };61 }62}...
Source:LoanMatchers.java
...19 @Override public void describeTo(Description description) {20 description.appendText("Loan should have a status of ")21 .appendText(status);22 }23 @Override protected boolean matchesSafely(JsonObject representation,24 Description description) {25 if (!representation.containsKey("status")) {26 description.appendText("has no status property");27 return false;28 }29 final Matcher<String> statusMatcher = is(status);30 final String statusName = representation31 .getJsonObject("status")32 .getString("name");33 statusMatcher.describeMismatch(statusName, description);34 return statusMatcher.matches(statusName);35 }36 };37 }38 static TypeSafeDiagnosingMatcher<JsonObject> doesNotHaveUserId() {39 return new TypeSafeDiagnosingMatcher<JsonObject>() {40 @Override public void describeTo(Description description) {41 description.appendText("Anonymized loan should not have a userId");42 }43 @Override protected boolean matchesSafely(JsonObject representation,44 Description description) {45 return !representation.containsKey("userId");46 }47 };48 }49}...
Source:Every.java
...7 /* JADX DEBUG: Failed to find minimal casts for resolve overloaded methods, cast all args instead8 method: MutableMD:(java.lang.Iterable, org.hamcrest.Description):boolean9 arg types: [java.lang.Iterable<? extends T>, org.hamcrest.Description]10 candidates:11 org.hamcrest.core.Every.matchesSafely(java.lang.Object, org.hamcrest.Description):boolean12 MutableMD:(java.lang.Object, org.hamcrest.Description):boolean13 MutableMD:(java.lang.Iterable, org.hamcrest.Description):boolean */14 @Override // org.hamcrest.TypeSafeDiagnosingMatcher15 public /* bridge */ /* synthetic */ boolean matchesSafely(Object obj, Description description) {16 return matchesSafely((Iterable) ((Iterable) obj), description);17 }18 public Every(Matcher<? super T> matcher2) {19 this.matcher = matcher2;20 }21 public boolean matchesSafely(Iterable<? extends T> collection, Description mismatchDescription) {22 for (T t : collection) {23 if (!this.matcher.matches(t)) {24 mismatchDescription.appendText("an item ");25 this.matcher.describeMismatch(t, mismatchDescription);26 return false;27 }28 }29 return true;30 }31 @Override // org.hamcrest.SelfDescribing32 public void describeTo(Description description) {33 description.appendText("every item is ").appendDescriptionOf(this.matcher);34 }35 public static <U> Matcher<Iterable<? extends U>> everyItem(Matcher<U> itemMatcher) {36 return new Every(itemMatcher);37 }...
Source:TypeSafeDiagnosingMatcher.java
1package org.hamcrest;2import org.hamcrest.Description;3import org.hamcrest.internal.ReflectiveTypeFinder;4public abstract class TypeSafeDiagnosingMatcher<T> extends BaseMatcher<T> {5 private static final ReflectiveTypeFinder TYPE_FINDER = new ReflectiveTypeFinder("matchesSafely", 2, 0);6 private final Class<?> expectedType;7 /* access modifiers changed from: protected */8 public abstract boolean matchesSafely(T t, Description description);9 protected TypeSafeDiagnosingMatcher(Class<?> expectedType2) {10 this.expectedType = expectedType2;11 }12 protected TypeSafeDiagnosingMatcher(ReflectiveTypeFinder typeFinder) {13 this.expectedType = typeFinder.findExpectedType(getClass());14 }15 protected TypeSafeDiagnosingMatcher() {16 this(TYPE_FINDER);17 }18 @Override // org.hamcrest.Matcher19 public final boolean matches(Object item) {20 return item != null && this.expectedType.isInstance(item) && matchesSafely(item, new Description.NullDescription());21 }22 @Override // org.hamcrest.BaseMatcher, org.hamcrest.Matcher23 public final void describeMismatch(Object item, Description mismatchDescription) {24 if (item == null || !this.expectedType.isInstance(item)) {25 super.describeMismatch(item, mismatchDescription);26 } else {27 matchesSafely(item, mismatchDescription);28 }29 }30}...
matches
Using AI Code Generation
1import static org.hamcrest.MatcherAssert.assertThat;2import static org.hamcrest.Matchers.*;3import java.util.Arrays;4import java.util.List;5import org.hamcrest.Description;6import org.hamcrest.Matcher;7import org.hamcrest.TypeSafeDiagnosingMatcher;8import org.junit.Test;9public class TypeSafeDiagnosingMatcherTest {10 public void matchesSafely() {11 Matcher<List<Integer>> matcher = matches(Arrays.asList(1, 2, 3));12 assertThat(Arrays.asList(1, 2, 3), matcher);13 assertThat(Arrays.asList(1, 2, 4), not(matcher));14 }15 public void describeTo() {16 Matcher<List<Integer>> matcher = matches(Arrays.asList(1, 2, 3));17 Description description = new StringDescription();18 matcher.describeTo(description);19 assertThat(description.toString(), equalTo("List containing [1, 2, 3]"));20 }21 public void describeMismatch() {22 Matcher<List<Integer>> matcher = matches(Arrays.asList(1, 2, 3));23 Description description = new StringDescription();24 matcher.describeMismatch(Arrays.asList(1, 2, 4), description);25 assertThat(description.toString(), equalTo("was List containing [1, 2, 4]"));26 }27 public void hasItem() {28 Matcher<List<Integer>> matcher = hasItem(3);29 assertThat(Arrays.asList(1, 2, 3), matcher);30 assertThat(Arrays.asList(1, 2, 4), not(matcher));31 }32 private static Matcher<List<Integer>> matches(final List<Integer> expected) {33 return new TypeSafeDiagnosingMatcher<List<Integer>>() {34 protected boolean matchesSafely(List<Integer> actual, Description mismatchDescription) {35 if (actual.equals(expected)) {36 return true;37 } else {38 mismatchDescription.appendText("was ").appendValue(actual);39 return false;40 }41 }42 public void describeTo(Description description) {43 description.appendText("List containing ").appendValue(expected);44 }45 };46 }47}48 at org.junit.Assert.assertEquals(Assert.java:115)
matches
Using AI Code Generation
1import org.hamcrest.Description2import org.hamcrest.TypeSafeDiagnosingMatcher3class IsEmptyString : TypeSafeDiagnosingMatcher<String>() {4 override fun describeTo(description: Description?) {5 description?.appendText("an empty string")6 }7 override fun matchesSafely(item: String?, mismatchDescription: Description?): Boolean {8 if (item == null) {9 mismatchDescription?.appendText("was null")10 }11 if (item == "") {12 }13 mismatchDescription?.appendText("was \"$item\"")14 }15}16fun isEmptyString() = IsEmptyString()17assertThat("", isEmptyString())18assertThat(" ", !isEmptyString())19assertThat(null, !isEmptyString())20assertThat("abc", !isEmptyString())21assertThat("", isEmptyString())22assertThat(" ", !isEmptyString())23assertThat(null, !isEmptyString())24assertThat("abc", !isEmptyString())25assertThat("", isEmptyString())26assertThat(" ", !isEmptyString())27assertThat(null, !isEmptyString())28assertThat("abc", !isEmptyString())29assertThat("", isEmptyString())30assertThat(" ", !isEmptyString())31assertThat(null, !isEmptyString())32assertThat("abc", !isEmptyString())33assertThat("", isEmptyString())34assertThat(" ", !isEmptyString())35assertThat(null, !isEmptyString())36assertThat("abc", !isEmptyString())
matches
Using AI Code Generation
1import org.hamcrest.TypeSafeDiagnosingMatcher2class IsStringContainingIgnoringCase extends TypeSafeDiagnosingMatcher<String> {3 IsStringContainingIgnoringCase(String expected) {4 }5 boolean matchesSafely(String actual, Description mismatchDescription) {6 boolean matches = actual.toLowerCase().contains(expected.toLowerCase())7 if (!matches)8 mismatchDescription.appendText("was ").appendValue(actual)9 }10 void describeTo(Description description) {11 description.appendText("a string containing ").appendValue(expected)12 }13 static Matcher<String> containsIgnoringCase(String expected) {14 return new IsStringContainingIgnoringCase(expected)15 }16}17assertThat('Some Text', containsIgnoringCase('text'))18import org.hamcrest.TypeSafeDiagnosingMatcher19class IsStringContainingIgnoringCase extends TypeSafeDiagnosingMatcher<String> {20 IsStringContainingIgnoringCase(String expected) {21 }22 boolean matchesSafely(String actual, Description mismatchDescription) {23 boolean matches = actual.toLowerCase().contains(expected.toLowerCase())24 if (!matches)25 mismatchDescription.appendText("was ").appendValue(actual)26 }27 void describeTo(Description description) {28 description.appendText("a string containing ").appendValue(expected)29 }30 static Matcher<String> containsIgnoringCase(String expected) {31 return new IsStringContainingIgnoringCase(expected)32 }33}34assertThat('Some Text', containsIgnoringCase('text'))35import org.hamcrest.TypeSafeDiagnosingMatcher;36class IsStringContainingIgnoringCase extends TypeSafeDiagnosingMatcher<String> {37 private final String expected;38 IsStringContainingIgnoringCase(String expected) {39 this.expected = expected;40 }41 boolean matchesSafely(String actual, Description mismatchDescription) {42 boolean matches = actual.toLowerCase().contains(expected.toLowerCase());43 if (!matches)44 mismatchDescription.appendText("was ").appendValue(actual);45 return matches;46 }
matches
Using AI Code Generation
1import static org.hamcrest.MatcherAssert.assertThat;2import static org.hamcrest.Matchers.*;3import static org.hamcrest.Matchers.equalTo;4import org.hamcrest.*;5import org.junit.Test;6public class TestStringMatcher {7 public void testStringMatcher() {8 String str = "This is a test string";9 assertThat(str, startsWith("This"));10 assertThat(str, endsWith("string"));11 assertThat(str, containsString("is a"));12 assertThat(str, equalTo("This is a test string"));13 }14}15BUILD SUCCESSFUL (total time: 0 seconds)
matches
Using AI Code Generation
1import org.hamcrest.TypeSafeDiagnosingMatcher2import org.hamcrest.Description3import java.util.regex.Pattern4class PatternMatches(5) : TypeSafeDiagnosingMatcher<String>() {6 override fun describeTo(description: Description) {7 description.appendText(matchDescription)8 }9 override fun matchesSafely(string: String, mismatchDescription: Description): Boolean {10 val matches = pattern.matcher(string).matches()11 if (matches) {12 mismatchDescription.appendText(this.mismatchDescription)13 }14 }15}16fun matches(pattern: Pattern, matchDescription: String, mismatchDescription: String): PatternMatches {17 return PatternMatches(pattern, matchDescription, mismatchDescription)18}19import org.hamcrest.TypeSafeDiagnosingMatcher20import org.hamcrest.Description21import java.util.regex.Pattern22class PatternMatches(23) : TypeSafeDiagnosingMatcher<String>() {24 override fun describeTo(description: Description) {25 description.appendText(matchDescription)26 }27 override fun matchesSafely(string: String, mismatchDescription: Description): Boolean {28 val matches = pattern.matcher(string).matches()29 if (matches) {30 mismatchDescription.appendText(this.mismatchDescription)31 }32 }33}34fun matches(pattern: Pattern, matchDescription: String, mismatchDescription: String): PatternMatches {35 return PatternMatches(pattern, matchDescription, mismatchDescription)36}
LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed JUnit testing chapters to help you get started:
You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.
Get 100 minutes of automation test minutes FREE!!