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
pull/9042/head
Madhura Bhave 8 years ago
parent 76593a3806
commit 0759652b24

@ -24,6 +24,7 @@ import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.internal.matchers.ThrowableMessageMatcher;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.mockito.Answers; import org.mockito.Answers;
import org.mockito.InOrder; import org.mockito.InOrder;
@ -38,6 +39,7 @@ import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
@ -136,6 +138,18 @@ public class BinderTests {
assertThat(result.get()).isEqualTo(123); 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 @Test
public void bindToValueShouldTriggerOnSuccess() throws Exception { public void bindToValueShouldTriggerOnSuccess() throws Exception {
this.sources.add(new MockConfigurationPropertySource("foo", "1", "line1")); this.sources.add(new MockConfigurationPropertySource("foo", "1", "line1"));

Loading…
Cancel
Save