From 0759652b24c96259137278f871201e95b88bf9b5 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Fri, 28 Apr 2017 12:01:56 -0700 Subject: [PATCH] Tests for missing placeholders during binding Add a test to ensure that that there is a hard failure when trying to bind properties with missing placeholders. Closes gh-8693 --- .../boot/context/properties/bind/BinderTests.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java index 9706f6e067..2adae96fa2 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java @@ -24,6 +24,7 @@ import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.internal.matchers.ThrowableMessageMatcher; import org.junit.rules.ExpectedException; import org.mockito.Answers; import org.mockito.InOrder; @@ -38,6 +39,7 @@ import org.springframework.format.annotation.DateTimeFormat; import org.springframework.test.context.support.TestPropertySourceUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -136,6 +138,18 @@ public class BinderTests { assertThat(result.get()).isEqualTo(123); } + @Test + public void bindToValueWithMissingPlaceholdersShouldThrowException() throws Exception { + StandardEnvironment environment = new StandardEnvironment(); + this.sources.add(new MockConfigurationPropertySource("foo", "${bar}")); + this.binder = new Binder(this.sources, + new PropertySourcesPlaceholdersResolver(environment)); + this.thrown.expect(BindException.class); + this.thrown.expectCause(ThrowableMessageMatcher.hasMessage( + containsString("Could not resolve placeholder 'bar' in value \"${bar}\""))); + this.binder.bind("foo", Bindable.of(Integer.class)); + } + @Test public void bindToValueShouldTriggerOnSuccess() throws Exception { this.sources.add(new MockConfigurationPropertySource("foo", "1", "line1"));