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

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

Source:Maps_assertContainsOnlyKeys_Test.java Github

copy

Full Screen

...25import org.junit.jupiter.api.Test;26import org.mockito.Mockito;27/**28 * Tests for29 * <code>{@link org.assertj.core.internal.Maps#assertContainsOnlyKeys(org.assertj.core.api.AssertionInfo, java.util.Map, java.lang.Object...)}</code>30 * .31 *32 * @author Christopher Arnott33 */34public class Maps_assertContainsOnlyKeys_Test extends MapsBaseTest {35 private static final String ARRAY_OF_KEYS = "array of keys";36 @Test37 public void should_fail_if_actual_is_null() {38 Assertions.assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> maps.assertContainsOnlyKeys(someInfo(), null, "name")).withMessage(FailureMessages.actualIsNull());39 }40 @Test41 public void should_fail_if_given_keys_array_is_null() {42 Assertions.assertThatNullPointerException().isThrownBy(() -> maps.assertContainsOnlyKeys(someInfo(), actual, ((String[]) (null)))).withMessage(ErrorMessages.keysToLookForIsNull(Maps_assertContainsOnlyKeys_Test.ARRAY_OF_KEYS));43 }44 @Test45 public void should_fail_if_given_keys_array_is_empty() {46 Assertions.assertThatIllegalArgumentException().isThrownBy(() -> maps.assertContainsOnlyKeys(someInfo(), actual, emptyKeys())).withMessage(ErrorMessages.keysToLookForIsEmpty(Maps_assertContainsOnlyKeys_Test.ARRAY_OF_KEYS));47 }48 @Test49 public void should_pass_if_actual_and_given_keys_are_empty() {50 maps.assertContainsOnlyKeys(TestData.someInfo(), Collections.emptyMap(), ((Object[]) (MapsBaseTest.emptyKeys())));51 }52 @Test53 public void should_pass_if_actual_contains_only_expected_keys() {54 maps.assertContainsOnlyKeys(TestData.someInfo(), actual, "color", "name");55 maps.assertContainsOnlyKeys(TestData.someInfo(), actual, "name", "color");56 }57 @Test58 public void should_fail_if_actual_contains_an_unexpected_key() {59 AssertionInfo info = TestData.someInfo();60 String[] expectedKeys = new String[]{ "name" };61 try {62 maps.assertContainsOnlyKeys(info, actual, expectedKeys);63 } catch (AssertionError e) {64 Mockito.verify(failures).failure(info, ShouldContainOnlyKeys.shouldContainOnlyKeys(actual, expectedKeys, Collections.emptySet(), Maps_assertContainsOnlyKeys_Test.newHashSet("color")));65 return;66 }67 Assertions.shouldHaveThrown(AssertionError.class);68 }69 @Test70 public void should_fail_if_actual_does_not_contains_all_expected_keys() {71 AssertionInfo info = TestData.someInfo();72 String[] expectedKeys = new String[]{ "name", "color" };73 Map<String, String> underTest = Maps.mapOf(MapEntry.entry("name", "Yoda"));74 try {75 maps.assertContainsOnlyKeys(info, underTest, expectedKeys);76 } catch (AssertionError e) {77 Mockito.verify(failures).failure(info, ShouldContainOnlyKeys.shouldContainOnlyKeys(underTest, expectedKeys, Maps_assertContainsOnlyKeys_Test.newHashSet("color"), Collections.emptySet()));78 return;79 }80 Assertions.shouldHaveThrown(AssertionError.class);81 }82 @Test83 public void should_fail_if_actual_does_not_contains_all_expected_keys_and_contains_unexpected_one() {84 AssertionInfo info = TestData.someInfo();85 String[] expectedKeys = new String[]{ "name", "color" };86 Map<String, String> underTest = Maps.mapOf(MapEntry.entry("name", "Yoda"), MapEntry.entry("job", "Jedi"));87 try {88 maps.assertContainsOnlyKeys(info, underTest, expectedKeys);89 } catch (AssertionError e) {90 Mockito.verify(failures).failure(info, ShouldContainOnlyKeys.shouldContainOnlyKeys(underTest, expectedKeys, Maps_assertContainsOnlyKeys_Test.newHashSet("color"), Maps_assertContainsOnlyKeys_Test.newHashSet("job")));91 return;92 }93 Assertions.shouldHaveThrown(AssertionError.class);94 }95}...

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