From e5b0a9dabcb40ec4b51ff3b70f3269e5182ac4e1 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 8 Aug 2018 18:09:27 +0200 Subject: [PATCH] Added better exception for null collection elements; fixes gh-703 --- .../verifier/assertion/CollectionAssert.java | 9 ++++++++- .../assertion/CollectionAssertTests.java | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/CollectionAssert.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/CollectionAssert.java index 5d6b746318..d5a44bb4b3 100644 --- a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/CollectionAssert.java +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/CollectionAssert.java @@ -30,14 +30,21 @@ public class CollectionAssert extends IterableAssert { isNotNull(); isNotEmpty(); for (Object anActual : this.actual) { + if (anActual == null) { + failWithMessageRelatedToRegex(regex, anActual); + } String value = anActual.toString(); if (!value.matches(regex)) { - failWithMessage("The value <%s> doesn't match the regex <%s>", value, regex); + failWithMessageRelatedToRegex(regex, value); } } return this; } + private void failWithMessageRelatedToRegex(String regex, Object value) { + failWithMessage("The value <%s> doesn't match the regex <%s>", value, regex); + } + /** * Flattens the collection and checks whether size is greater than or equal to the provided value * @param size - the flattened collection should have size greater than or equal to this value diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/assertion/CollectionAssertTests.java b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/assertion/CollectionAssertTests.java index ef6b74ad09..5e2fa07a94 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/assertion/CollectionAssertTests.java +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/assertion/CollectionAssertTests.java @@ -33,6 +33,18 @@ public class CollectionAssertTests { } } + @Test + public void should_throw_an_exception_when_element_is_null() { + Collection collection = collectionWithNulls(); + + try { + SpringCloudContractAssertions.assertThat(collection).allElementsMatch("[0-9]"); + Assertions.fail("should throw exception"); + } catch (AssertionError e) { + Assertions.assertThat(e).hasMessageContaining("The value doesn't match the regex <[0-9]>"); + } + } + @Test public void should_throw_an_exception_when_collection_is_null() { Collection collection = null; @@ -265,6 +277,12 @@ public class CollectionAssertTests { return list; } + private Collection collectionWithNulls() { + List list = new ArrayList<>(); + list.add(null); + return list; + } + private Collection nestedCollection() { List list = new ArrayList<>(); List list1 = new ArrayList<>();