Search in sources :

Example 86 with MockConfigurationPropertySource

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

the class BoundPropertiesTrackingBindHandlerTests method handlerShouldCallRecordBindingIfConfigurationPropertyIsNotNull.

@Test
void handlerShouldCallRecordBindingIfConfigurationPropertyIsNotNull() {
    this.sources.add(new MockConfigurationPropertySource("foo.age", 4));
    this.binder.bind("foo", Bindable.of(ExampleBean.class), this.handler);
    then(this.consumer).should().accept(any(ConfigurationProperty.class));
    then(this.consumer).should(never()).accept(null);
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 87 with MockConfigurationPropertySource

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

the class CollectionBinderTests method bindToCollectionWithNoDefaultConstructor.

@Test
void bindToCollectionWithNoDefaultConstructor() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.items", "a,b,c,c");
    this.sources.add(source);
    ExampleCustomNoDefaultConstructorBean result = this.binder.bind("foo", ExampleCustomNoDefaultConstructorBean.class).get();
    assertThat(result.getItems()).hasSize(4);
    assertThat(result.getItems()).containsExactly("a", "b", "c", "c");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 88 with MockConfigurationPropertySource

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

the class CollectionBinderTests method bindToCollectionWithDefaultConstructor.

@Test
void bindToCollectionWithDefaultConstructor() {
    // gh-12322
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.items", "a,b,c,c");
    this.sources.add(source);
    ExampleCustomWithDefaultConstructorBean result = this.binder.bind("foo", ExampleCustomWithDefaultConstructorBean.class).get();
    assertThat(result.getItems()).hasSize(4);
    assertThat(result.getItems()).containsExactly("a", "b", "c", "c");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 89 with MockConfigurationPropertySource

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

the class CollectionBinderTests method bindToCollectionWhenMultipleSourceShouldOnlyUseFirst.

@Test
void bindToCollectionWhenMultipleSourceShouldOnlyUseFirst() {
    MockConfigurationPropertySource source1 = new MockConfigurationPropertySource();
    source1.put("bar", "baz");
    this.sources.add(source1);
    MockConfigurationPropertySource source2 = new MockConfigurationPropertySource();
    source2.put("foo[0]", "1");
    source2.put("foo[1]", "2");
    this.sources.add(source2);
    MockConfigurationPropertySource source3 = new MockConfigurationPropertySource();
    source3.put("foo[0]", "7");
    source3.put("foo[1]", "8");
    source3.put("foo[2]", "9");
    this.sources.add(source3);
    List<Integer> result = this.binder.bind("foo", INTEGER_LIST).get();
    assertThat(result).containsExactly(1, 2);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 90 with MockConfigurationPropertySource

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

the class CollectionBinderTests method bindToBeanWithNestedCollectionAndNonIterableSourceShouldNotFail.

@Test
void bindToBeanWithNestedCollectionAndNonIterableSourceShouldNotFail() {
    // gh-10702
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    this.sources.add(source.nonIterable());
    Bindable<BeanWithNestedCollection> target = Bindable.of(BeanWithNestedCollection.class);
    this.binder.bind("foo", target);
}
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