diff --git a/serenity-core/src/main/java/net/thucydides/core/matchers/BeanMatcherAsserts.java b/serenity-core/src/main/java/net/thucydides/core/matchers/BeanMatcherAsserts.java index 51fc9be4fc..b66a06ff99 100644 --- a/serenity-core/src/main/java/net/thucydides/core/matchers/BeanMatcherAsserts.java +++ b/serenity-core/src/main/java/net/thucydides/core/matchers/BeanMatcherAsserts.java @@ -112,6 +112,14 @@ public static void shouldMatch(T bean, BeanMatcher... matchers) { } } + public static void shouldNotMatch(List items, BeanMatcher... matchers) { + if (matches(items, matchers)) { + throw new AssertionError("Found unneeded matching elements for " + join(matchers) + + NEW_LINE + +"Elements where " + join(items)); + } + } + private static String descriptionOf(Object bean) { if (isAMap(bean)) { diff --git a/serenity-core/src/test/java/net/thucydides/core/matchers/WhenMatchingPropertyValueCollections.java b/serenity-core/src/test/java/net/thucydides/core/matchers/WhenMatchingPropertyValueCollections.java index fed4713fe6..81c19f2644 100644 --- a/serenity-core/src/test/java/net/thucydides/core/matchers/WhenMatchingPropertyValueCollections.java +++ b/serenity-core/src/test/java/net/thucydides/core/matchers/WhenMatchingPropertyValueCollections.java @@ -275,6 +275,46 @@ public void should_check_field_uniqueness_when_not_unique() { shouldMatch(persons, containsTwoEntries, firstNameIsBill, lastNamesAreDifferent); } + @Test + public void should_check_field_similarities() { + List persons = Arrays.asList(billoddie, tim, graeme, billoddie); + + BeanMatcher containsTwoEntries = the_count(is(2)); + BeanMatcher lastNamesAreDifferent = each("lastName").isDifferent(); + BeanMatcher firstNameIsBill = the("firstName", is("Bill")); + + shouldNotMatch(persons, containsTwoEntries, firstNameIsBill, lastNamesAreDifferent); + } + + @Test(expected = AssertionError.class) + public void should_check_field_similarities_when_all_unique() { + List persons = Arrays.asList(billoddie, tim, graeme, billkidd); + + BeanMatcher containsTwoEntries = the_count(is(2)); + BeanMatcher lastNamesAreDifferent = each("lastName").isDifferent(); + BeanMatcher firstNameIsBill = the("firstName", is("Bill")); + + shouldNotMatch(persons, containsTwoEntries, firstNameIsBill, lastNamesAreDifferent); + } + + @Test + public void should_check_element_is_not_present() { + List persons = Arrays.asList(billoddie, tim, graeme, billoddie); + + BeanMatcher firstNameIsJohn = the("firstName", is("John")); + + shouldNotMatch(persons, firstNameIsJohn); + } + + @Test(expected = AssertionError.class) + public void should_check_element_is_not_present_with_failing_case() { + List persons = Arrays.asList(billoddie, tim, graeme, billkidd); + + BeanMatcher firstNameIsBill = the("firstName", is("Bill")); + + shouldNotMatch(persons, firstNameIsBill); + } + @Test public void should_check_multiple_different_types_of_matches() { List persons = Arrays.asList(billoddie, tim, graeme, billkidd); diff --git a/serenity-core/src/test/java/net/thucydides/core/matchers/WhenMatchingPropertyValuesWithMaps.java b/serenity-core/src/test/java/net/thucydides/core/matchers/WhenMatchingPropertyValuesWithMaps.java index adb87c9d19..e20db5c44f 100644 --- a/serenity-core/src/test/java/net/thucydides/core/matchers/WhenMatchingPropertyValuesWithMaps.java +++ b/serenity-core/src/test/java/net/thucydides/core/matchers/WhenMatchingPropertyValuesWithMaps.java @@ -192,5 +192,26 @@ public void should_check_field_uniqueness() { BeanMatcherAsserts.shouldMatch(persons, containsTwoEntries, firstNameIsBill, lastNamesAreDifferent); } + @Test + public void should_check_field_absence() { + List> persons = Arrays.asList(mappedPerson("Bill", "Oddie"), + mappedPerson("Bill", "Kidd"), + mappedPerson("Graeam", "Garden"), + mappedPerson("Tim", "Brooke-Taylor")); + + BeanMatcher firstNameIsJohn = BeanMatchers.the("firstName", is("John")); + BeanMatcherAsserts.shouldNotMatch(persons, firstNameIsJohn); + } + + @Test(expected = AssertionError.class) + public void should_check_field_absence_with_failing_case() { + List> persons = Arrays.asList(mappedPerson("Bill", "Oddie"), + mappedPerson("Bill", "Kidd"), + mappedPerson("Graeam", "Garden"), + mappedPerson("Tim", "Brooke-Taylor")); + + BeanMatcher firstNameIsBill = BeanMatchers.the("firstName", is("Bill")); + BeanMatcherAsserts.shouldNotMatch(persons, firstNameIsBill); + } }