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 4e93b12359..648af6cf01 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 213b03c42d..91e183da07 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; @@ -277,6 +289,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<>(); diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java index 466069cc1b..1d71732a52 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java @@ -16,18 +16,16 @@ package org.springframework.cloud.contract.wiremock; -import com.github.tomakehurst.wiremock.WireMockServer; -import com.github.tomakehurst.wiremock.client.WireMock; -import com.github.tomakehurst.wiremock.common.Slf4jNotifier; -import com.github.tomakehurst.wiremock.core.Options; - import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import javax.annotation.PostConstruct; +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.common.Slf4jNotifier; +import com.github.tomakehurst.wiremock.core.Options; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -43,10 +41,6 @@ import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.util.Assert; import org.springframework.util.StreamUtils; import org.springframework.util.StringUtils; - -import com.github.tomakehurst.wiremock.WireMockServer; -import com.github.tomakehurst.wiremock.client.WireMock; -import com.github.tomakehurst.wiremock.core.Options; import org.springframework.web.client.RestTemplate; /**