Search in sources :

Example 6 with MockConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MockConfigurationPropertySource in project spring-boot by spring-projects.

the class NoUnboundElementsBindHandlerTests method bindWhenUsingNoUnboundElementsHandlerShouldBindIfUnboundSystemProperties.

@Test
void bindWhenUsingNoUnboundElementsHandlerShouldBindIfUnboundSystemProperties() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("example.foo", "bar");
    source.put("example.other", "baz");
    this.sources.add(source);
    this.binder = new Binder(this.sources);
    NoUnboundElementsBindHandler handler = new NoUnboundElementsBindHandler(BindHandler.DEFAULT, ((configurationPropertySource) -> false));
    Example bound = this.binder.bind("example", Bindable.of(Example.class), handler).get();
    assertThat(bound.getFoo()).isEqualTo("bar");
}
Also used : Test(org.junit.jupiter.api.Test) ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource) Bindable(org.springframework.boot.context.properties.bind.Bindable) List(java.util.List) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) ConfigurationPropertyName(org.springframework.boot.context.properties.source.ConfigurationPropertyName) BindException(org.springframework.boot.context.properties.bind.BindException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Binder(org.springframework.boot.context.properties.bind.Binder) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) ArrayList(java.util.ArrayList) Binder(org.springframework.boot.context.properties.bind.Binder) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 7 with MockConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MockConfigurationPropertySource in project spring-boot by spring-projects.

the class NoUnboundElementsBindHandlerTests method bindWhenUsingNoUnboundElementsHandlerAndUnboundListElementsShouldThrowException.

@Test
void bindWhenUsingNoUnboundElementsHandlerAndUnboundListElementsShouldThrowException() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("example.foo[0]", "bar");
    this.sources.add(source);
    this.binder = new Binder(this.sources);
    assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.binder.bind("example", Bindable.of(Example.class), new NoUnboundElementsBindHandler())).satisfies((ex) -> assertThat(ex.getCause().getMessage()).contains("The elements [example.foo[0]] were left unbound"));
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 8 with MockConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MockConfigurationPropertySource in project spring-boot by spring-projects.

the class NoUnboundElementsBindHandlerTests method bindWhenUsingNoUnboundElementsHandlerShouldBindIfPrefixDifferent.

@Test
void bindWhenUsingNoUnboundElementsHandlerShouldBindIfPrefixDifferent() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("example.foo", "bar");
    source.put("other.baz", "bar");
    this.sources.add(source);
    this.binder = new Binder(this.sources);
    Example bound = this.binder.bind("example", Bindable.of(Example.class), new NoUnboundElementsBindHandler()).get();
    assertThat(bound.getFoo()).isEqualTo("bar");
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 9 with MockConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MockConfigurationPropertySource in project spring-boot by spring-projects.

the class NoUnboundElementsBindHandlerTests method bindWhenUsingNoUnboundElementsHandlerShouldBindIfUnboundNestedCollectionProperties.

@Test
void bindWhenUsingNoUnboundElementsHandlerShouldBindIfUnboundNestedCollectionProperties() {
    MockConfigurationPropertySource source1 = new MockConfigurationPropertySource();
    source1.put("example.nested[0].string-value", "bar");
    MockConfigurationPropertySource source2 = new MockConfigurationPropertySource();
    source2.put("example.nested[0].string-value", "bar");
    source2.put("example.nested[0].int-value", "2");
    source2.put("example.nested[1].string-value", "baz");
    source2.put("example.nested[1].other-nested.baz", "baz");
    this.sources.add(source1);
    this.sources.add(source2);
    this.binder = new Binder(this.sources);
    NoUnboundElementsBindHandler handler = new NoUnboundElementsBindHandler();
    ExampleWithNestedList bound = this.binder.bind("example", Bindable.of(ExampleWithNestedList.class), handler).get();
    assertThat(bound.getNested().get(0).getStringValue()).isEqualTo("bar");
}
Also used : Binder(org.springframework.boot.context.properties.bind.Binder) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 10 with MockConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MockConfigurationPropertySource in project spring-boot by spring-projects.

the class ValidationBindHandlerTests method bindShouldBindWithoutHandler.

@Test
void bindShouldBindWithoutHandler() {
    this.sources.add(new MockConfigurationPropertySource("foo.age", 4));
    ExampleValidatedBean bean = this.binder.bind("foo", Bindable.of(ExampleValidatedBean.class)).get();
    assertThat(bean.getAge()).isEqualTo(4);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Aggregations

MockConfigurationPropertySource (org.springframework.boot.context.properties.source.MockConfigurationPropertySource)207 Test (org.junit.jupiter.api.Test)204 Map (java.util.Map)18 HashMap (java.util.HashMap)17 LinkedHashMap (java.util.LinkedHashMap)17 ArrayList (java.util.ArrayList)16 List (java.util.List)16 Binder (org.springframework.boot.context.properties.bind.Binder)15 InOrder (org.mockito.InOrder)8 LinkedList (java.util.LinkedList)7 ResolvableType (org.springframework.core.ResolvableType)7 ValidationBindHandler (org.springframework.boot.context.properties.bind.validation.ValidationBindHandler)5 ConfigurationProperty (org.springframework.boot.context.properties.source.ConfigurationProperty)5 StandardEnvironment (org.springframework.core.env.StandardEnvironment)5 ConfigurationPropertyName (org.springframework.boot.context.properties.source.ConfigurationPropertyName)4 LocalDate (java.time.LocalDate)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 BindException (org.springframework.boot.context.properties.bind.BindException)3