Skip to content

Commit

Permalink
feat: add ability to assert that matched element is not present in co…
Browse files Browse the repository at this point in the history
…llection

now it is possible to assert that element is not present in collection using BeanMatcherAsserts.shouldNotMatch(List<T> items, BeanMatcher... matchers) method
  • Loading branch information
viktor-klymenko committed Jun 15, 2016
1 parent 3e547d4 commit 1010a8d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ public static <T> void shouldMatch(T bean, BeanMatcher... matchers) {
}
}

public static <T> void shouldNotMatch(List<T> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person> 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<Person> 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<Person> 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<Person> 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<Person> persons = Arrays.asList(billoddie, tim, graeme, billkidd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,26 @@ public void should_check_field_uniqueness() {
BeanMatcherAsserts.shouldMatch(persons, containsTwoEntries, firstNameIsBill, lastNamesAreDifferent);
}

@Test
public void should_check_field_absence() {
List<Map<String,String>> 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<Map<String,String>> 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);
}

}

0 comments on commit 1010a8d

Please sign in to comment.