Best Assertj code snippet using org.assertj.core.api.AbstractMapAssert
Source:DocumentAssert.java  
...29import java.util.Map;30import java.util.Map.Entry;31import java.util.Set;32import java.util.function.Consumer;33import org.assertj.core.api.AbstractMapAssert;34import org.assertj.core.api.Condition;35import org.assertj.core.error.ShouldContainAnyOf;36import org.assertj.core.internal.Failures;37import org.bson.Document;38import org.bson.conversions.Bson;39import org.springframework.util.Assert;40import org.springframework.util.ObjectUtils;41/**42 * Assertions for Mongo's {@link Document}. Assertions based on keys/entries are translated to document paths allowing43 * to assert nested elements.44 *45 * <pre>46 * <code>47 *  Document document = Document.parse("{ $set: { concreteInnerList: [ { foo: "bar", _class: ⦠}] } }");48 *49 *  assertThat(mappedUpdate).containsKey("$set.concreteInnerList.[0].foo").doesNotContainKey("$set.concreteInnerList.[0].bar");50 * </code>51 * </pre>52 *53 * @author Mark Paluch54 */55public class DocumentAssert extends AbstractMapAssert<DocumentAssert, Map<String, Object>, String, Object> {56	private final Document actual;57	DocumentAssert(Document actual) {58		super(actual, DocumentAssert.class);59		this.actual = actual;60	}61	/*62	 * (non-Javadoc)63	 * @see org.assertj.core.api.AbstractMapAssert#containsEntry(java.lang.Object, java.lang.Object)64	 */65	@Override66	public DocumentAssert containsEntry(String key, Object value) {67		Assert.hasText(key, "The key to look for must not be empty!");68		Lookup<?> lookup = lookup(key);69		if (!lookup.isPathFound() || !ObjectUtils.nullSafeEquals(value, lookup.getValue())) {70			throw Failures.instance().failure(info, AssertErrors.shouldHaveProperty(actual, key, value));71		}72		return myself;73	}74	/*75	 * (non-Javadoc)76	 * @see org.assertj.core.api.AbstractMapAssert#doesNotContainEntry(java.lang.Object, java.lang.Object)77	 */78	@Override79	public DocumentAssert doesNotContainEntry(String key, Object value) {80		Assert.hasText(key, "The key to look for must not be empty!");81		Lookup<?> lookup = lookup(key);82		if (lookup.isPathFound() && ObjectUtils.nullSafeEquals(value, lookup.getValue())) {83			throw Failures.instance().failure(info, AssertErrors.shouldNotHaveProperty(actual, key, value));84		}85		return myself;86	}87	/*88	 * (non-Javadoc)89	 * @see org.assertj.core.api.AbstractMapAssert#containsKey(java.lang.Object)90	 */91	@Override92	public DocumentAssert containsKey(String key) {93		return containsKeys(key);94	}95	/*96	 * (non-Javadoc)97	 * @see org.assertj.core.api.AbstractMapAssert#containsKeys(java.lang.Object[])98	 */99	@Override100	public final DocumentAssert containsKeys(String... keys) {101		Set<String> notFound = new LinkedHashSet<>();102		for (String key : keys) {103			if (!lookup(key).isPathFound()) {104				notFound.add(key);105			}106		}107		if (!notFound.isEmpty()) {108			throw Failures.instance().failure(info, shouldContainKeys(actual, notFound));109		}110		return myself;111	}112	/*113	 * (non-Javadoc)114	 * @see org.assertj.core.api.AbstractMapAssert#doesNotContainKey(java.lang.Object)115	 */116	@Override117	public DocumentAssert doesNotContainKey(String key) {118		return doesNotContainKeys(key);119	}120	/*121	 * (non-Javadoc)122	 * @see org.assertj.core.api.AbstractMapAssert#doesNotContainKeys(java.lang.Object[])123	 */124	@Override125	public final DocumentAssert doesNotContainKeys(String... keys) {126		Set<String> found = new LinkedHashSet<>();127		for (String key : keys) {128			if (lookup(key).isPathFound()) {129				found.add(key);130			}131		}132		if (!found.isEmpty()) {133			throw Failures.instance().failure(info, shouldNotContainKeys(actual, found));134		}135		return myself;136	}137	// override methods to annotate them with @SafeVarargs, we unfortunately can't do that in AbstractMapAssert as it is138	// used in soft assertions which need to be able to proxy method - @SafeVarargs requiring method to be final prevents139	// using proxies.140	/*141	 * (non-Javadoc)142	 * @see org.assertj.core.api.AbstractMapAssert#contains(java.util.Map.Entry[])143	 */144	@SafeVarargs145	@Override146	public final DocumentAssert contains(Map.Entry<? extends String, ? extends Object>... entries) {147		// if both actual and values are empty, then assertion passes.148		if (actual.isEmpty() && entries.length == 0) {149			return myself;150		}151		Set<Map.Entry<? extends String, ? extends Object>> notFound = new LinkedHashSet<>();152		for (Map.Entry<? extends String, ? extends Object> entry : entries) {153			if (!containsEntry(entry)) {154				notFound.add(entry);155			}156		}157		if (!notFound.isEmpty()) {158			throw Failures.instance().failure(info, shouldContain(actual, entries, notFound));159		}160		return myself;161	}162	/*163	 * (non-Javadoc)164	 * @see org.assertj.core.api.AbstractMapAssert#containsAnyOf(java.util.Map.Entry[])165	 */166	@SafeVarargs167	@Override168	public final DocumentAssert containsAnyOf(Map.Entry<? extends String, ? extends Object>... entries) {169		for (Map.Entry<? extends String, ? extends Object> entry : entries) {170			if (containsEntry(entry)) {171				return myself;172			}173		}174		throw Failures.instance().failure(info, ShouldContainAnyOf.shouldContainAnyOf(actual, entries));175	}176	/*177	 * (non-Javadoc)178	 * @see org.assertj.core.api.AbstractMapAssert#containsOnly(java.util.Map.Entry[])179	 */180	@SafeVarargs181	@Override182	public final DocumentAssert containsOnly(Map.Entry<? extends String, ? extends Object>... entries) {183		throw new UnsupportedOperationException();184	}185	/*186	 * (non-Javadoc)187	 * @see org.assertj.core.api.AbstractMapAssert#doesNotContain(java.util.Map.Entry[])188	 */189	@SafeVarargs190	@Override191	public final DocumentAssert doesNotContain(Map.Entry<? extends String, ? extends Object>... entries) {192		Set<Map.Entry<? extends String, ? extends Object>> found = new LinkedHashSet<>();193		for (Map.Entry<? extends String, ? extends Object> entry : entries) {194			if (containsEntry(entry)) {195				found.add(entry);196			}197		}198		if (!found.isEmpty()) {199			throw Failures.instance().failure(info, shouldNotContain(actual, entries, found));200		}201		return myself;202	}203	/*204	 * (non-Javadoc)205	 * @see org.assertj.core.api.AbstractMapAssert#containsExactly(java.util.Map.Entry[])206	 */207	@SafeVarargs208	@Override209	public final DocumentAssert containsExactly(Map.Entry<? extends String, ? extends Object>... entries) {210		throw new UnsupportedOperationException();211	}212	private boolean containsEntry(Entry<? extends String, ?> entry) {213		Lookup<?> lookup = lookup(entry.getKey());214		return lookup.isPathFound() && ObjectUtils.nullSafeEquals(entry.getValue(), lookup.getValue());215	}216	private <T> Lookup<T> lookup(String path) {217		return lookup(actual, path);218	}219	@SuppressWarnings("unchecked")220	private static <T> Lookup<T> lookup(Bson source, String path) {221		String[] fragments = path.split("(?<!\\\\)\\.");222		if (fragments.length == 1) {223			Document document = (Document) source;224			String pathToUse = path.replace("\\.", ".");225			if (document.containsKey(pathToUse)) {226				return Lookup.found((T) document.get(pathToUse));227			}228			return Lookup.notFound();229		}230		Iterator<String> it = Arrays.asList(fragments).iterator();231		Object current = source;232		while (it.hasNext()) {233			String key = it.next().replace("\\.", ".");234			if (!(current instanceof Bson) && !key.startsWith("[")) {235				return Lookup.found(null);236			}237			if (key.startsWith("[")) {238				String indexNumber = key.substring(1, key.indexOf("]"));239				if (current instanceof List) {240					current = ((List) current).get(Integer.valueOf(indexNumber));241				}242				if (!it.hasNext()) {243					return Lookup.found((T) current);244				}245			} else {246				if (current instanceof Document) {247					Document document = (Document) current;248					if (!it.hasNext() && !document.containsKey(key)) {249						return Lookup.notFound();250					}251					current = document.get(key);252				}253				if (!it.hasNext()) {254					return Lookup.found((T) current);255				}256			}257		}258		return Lookup.notFound();259	}260	/*261	 * (non-Javadoc)262	 * @see org.assertj.core.api.AbstractMapAssert#hasEntrySatisfying(java.lang.Object, org.assertj.core.api.Condition)263	 */264	@Override265	public DocumentAssert hasEntrySatisfying(String key, Condition<? super Object> valueCondition) {266		Lookup<Object> value = lookup(key);267		if (!value.isPathFound() || !valueCondition.matches(value.getValue())) {268			throw Failures.instance().failure(info, elementsShouldBe(actual, value, valueCondition));269		}270		return myself;271	}272	/*273	 * (non-Javadoc)274	 * @see org.assertj.core.api.AbstractMapAssert#hasEntrySatisfying(java.lang.Object, java.util.function.Consumer)275	 */276	@Override277	public DocumentAssert hasEntrySatisfying(String key, Consumer<? super Object> valueRequirements) {278		containsKey(key);279		valueRequirements.accept(lookup(key).getValue());280		return myself;281	}282	@RequiredArgsConstructor(access = AccessLevel.PRIVATE)283	@Getter284	static class Lookup<T> {285		private final T value;286		private final boolean pathFound;287		/**288		 * Factory method to construct a lookup with a hit....AbstractMapAssert
Using AI Code Generation
1import org.assertj.core.api.AbstractMapAssert;2import org.assertj.core.api.MapAssert;3import org.assertj.core.api.MapEntryAssert;4import org.assertj.core.api.MapEntryListAssert;5import org.assertj.core.api.MapEntrySetAssert;6import org.assertj.core.api.MapListAssert;7import org.assertj.core.api.MapSetAssert;8import org.assertj.core.api.MapSizeAssert;9import org.assertj.core.api.MapValueAssert;10import org.assertj.core.api.ObjectAssert;11import org.assertj.core.api.ObjectArrayAssert;12import org.assertj.core.api.ObjectEnumerableAssert;13import org.assertj.core.api.ObjectListAssert;14import org.assertj.core.api.ObjectMapAssert;15import org.assertj.core.api.ObjectSetAssert;16import org.assertj.core.api.ObjectSizeAssert;17import org.assertj.core.api.ObjectValueAssert;18import org.assertj.core.api.AbstractMapAssert;19import org.assertj.core.api.MapAssert;20import org.assertj.core.api.MapEntryAssert;21import org.assertj.core.api.MapEntryListAssert;22import org.assertj.core.api.MapEntrySetAssert;23import org.assertj.core.api.MapListAssert;24import org.assertj.core.api.MapSetAssert;25import org.assertj.core.api.MapSizeAssert;26import org.assertj.core.api.MapValueAssert;27import org.assertj.core.api.ObjectAssert;28import org.assertj.core.api.ObjectArrayAssert;29import org.assertj.core.api.ObjectEnumerableAssert;30import org.assertj.core.api.ObjectListAssert;31import org.assertj.core.api.ObjectMapAssert;32import org.assertj.core.api.ObjectSetAssert;33import org.assertj.core.api.ObjectSizeAssert;34import org.assertj.core.api.ObjectValueAssert;35import org.assertj.core.api.AbstractMapAssert;36import org.assertj.core.api.MapAssert;37import org.assertj.core.api.MapEntryAssert;38import org.assertj.core.api.MapEntryListAssert;39import org.assertj.core.api.MapEntrySetAssert;40import org.assertj.core.api.MapListAssert;41import org.assertj.core.api.MapSetAssert;42import org.assertj.core.api.MapSizeAssert;43import org.assertj.core.api.MapValueAssert;44import org.assertj.core.api.ObjectAssert;45import org.assertj.core.api.ObjectArrayAssert;46import org.assertj.core.api.ObjectEnumerableAssert;47import org.assertj.core.api.ObjectListAssert;48import org.assertj.core.api.ObjectMapAssert;49import org.assertj.core.api.ObjectSetAssert;50import org.assertj.core.api.ObjectSizeAssert;51import org.assertj.core.api.ObjectValueAssert;52import org.assertj.core.api.AbstractMapAssert;53import org.assertj.core.api.MapAssert;54import org.assertj.core.api.MapEntryAssert;55import org.assertj.core.api.MapEntryListAssert;56import org.assertjAbstractMapAssert
Using AI Code Generation
1import org.assertj.core.api.AbstractMapAssert;2import org.assertj.core.api.MapAssert;3import org.assertj.core.api.MapEntryAssert;4import org.assertj.core.api.MapEntryCondition;5import org.assertj.core.api.MapEntryCondition.Entry;6import org.assertj.core.api.MapEntryConditionFactory;7import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory1;8import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory2;9import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory3;10import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory4;11import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory5;12import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory6;13import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory7;14import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory8;15import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory9;16import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory10;17import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory11;18import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory12;19import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory13;20import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory14;21import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory15;22import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory16;23import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory17;24import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory18;25import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory19;26import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory20;27import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory21;28import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory22;29import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory23;30import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory24;31import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory25;32import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory26;33import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory27;34import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory28;35import org.assertj.core.api.MapEntryConditionFactory.MapEntryConditionFactory29;36import org.assertj.core.api.MapEntryConditionAbstractMapAssert
Using AI Code Generation
1import static org.assertj.core.api.Assertions.*;2import java.util.HashMap;3public class AbstractMapAssertExample {4	public static void main(String[] args) {5		HashMap<String, Integer> map = new HashMap<>();6		map.put("one", 1);7		map.put("two", 2);8		map.put("three", 3);9		map.put("four", 4);10		map.put("five", 5);11		assertThat(map).containsKey("one").containsValue(1).containsEntry("two", 2).doesNotContainKey("ten")12				.doesNotContainValue(10).doesNotContainEntry("ten", 10).hasSize(5).isEmpty();13	}14}15     but: was <{"one"=1, "two"=2, "three"=3, "four"=4, "five"=5}>16	at org.junit.Assert.assertEquals(Assert.java:115)17	at org.junit.Assert.assertEquals(Assert.java:144)18	at org.assertj.core.api.AbstractMapAssertTest.testIsEmpty(AbstractMapAssertTest.java:90)19	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)20	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)21	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)22	at java.lang.reflect.Method.invoke(Method.java:498)23	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)24	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)25	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)26	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)27	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)28	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)29	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)30	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)31	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)32	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)33	at org.junit.runners.ParentRunner$1.schedule(ParentRunnerAbstractMapAssert
Using AI Code Generation
1import org.assertj.core.api.AbstractMapAssert;2import java.util.Map;3import org.junit.jupiter.api.Assertions;4public class Main {5    public static void main(String[] args) {6        Map<Integer, String> map = Map.of(1, "one", 2, "two", 3, "three");7        AbstractMapAssert<?, ?> assertions = Assertions.assertThat(map);8        System.out.println(assertions);9    }10}11Java 9 - Map.of() Method12Java 9 - Map.ofEntries() Method13Java 9 - Map.entry() Method14Java 9 - Map.getOrDefault() Method15Java 9 - Map.remove() Method16Java 9 - Map.merge() Method17Java 9 - Map.putIfAbsent() Method18Java 9 - Map.replace() Method19Java 9 - Map.replace() Method20Java 9 - Map.computeIfPresent() Method21Java 9 - Map.computeIfAbsent() Method22Java 9 - Map.compute() Method23Java 9 - Map.forEach() Method24Java 9 - Map.keySet() Method25Java 9 - Map.values() Method26Java 9 - Map.entrySet() Method27Java 9 - Map.getOrDefault() Method28Java 9 - Map.remove() Method29Java 9 - Map.merge() Method30Java 9 - Map.putIfAbsent() Method31Java 9 - Map.replace() Method32Java 9 - Map.replace() Method33Java 9 - Map.computeIfPresent() Method34Java 9 - Map.computeIfAbsent() Method35Java 9 - Map.compute() Method36Java 9 - Map.forEach() Method37Java 9 - Map.keySet() Method38Java 9 - Map.values() Method39Java 9 - Map.entrySet() Method40Java 9 - Map.getOrDefault() Method41Java 9 - Map.remove() Method42Java 9 - Map.merge() Method43Java 9 - Map.putIfAbsent() Method44Java 9 - Map.replace() Method45Java 9 - Map.replace() Method46Java 9 - Map.computeIfPresent() Method47Java 9 - Map.computeIfAbsent() Method48Java 9 - Map.compute() MethodAbstractMapAssert
Using AI Code Generation
1import static org.assertj.core.api.Assertions.*;2import java.util.*;3public class AbstractMapAssertExample {4  public static void main(String[] args) {5    Map<String,String> map = new HashMap<>();6    map.put("key1", "value1");7    map.put("key2", "value2");8    map.put("key3", "value3");9    assertThat(map).containsKey("key1");10    assertThat(map).containsValue("value1");11  }12}AbstractMapAssert
Using AI Code Generation
1import org.assertj.core.api.AbstractMapAssert;2import static org.assertj.core.api.Assertions.*;3public class MyMapAssert extends AbstractMapAssert<MyMapAssert, Map<?,?>, Object, Object> {4public MyMapAssert(Map<?, ?> actual) {5super(actual, MyMapAssert.class);6}7public static MyMapAssert assertThat(Map<?, ?> actual) {8return new MyMapAssert(actual);9}10public MyMapAssert isEmpty() {11isEmpty();12return this;13}14}15import org.assertj.core.api.Assertions;16import static org.assertj.core.api.Assertions.*;17public class MyMapAssertTest {18public void testMyMapAssert() {19Map<String, String> map = new HashMap<>();20map.put("key1", "value1");21map.put("key2", "value2");22Assertions.assertThat(map).isEmpty();23}24}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!!
