How to use failIfNullOrEmpty method of org.assertj.core.internal.Maps class

Best Assertj code snippet using org.assertj.core.internal.Maps.failIfNullOrEmpty

Source:Maps.java Github

copy

Full Screen

...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) {...

Full Screen

Full Screen

failIfNullOrEmpty

Using AI Code Generation

copy

Full Screen

1Maps maps = new Maps();2Map<String, String> map = new HashMap<>();3map.put("key", "value");4maps.failIfNullOrEmpty(info(), map);5Map<String, String> map = new HashMap<>();6map.put("key", "value");7Assertions.assertThat(map).failIfNullOrEmpty();8Map<String, String> map = new HashMap<>();9map.put("key", "value");10Assertions.assertThat(map).failIfNullOrEmpty();

Full Screen

Full Screen

failIfNullOrEmpty

Using AI Code Generation

copy

Full Screen

1Map < String , String > map = new HashMap < > ( ) ; 2map . put ( "key" , "value" ) ; 3assertThat ( map ) . failIfNullOrEmpty ( ) ; 4Map < String , String > map = new HashMap < > ( ) ; 5map . put ( "key" , "value" ) ; 6assertThat ( map ) . isEmpty ( ) ;

Full Screen

Full Screen

failIfNullOrEmpty

Using AI Code Generation

copy

Full Screen

1public void test1() {2 Map<String, String> map = new HashMap<>();3 map.put("key1", "value1");4 map.put("key2", "value2");5 assertThat(map).failIfNullOrEmpty();6}7public void test2() {8 Map<String, String> map = new HashMap<>();9 assertThat(map).failIfNullOrEmpty();10}11public void test3() {12 Map<String, String> map = null;13 assertThat(map).failIfNullOrEmpty();14}15public void test4() {16 Map<String, String> map = new HashMap<>();17 map.put("key1", "value1");18 map.put("key2", "value2");19 org.assertj.core.api.MapAssert<String, String> mapAssert = assertThat(map);20 mapAssert.failIfNullOrEmpty();21}22public void test5() {23 Map<String, String> map = new HashMap<>();24 org.assertj.core.api.MapAssert<String, String> mapAssert = assertThat(map);25 mapAssert.failIfNullOrEmpty();26}27public void test6() {28 Map<String, String> map = null;29 org.assertj.core.api.MapAssert<String, String> mapAssert = assertThat(map);30 mapAssert.failIfNullOrEmpty();31}32public void test7() {33 Map<String, String> map = new HashMap<>();34 map.put("key1", "value1");35 map.put("key2", "value2");36 org.assertj.core.api.AbstractMapAssert<?, ?, ?> abstractMapAssert = assertThat(map);

Full Screen

Full Screen

failIfNullOrEmpty

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.*;2import org.assertj.core.api.Assertions.*;3import org.assertj.core.internal.*;4import java.util.*;5public class AssertjTest {6 public static void main(String[] args) {7 AssertjTest test = new AssertjTest();8 test.testForNullMap();9 test.testForEmptyMap();10 test.testForNonEmptyMap();11 }12 public void testForNullMap() {13 try {14 Maps maps = new Maps();15 maps.failIfNullOrEmpty(null);16 } catch (AssertionError e) {17 System.out.println("AssertionError: " + e.getMessage());18 }19 }20 public void testForEmptyMap() {21 try {22 Maps maps = new Maps();23 maps.failIfNullOrEmpty(new HashMap<>());24 } catch (AssertionError e) {25 System.out.println("AssertionError: " + e.getMessage());26 }27 }28 public void testForNonEmptyMap() {29 Maps maps = new Maps();30 maps.failIfNullOrEmpty(new HashMap<String, String>() {{31 put("key1", "value1");32 }});33 System.out.println("Test passed");34 }35}

Full Screen

Full Screen

failIfNullOrEmpty

Using AI Code Generation

copy

Full Screen

1package com.mycompany.app;2import org.junit.Test;3import org.assertj.core.internal.Maps;4import java.util.Map;5import java.util.HashMap;6public class AppTest {7 public void testApp() {8 Map<String, String> map = new HashMap<String, String>();9 map.put("key1", "value1");10 map.put("key2", "value2");11 Maps maps = new Maps();12 maps.failIfNullOrEmpty(map);13 }14}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful