How to use GroupTypeDescription method of org.assertj.core.error.GroupTypeDescription class

Best Assertj code snippet using org.assertj.core.error.GroupTypeDescription.GroupTypeDescription

Source:CustomMapAssert.java Github

copy

Full Screen

2import org.assertj.core.api.AssertionInfo;3import org.assertj.core.api.MapAssert;4import org.assertj.core.error.BasicErrorMessageFactory;5import org.assertj.core.error.ErrorMessageFactory;6import org.assertj.core.error.GroupTypeDescription;7import org.assertj.core.internal.ComparisonStrategy;8import org.assertj.core.internal.Failures;9import org.assertj.core.internal.Objects;10import org.assertj.core.internal.StandardComparisonStrategy;11import java.util.Collection;12import java.util.LinkedHashMap;13import java.util.LinkedHashSet;14import java.util.Map;15import java.util.Optional;16import java.util.Set;17import static org.assertj.core.api.Assertions.entry;18import static org.assertj.core.error.GroupTypeDescription.getGroupTypeDescription;19/**20 * Assertions for {@link Map}s.21 *22 * Includes slight changes from AssertJ's provided assertions to improve feedback23 * provided by failure messages.24 */25public class CustomMapAssert<K, V> extends MapAssert<K, V> {26 Failures failures = Failures.instance();27 public CustomMapAssert(Map<K, V> actual) {28 super(actual);29 }30 @Override31 public MapAssert<K, V> containsAllEntriesOf(Map<? extends K, ? extends V> other) {32 assertNotNull(info, actual);33 Map<K, V> getValueMismatch = new LinkedHashMap<>();34 Set<K> missingNullValuedEntries = new LinkedHashSet<>();35 for (Map.Entry<? extends K, ? extends V> entry : other.entrySet()) {36 if (entry.getValue() == null) {37 if (!actual.containsKey(entry.getKey())) {38 missingNullValuedEntries.add(entry.getKey());39 continue;40 }41 }42 Optional<Optional<V>> mismatchedGetValue = obtainGetValueMismatch(actual, entry);43 mismatchedGetValue.ifPresent(v -> getValueMismatch.put(entry.getKey(), v.orElse(null)));44 }45 if (getValueMismatch.isEmpty() && missingNullValuedEntries.isEmpty()) {46 return myself;47 }48 throw failures.failure(info, ShouldContainMultiple.shouldContain(49 actual, other, getValueMismatch, missingNullValuedEntries));50 }51 @Override52 public MapAssert<K, V> containsEntry(K key, V value) {53 assertNotNull(info, actual);54 Map.Entry<K, V> entry = entry(key, value);55 if (value == null) {56 // throw if value is null and key is not in actual57 containsKey(key);58 }59 Optional<Optional<V>> mismatchedGetValue = obtainGetValueMismatch(actual, entry);60 if (mismatchedGetValue.isPresent()) {61 throw failures.failure(info, MismatchedGet.shouldMatch(actual, entry,62 mismatchedGetValue.get().orElse(null)));63 }64 return myself;65 }66 /**67 * returns an optional:68 * - empty if get on the actual map matched the value of the entry;69 * - containing an optional:70 * - empty if the actual get value was null71 * - containing the actual get value if it was non-null72 * (this needs to be a nested Optional since a null-valued Optional is treated as empty,73 * meaning that the Optional would always be empty if actual.get returned null.)74 */75 private static <K, V> Optional<Optional<V>> obtainGetValueMismatch(Map<K, V> actual,76 Map.Entry<? extends K, ? extends V> entry) {77 V value = actual.get(entry.getKey());78 if (java.util.Objects.deepEquals(value, entry.getValue())) {79 return Optional.empty();80 }81 return Optional.of(Optional.ofNullable(value));82 }83 @Override84 public MapAssert<K, V> containsKey(K key) {85 assertNotNull(info, actual);86 if (!actual.containsKey(key)) {87 throw failures.failure(info, ContainsKeyFailure.shouldContainKey(actual, key));88 }89 return myself;90 }91 /**92 * Verifies that the actual map contains the given keys.93 * <p>94 * Example :95 * <pre><code class='java'> Map&lt;Ring, TolkienCharacter&gt; ringBearers = new HashMap&lt;&gt;();96 * ringBearers.put(nenya, galadriel);97 * ringBearers.put(narya, gandalf);98 * ringBearers.put(oneRing, frodo);99 *100 * // assertions will pass101 * assertThat(ringBearers).containsKeys(List.of(nenya, oneRing));102 *103 * // assertions will fail104 * assertThat(ringBearers).containsKeys(List.of(vilya));105 * assertThat(ringBearers).containsKeys(List.of(vilya, oneRing));</code></pre>106 *107 * @param keys the given keys108 * @return {@code this} assertions object109 * @throws AssertionError if the actual map is {@code null}.110 * @throws AssertionError if the actual map does not contain the given key.111 */112 public MapAssert<K, V> containsKeys(Collection<? extends K> keys) {113 assertNotNull(info, actual);114 Set<K> missingKeys = new LinkedHashSet<>();115 for (K key : keys) {116 if (!actual.containsKey(key)) {117 missingKeys.add(key);118 }119 }120 if (!missingKeys.isEmpty()) {121 throw failures.failure(info, ContainsKeyFailure.shouldContainKeys(actual, missingKeys));122 }123 return myself;124 }125 @Override126 public MapAssert<K, V> doesNotContainKey(K key) {127 assertNotNull(info, actual);128 if (actual.containsKey(key)) {129 throw failures.failure(info, ContainsKeyFailure.shouldNotContainKey(actual, key));130 }131 return myself;132 }133 private void assertNotNull(AssertionInfo info, Map<?, ?> actual) {134 Objects.instance().assertNotNull(info, actual);135 }136 public static final class ContainsKeyFailure extends BasicErrorMessageFactory {137 private <K, V> ContainsKeyFailure(Map<K, V> actual, K key, boolean expectedBool, boolean actualBool) {138 super("%nExpecting:%n"139 + " <%s>%n"140 + "to " + (expectedBool ? "contain" : "not contain") + " key:%n"141 + " <%s>%n"142 + "but containsKey returned " + actualBool,143 actual, key);144 }145 private <K, V> ContainsKeyFailure(Map<K, V> actual, Set<K> keys, boolean expectedBool, boolean actualBool) {146 super("%nExpecting:%n"147 + " <%s>%n"148 + "to " + (expectedBool ? "contain" : "not contain") + " keys:%n"149 + " <%s>%n"150 + "but containsKey returned " + actualBool + " for each",151 actual, keys);152 }153 /**154 * Creates a new {@code ContainsKeyFailure}.155 *156 * @param actual the actual value in the failed assertion.157 * @param key the expected key158 * @return the created {@code ErrorMessageFactory}.159 */160 public static <K, V> ErrorMessageFactory shouldContainKey(Map<K, V> actual, K key) {161 return new ContainsKeyFailure(actual, key, true, false);162 }163 /**164 * Creates a new {@code ContainsKeyFailure}.165 *166 * @param actual the actual value in the failed assertion.167 * @param keys the expected keys168 * @return the created {@code ErrorMessageFactory}.169 */170 public static <K, V> ErrorMessageFactory shouldContainKeys(Map<K, V> actual, Set<K> keys) {171 if (keys.size() == 1) {172 return new ContainsKeyFailure(actual, keys.iterator().next(), true, false);173 }174 return new ContainsKeyFailure(actual, keys, true, false);175 }176 /**177 * Creates a new {@code ContainsKeyFailure}.178 *179 * @param actual the actual value in the failed assertion.180 * @param key the unexpected key181 * @return the created {@code ErrorMessageFactory}.182 */183 public static <V, K> ErrorMessageFactory shouldNotContainKey(Map<K, V> actual, K key) {184 return new ContainsKeyFailure(actual, key, false, true);185 }186 }187 /**188 * Creates an error message indicating that an assertion that verifies a map189 * contains a given entry failed because the map returned an unexpected and non-null value.190 * It also mentions the {@link ComparisonStrategy} used.191 */192 public static final class MismatchedGet extends BasicErrorMessageFactory {193 private <K, V> MismatchedGet(Map<K, V> actual,194 Map.Entry<K, V> expected,195 V actualGetValue,196 ComparisonStrategy comparisonStrategy,197 GroupTypeDescription groupTypeDescription) {198 super("%nExpecting " + groupTypeDescription.getGroupTypeName() + ":%n"199 + " <%s>%n"200 + "to contain:%n"201 + " <%s>%n"202 + "but get returned:%n"203 + " <%s>%n"204 + "%s",205 actual, expected, actualGetValue,206 comparisonStrategy);207 }208 /**209 * Creates a new {@code MismatchedGet}.210 *211 * @param actual the actual value in the failed assertion.212 * @param expected entry expected to be in {@code actual}.213 * @param actualGetValue the value returned by {@code actual.get}.214 * @return the created {@code ErrorMessageFactory}.215 */216 public static <K, V> ErrorMessageFactory shouldMatch(Map<K, V> actual,217 Map.Entry<K, V> expected,218 V actualGetValue) {219 return shouldMatch(actual, expected, actualGetValue, StandardComparisonStrategy.instance());220 }221 /**222 * Creates a new {@code MismatchedGet}.223 *224 * @param actual the actual value in the failed assertion.225 * @param expected entry expected to be in {@code actual}.226 * @param actualGetValue the value returned by {@code actual.get}.227 * @param comparisonStrategy the {@link ComparisonStrategy} used to evaluate assertion.228 * @return the created {@code ErrorMessageFactory}.229 */230 public static <K, V> ErrorMessageFactory shouldMatch(Map<K, V> actual,231 Map.Entry<K, V> expected,232 V actualGetValue,233 ComparisonStrategy comparisonStrategy) {234 GroupTypeDescription groupTypeDescription = getGroupTypeDescription(actual);235 return new MismatchedGet(actual, expected, actualGetValue, comparisonStrategy, groupTypeDescription);236 }237 }238 /**239 * Creates an error message indicating that an assertion that verifies a {@code Map}240 * contains a given set of entries failed.241 * It also mentions the {@link ComparisonStrategy} used.242 */243 private static final class ShouldContainMultiple extends BasicErrorMessageFactory {244 private <K, V> ShouldContainMultiple(Map<K, V> actual,245 Map<? extends K, ? extends V> expected,246 Map<K, V> mismatches,247 Set<K> notFound,248 ComparisonStrategy comparisonStrategy,249 GroupTypeDescription groupTypeDescription) {250 super("%nExpecting " + groupTypeDescription.getGroupTypeName() + ":%n"251 + " <%s>%n"252 + "to contain:%n"253 + " <%s>%n"254 + "but get calls returned unexpected values for the following:%n"255 + " <%s>%n"256 + "and some keys were not contained:%n"257 + " <%s>%n"258 + "%s",259 actual, expected, mismatches, notFound, comparisonStrategy);260 }261 private <K, V> ShouldContainMultiple(Map<K, V> actual,262 Map<? extends K, ? extends V> expected,263 Map<K, V> mismatches,264 ComparisonStrategy comparisonStrategy,265 GroupTypeDescription groupTypeDescription) {266 super("%nExpecting " + groupTypeDescription.getGroupTypeName() + ":%n"267 + " <%s>%n"268 + "to contain:%n"269 + " <%s>%n"270 + "but get calls returned unexpected values for the following:%n"271 + " <%s>%n"272 + "%s",273 actual, expected, mismatches, comparisonStrategy);274 }275 private <K, V> ShouldContainMultiple(Map<K, V> actual,276 Map<? extends K, ? extends V> expected,277 Set<K> notFound,278 ComparisonStrategy comparisonStrategy,279 GroupTypeDescription groupTypeDescription) {280 super("%nExpecting " + groupTypeDescription.getGroupTypeName() + ":%n"281 + " <%s>%n"282 + "to contain:%n"283 + " <%s>%n"284 + "but some keys were not contained:%n"285 + " <%s>%n"286 + "%s",287 actual, expected, notFound, comparisonStrategy);288 }289 /**290 * Creates a new {@code MismatchedGet}.291 *292 * @param actual the actual value in the failed assertion.293 * @param expected map containing entries expected to be in {@code actual}.294 * @param mismatches map containing mismatched values returned by {@code actual.get}.295 * @param notFound set of expected keys not contained by the actual map296 * (only includes keys which were expected to have null values)297 * @return the created {@code ErrorMessageFactory}.298 */299 public static <K, V> ErrorMessageFactory shouldContain(Map<K, V> actual,300 Map<? extends K, ? extends V> expected,301 Map<K, V> mismatches,302 Set<K> notFound) {303 return shouldContain(actual, expected, mismatches, notFound, StandardComparisonStrategy.instance());304 }305 /**306 * Creates a new {@code MismatchedGet}.307 *308 * @param actual the actual value in the failed assertion.309 * @param expected map containing entries expected to be in {@code actual}.310 * @param mismatches map containing mismatched values returned by {@code actual.get}.311 * @param notFound set of expected keys not contained by the actual map312 * (only includes keys which were expected to have null values)313 * @param comparisonStrategy the {@link ComparisonStrategy} used to evaluate assertion.314 * @return the created {@code ErrorMessageFactory}.315 */316 public static <K, V> ErrorMessageFactory shouldContain(Map<K, V> actual,317 Map<? extends K, ? extends V> expected,318 Map<K, V> mismatches,319 Set<K> notFound,320 ComparisonStrategy comparisonStrategy) {321 GroupTypeDescription groupTypeDescription = getGroupTypeDescription(actual);322 if (mismatches.isEmpty()) {323 return new ShouldContainMultiple(324 actual,325 expected,326 notFound,327 comparisonStrategy,328 groupTypeDescription);329 }330 if (notFound.isEmpty()) {331 return new ShouldContainMultiple(332 actual,333 expected,334 mismatches,335 comparisonStrategy,...

Full Screen

Full Screen

GroupTypeDescription

Using AI Code Generation

copy

Full Screen

1 public static String groupTypeDescription(Object group) {2 if (group == null) {3 return "null";4 }5 if (group.getClass().isArray()) {6 return "array of " + arrayTypeDescription(group);7 }8 if (group instanceof Collection) {9 return "collection of " + collectionTypeDescription((Collection<?>) group);10 }11 return "group of " + group.getClass().getSimpleName();12 }13 private static String arrayTypeDescription(Object array) {14 if (array == null) {15 return "null";16 }17 if (array.getClass().getComponentType().isPrimitive()) {18 return array.getClass().getComponentType().getSimpleName();19 }20 return array.getClass().getComponentType().getSimpleName() + "s";21 }22 private static String collectionTypeDescription(Collection<?> collection) {23 if (collection.isEmpty()) {24 return "empty collection";25 }26 Iterator<?> iterator = collection.iterator();27 Object firstElement = iterator.next();28 if (firstElement == null) {29 return "null";30 }31 return firstElement.getClass().getSimpleName() + "s";32 }33This file has been truncated. [show original](github.com/assertj/assertj-cor...) 34[github.com](github.com/assertj/assertj-cor...) 35#### [assertj/assertj-core/blob/main/src/main/java/org/assertj/core/error/ShouldContain.java](github.com/assertj/assertj-cor...)36 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with

Full Screen

Full Screen

GroupTypeDescription

Using AI Code Generation

copy

Full Screen

1@DisplayName("GroupTypeDescription")2class GroupTypeDescriptionTest {3 @DisplayName("should return correct description")4 void shouldReturnCorrectDescription() {5 String expected = "a collection";6 String result = GroupTypeDescription.groupTypeDescription();7 assertThat(result).isEqualTo(expected);8 }9}10package org.assertj.core.error;11import static org.assertj.core.error.GroupTypeDescription.groupTypeDescription;12import org.assertj.core.description.Description;13import org.assertj.core.description.TextDescription;14public class GroupTypeDescription {15 private static final Description DESCRIPTION = new TextDescription("a collection");16 public static Description groupTypeDescription() {17 return DESCRIPTION;18 }19 private GroupTypeDescription() {}20}21package org.assertj.core.error;22import org.assertj.core.description.Description;23public class GroupTypeDescription {24 private static final Description DESCRIPTION = new TextDescription("a collection");25 public static Description groupTypeDescription() {26 return DESCRIPTION;27 }28 private GroupTypeDescription() {}29}30package org.assertj.core.error;31import org.assertj.core.description.Description;32import org.assertj.core.description.TextDescription;33public class GroupTypeDescription {34 private static final Description DESCRIPTION = new TextDescription("a collection");35 public static Description groupTypeDescription() {36 return DESCRIPTION;37 }38 private GroupTypeDescription() {}39}40package org.assertj.core.error;41import org.assertj.core.description.Description;42import org.assertj.core.description.TextDescription;43public class GroupTypeDescription {44 private static final Description DESCRIPTION = new TextDescription("a collection");45 public static Description groupTypeDescription() {46 return DESCRIPTION;47 }48 private GroupTypeDescription() {}49}50package org.assertj.core.error;51import org.assertj.core.description.Description;52import org.assertj.core.description.TextDescription;53public class GroupTypeDescription {54 private static final Description DESCRIPTION = new TextDescription("a collection");55 public static Description groupTypeDescription() {56 return DESCRIPTION;57 }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Assertj 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