Best Assertj code snippet using org.assertj.core.internal.Maps.failIfNull
Source:Maps.java
...161 * @throws AssertionError if the given {@code Map} is {@code null}.162 * @throws AssertionError if the given {@code Map} does not contain the given entries.163 */164 public <K, V> void assertContains(AssertionInfo info, Map<K, V> actual, Map.Entry<? extends K, ? extends V>[] entries) {165 failIfNull(entries);166 assertNotNull(info, actual);167 // if both actual and values are empty, then assertion passes.168 if (actual.isEmpty() && entries.length == 0)169 return;170 failIfEmptySinceActualIsNotEmpty(entries);171 Set<Map.Entry<? extends K, ? extends V>> notFound = new LinkedHashSet<>();172 for (Map.Entry<? extends K, ? extends V> entry : entries) {173 if (!containsEntry(actual, entry)) {174 notFound.add(entry);175 }176 }177 if (notFound.isEmpty()) return;178 throw failures.failure(info, shouldContain(actual, entries, notFound));179 }180 /**181 * Asserts that the given {@code Map} does not contain the given entries.182 * 183 * @param info contains information about the assertion.184 * @param actual the given {@code Map}.185 * @param entries the entries that are expected to be in the given {@code Map}.186 * @throws NullPointerException if the array of entries is {@code null}.187 * @throws IllegalArgumentException if the array of entries is empty.188 * @throws NullPointerException if any of the entries in the given array is {@code null}.189 * @throws AssertionError if the given {@code Map} is {@code null}.190 * @throws AssertionError if the given {@code Map} contains any of the given entries.191 */192 public <K, V> void assertDoesNotContain(AssertionInfo info, Map<K, V> actual,193 Map.Entry<? extends K, ? extends V>[] entries) {194 failIfNullOrEmpty(entries);195 assertNotNull(info, actual);196 Set<Map.Entry<? extends K, ? extends V>> found = new LinkedHashSet<>();197 for (Map.Entry<? extends K, ? extends V> entry : entries) {198 if (containsEntry(actual, entry)) {199 found.add(entry);200 }201 }202 if (found.isEmpty()) return;203 throw failures.failure(info, shouldNotContain(actual, entries, found));204 }205 /**206 * Verifies that the actual map contain the given key.207 * 208 * @param info contains information about the assertion.209 * @param actual the given {@code Map}.210 * @param keys the given keys211 * @throws AssertionError if the actual map is {@code null}.212 * @throws AssertionError if the actual map not contains the given key.213 */214 public <K, V> void assertContainsKeys(AssertionInfo info, Map<K, V> actual, @SuppressWarnings("unchecked") K... keys) {215 assertNotNull(info, actual);216 Set<K> notFound = new LinkedHashSet<>();217 for (K key : keys) {218 if (!actual.containsKey(key)) {219 notFound.add(key);220 }221 }222 if (notFound.isEmpty()) return;223 throw failures.failure(info, shouldContainKeys(actual, notFound));224 }225 /**226 * Verifies that the actual map not contains the given key.227 * 228 * @param info contains information about the assertion.229 * @param actual the given {@code Map}.230 * @param key the given key231 * @throws AssertionError if the actual map is {@code null}.232 * @throws AssertionError if the actual map contains the given key.233 */234 public <K, V> void assertDoesNotContainKey(AssertionInfo info, Map<K, V> actual, K key) {235 assertNotNull(info, actual);236 if (actual.containsKey(key)) throw failures.failure(info, shouldNotContainKey(actual, key));237 }238 /**239 * Verifies that the actual map not contains all the given keys.240 *241 * @param info contains information about the assertion.242 * @param actual the given {@code Map}.243 * @param keys the given keys244 * @throws AssertionError if the actual map is {@code null}.245 * @throws AssertionError if the actual map contains all the given keys.246 */247 public <K, V> void assertDoesNotContainKeys(AssertionInfo info, Map<K, V> actual,248 @SuppressWarnings("unchecked") K... keys) {249 assertNotNull(info, actual);250 Set<K> found = new LinkedHashSet<>();251 for (K key : keys) {252 if (key != null && actual.containsKey(key)) {253 found.add(key);254 }255 }256 if (!found.isEmpty()) throw failures.failure(info, shouldNotContainKeys(actual, found));257 }258 /**259 * Verifies that the actual map contains only the given keys and nothing else, in any order.260 * 261 * @param info contains information about the assertion.262 * @param actual the given {@code Map}.263 * @param keys the keys that are expected to be in the given {@code Map}.264 * @throws NullPointerException if the array of keys is {@code null}.265 * @throws IllegalArgumentException if the array of keys is empty.266 * @throws AssertionError if the given {@code Map} is {@code null}.267 * @throws AssertionError if the given {@code Map} does not contain the given keys or if the given {@code Map}268 * contains keys that are not in the given array.269 */270 public <K, V> void assertContainsOnlyKeys(AssertionInfo info, Map<K, V> actual,271 @SuppressWarnings("unchecked") K... keys) {272 assertNotNull(info, actual);273 failIfNull(keys);274 if (actual.isEmpty() && keys.length == 0) {275 return;276 }277 failIfEmpty(keys);278 Set<K> notFound = new LinkedHashSet<>();279 Set<K> notExpected = new LinkedHashSet<>();280 compareActualMapAndExpectedKeys(actual, keys, notExpected, notFound);281 if (!notFound.isEmpty() || !notExpected.isEmpty())282 throw failures.failure(info, shouldContainOnlyKeys(actual, keys, notFound, notExpected));283 }284 /**285 * Verifies that the actual map contain the given value.286 * 287 * @param info contains information about the assertion.288 * @param actual the given {@code Map}.289 * @param value the given value290 * @throws AssertionError if the actual map is {@code null}.291 * @throws AssertionError if the actual map not contains the given value.292 */293 public <K, V> void assertContainsValue(AssertionInfo info, Map<K, V> actual, V value) {294 assertNotNull(info, actual);295 if (!actual.containsValue(value)) throw failures.failure(info, shouldContainValue(actual, value));296 }297 /**298 * Verifies that the actual map contain the given values.299 *300 * @param info contains information about the assertion.301 * @param actual the given {@code Map}.302 * @param values the given values303 * @throws AssertionError if the actual map is {@code null}.304 * @throws AssertionError if the actual map not contains the given values.305 * @throws NullPointerException if values vararg is {@code null}.306 */307 public <K, V> void assertContainsValues(AssertionInfo info, Map<K, V> actual,308 @SuppressWarnings("unchecked") V... values) {309 assertNotNull(info, actual);310 checkNotNull(values, "The array of values to look for should not be null");311 if (actual.isEmpty() && values.length == 0) return;312 //313 Set<V> valuesNotFound = new LinkedHashSet<>();314 for (V valueToLookFor : values) {315 if (!actual.containsValue(valueToLookFor)) valuesNotFound.add(valueToLookFor);316 }317 if (!valuesNotFound.isEmpty()) throw failures.failure(info, shouldContainValues(actual, valuesNotFound));318 }319 /**320 * Verifies that the actual map not contains the given value.321 * 322 * @param info contains information about the assertion.323 * @param actual the given {@code Map}.324 * @param value the given value325 * @throws AssertionError if the actual map is {@code null}.326 * @throws AssertionError if the actual map contains the given value.327 */328 public <K, V> void assertDoesNotContainValue(AssertionInfo info, Map<K, V> actual, V value) {329 assertNotNull(info, actual);330 if (actual.containsValue(value)) throw failures.failure(info, shouldNotContainValue(actual, value));331 }332 /**333 * Verifies that the actual map contains only the given entries and nothing else, in any order.334 * 335 * @param info contains information about the assertion.336 * @param actual the given {@code Map}.337 * @param entries the entries that should be in the actual map.338 * @throws AssertionError if the actual map is {@code null}.339 * @throws NullPointerException if the given entries array is {@code null}.340 * @throws IllegalArgumentException if the given entries array is empty.341 * @throws AssertionError if the actual map does not contain the given entries, i.e. the actual map contains some or342 * none of the given entries, or the actual map contains more entries than the given ones.343 */344 public <K, V> void assertContainsOnly(AssertionInfo info, Map<K, V> actual,345 @SuppressWarnings("unchecked") Map.Entry<? extends K, ? extends V>... entries) {346 doCommonContainsCheck(info, actual, entries);347 if (actual.isEmpty() && entries.length == 0) {348 return;349 }350 failIfEmpty(entries);351 Set<Map.Entry<? extends K, ? extends V>> notFound = new LinkedHashSet<>();352 Set<Map.Entry<? extends K, ? extends V>> notExpected = new LinkedHashSet<>();353 compareActualMapAndExpectedEntries(actual, entries, notExpected, notFound);354 if (!notFound.isEmpty() || !notExpected.isEmpty())355 throw failures.failure(info, shouldContainOnly(actual, entries, notFound, notExpected));356 }357 /**358 * Verifies that the actual map contains only the given entries and nothing else, <b>in order</b>.<br>359 * This assertion should only be used with map that have a consistent iteration order (i.e. don't use it with360 * {@link java.util.HashMap}).361 * 362 * @param info contains information about the assertion.363 * @param actual the given {@code Map}.364 * @param entries the given entries.365 * @throws NullPointerException if the given entries array is {@code null}.366 * @throws AssertionError if the actual map is {@code null}.367 * @throws IllegalArgumentException if the given entries array is empty.368 * @throws AssertionError if the actual map does not contain the given entries with same order, i.e. the actual map369 * contains some or none of the given entries, or the actual map contains more entries than the given ones370 * or entries are the same but the order is not.371 */372 public <K, V> void assertContainsExactly(AssertionInfo info, Map<K, V> actual,373 @SuppressWarnings("unchecked") Map.Entry<? extends K, ? extends V>... entries) {374 doCommonContainsCheck(info, actual, entries);375 if (actual.isEmpty() && entries.length == 0) return;376 failIfEmpty(entries);377 assertHasSameSizeAs(info, actual, entries);378 Set<Map.Entry<? extends K, ? extends V>> notFound = new LinkedHashSet<>();379 Set<Map.Entry<? extends K, ? extends V>> notExpected = new LinkedHashSet<>();380 compareActualMapAndExpectedEntries(actual, entries, notExpected, notFound);381 if (notExpected.isEmpty() && notFound.isEmpty()) {382 // check entries order383 int index = 0;384 for (K keyFromActual : actual.keySet()) {385 if (!areEqual(keyFromActual, entries[index].getKey())) {386 Map.Entry<K, V> actualEntry = entry(keyFromActual, actual.get(keyFromActual));387 throw failures.failure(info, elementsDifferAtIndex(actualEntry, entries[index], index));388 }389 index++;390 }391 // all entries are in the same order.392 return;393 }394 throw failures.failure(info, shouldContainExactly(actual, entries, notFound, notExpected));395 }396 private <K, V> void compareActualMapAndExpectedKeys(Map<K, V> actual, K[] keys, Set<K> notExpected, Set<K> notFound) {397 Map<K, V> actualEntries = new LinkedHashMap<>(actual);398 for (K key : keys) {399 if (actualEntries.containsKey(key)) {400 // this is an expected key401 actualEntries.remove(key);402 } else {403 // this is a not found key404 notFound.add(key);405 }406 }407 // All remaining keys from actual copy are not expected entries.408 for (K key : actualEntries.keySet()) {409 notExpected.add(key);410 }411 }412 private <K, V> void compareActualMapAndExpectedEntries(Map<K, V> actual,413 Map.Entry<? extends K, ? extends V>[] entries,414 Set<Map.Entry<? extends K, ? extends V>> notExpected,415 Set<Map.Entry<? extends K, ? extends V>> notFound) {416 Map<K, V> expectedEntries = entriesToMap(entries);417 Map<K, V> actualEntries = new LinkedHashMap<>(actual);418 for (Map.Entry<K, V> entry : expectedEntries.entrySet()) {419 if (containsEntry(actualEntries, entry(entry.getKey(), entry.getValue()))) {420 // this is an expected entry421 actualEntries.remove(entry.getKey());422 } else {423 // this is a not found entry424 notFound.add(entry(entry.getKey(), entry.getValue()));425 }426 }427 // All remaining entries from actual copy are not expected entries.428 for (Map.Entry<K, V> entry : actualEntries.entrySet()) {429 notExpected.add(entry(entry.getKey(), entry.getValue()));430 }431 }432 private <K, V> void doCommonContainsCheck(AssertionInfo info, Map<K, V> actual,433 Map.Entry<? extends K, ? extends V>[] entries) {434 assertNotNull(info, actual);435 failIfNull(entries);436 }437 private static <K, V> Map<K, V> entriesToMap(Map.Entry<? extends K, ? extends V>[] entries) {438 Map<K, V> expectedEntries = new LinkedHashMap<>();439 for (Map.Entry<? extends K, ? extends V> entry : entries) {440 expectedEntries.put(entry.getKey(), entry.getValue());441 }442 return expectedEntries;443 }444 private static <K> void failIfEmpty(K[] keys) {445 if (keys.length == 0) throw new IllegalArgumentException("The array of keys to look for should not be empty");446 }447 private static <K, V> void failIfEmpty(Map.Entry<? extends K, ? extends V>[] entries) {448 if (entries.length == 0)449 throw new IllegalArgumentException("The array of entries to look for should not be empty");450 }451 private static <K, V> void failIfNullOrEmpty(Map.Entry<? extends K, ? extends V>[] entries) {452 failIfNull(entries);453 failIfEmpty(entries);454 }455 private static <K> void failIfNull(K[] keys) {456 checkNotNull(keys, "The array of keys to look for should not be null");457 }458 private static <K, V> void failIfNull(Map.Entry<? extends K, ? extends V>[] entries) {459 checkNotNull(entries, "The array of entries to look for should not be null");460 }461 private <K, V> boolean containsEntry(Map<K, V> actual, Map.Entry<? extends K, ? extends V> entry) {462 checkNotNull(entry, "Entries to look for should not be null");463 return actual.containsKey(entry.getKey()) ? areEqual(actual.get(entry.getKey()), entry.getValue()) : false;464 }465 private void assertNotNull(AssertionInfo info, Map<?, ?> actual) {466 Objects.instance().assertNotNull(info, actual);467 }468 private static <K, V> void failIfEmptySinceActualIsNotEmpty(Map.Entry<? extends K, ? extends V>[] values) {469 if (values.length == 0) throw new AssertionError("actual is not empty");470 }471}...
failIfNull
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.fail;3import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;4import static org.assertj.core.api.Assertions.failIfNull;5import static org.assertj.core.api.Assertions.failIfNullOrEmpty;6import static org.assertj.core.api.Assertions.failIfNullObject;7import static org.assertj.core.api.Assertions.failIfNullObjectArray;8import static org.assertj.core.api.Assertions.failIfNullOrEmptyObjectArray;9import static org.assertj.core.api.Assertions.failIfNullString;10import static org.assertj.core.api.Assertions.failIfNullOrEmptyString;11import static org.assertj.core.api.Assertions.failIfNullStringArray;12import static org.assertj.core.api.Assertions.failIfNullOrEmptyStringArray;13import static org.assertj.core.api.Assertions.failIfNullObjectArray;14import static org.assertj.core.api.Assertions.failIfNullOrEmptyObjectArray;15import static org.assertj.core.api.Assertions.failIfNullString;16import static org.assertj.core.api.Assertions.failIfNullOrEmptyString;17import static org.assertj.core.api.Assertions.failIfNullStringArray;18import static org.assertj.core.api.Assertions.failIfNullOrEmptyStringArray;19import static org.assertj.core.api.Assertions.failIfNullObjectArray;20import static org.assertj.core.api.Assertions.failIfNullOrEmptyObjectArray;21import java.util.ArrayList;22import java.util.Arrays;23import java.util.HashMap;24import java.util.List;25import java.util.Map;26import org.assertj.core.api.ThrowableAssert.ThrowingCallable;27import org.assertj.core.internal.Maps;28import org.assertj.core.util.Lists;29import org.junit.Test;30public class FailIfNullTest {31 public void failIfNull() {32 failIfNull(null, "test");33 }34 public void failIfNullObject() {35 failIfNullObject(null, "test");36 }37 public void failIfNullString() {38 failIfNullString(null, "test");39 }40 public void failIfNullObjectArray() {41 failIfNullObjectArray(null, "test");42 }43 public void failIfNullStringArray() {44 failIfNullStringArray(null, "test");45 }46 public void failIfNullOrEmpty() {47 failIfNullOrEmpty(null, "test");48 }49 public void failIfNullOrEmptyObjectArray() {50 failIfNullOrEmptyObjectArray(null, "test");
failIfNull
Using AI Code Generation
1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.fail;3import static org.assertj.core.internal.Maps.failIfNull;4import java.util.Map;5import org.junit.Test;6public class Maps_failIfNull_Test {7 public void should_fail_if_map_is_null() {8 Map<?, ?> map = null;9 try {10 failIfNull(info(), map);11 } catch (AssertionError e) {12 assertThat(e).hasMessage(format("%nExpecting actual not to be null"));13 return;14 }15 fail("AssertionError expected");16 }17}18import static org.assertj.core.api.Assertions.assertThat;19import static org.assertj.core.api.Assertions.fail;20import static org.assertj.core.internal.Maps.failIfNullOrEmpty;21import java.util.Map;22import org.junit.Test;23public class Maps_failIfNullOrEmpty_Test {24 public void should_fail_if_map_is_null() {25 Map<?, ?> map = null;26 try {27 failIfNullOrEmpty(info(), map);28 } catch (AssertionError e) {29 assertThat(e).hasMessage(format("%nExpecting actual not to be null"));30 return;31 }32 fail("AssertionError expected");33 }34 public void should_fail_if_map_is_empty() {35 Map<?, ?> map = new HashMap<>();36 try {37 failIfNullOrEmpty(info(), map);38 } catch (AssertionError e) {39 assertThat(e).hasMessage(format("%nExpecting actual not to be empty"));40 return;41 }42 fail("AssertionError expected");43 }44}45import static org.assertj.core.api.Assertions.assertThat;46import static org.assertj.core.api.Assertions.fail;47import static org.assertj.core.internal.Maps.failIfEmpty;48import java.util.Map;49import org.junit.Test;50public class Maps_failIfEmpty_Test {51 public void should_fail_if_map_is_empty() {52 Map<?, ?> map = new HashMap<>();53 try {54 failIfEmpty(info(), map);55 } catch (AssertionError e) {56 assertThat(e).hasMessage(format("%nExpecting actual not to be empty"));57 return;58 }
failIfNull
Using AI Code Generation
1import org.assertj.core.api.MapAssert;2import org.assertj.core.api.Assertions;3import org.assertj.core.internal.Maps;4import java.util.Map;5import java.util.HashMap;6public class MapAssertTest {7 public static void main(String args[]) {8 MapAssert<String, String> mapAssert = new MapAssert<>("Map");9 Map<String, String> map = new HashMap<>();10 map.put("1", "One");11 map.put("2", "Two");12 map.put("3", "Three");13 Maps maps = Maps.instance();14 maps.failIfNull(map);15 System.out.println(mapAssert);16 }17}18{1=One, 2=Two, 3=Three}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!