Search in sources :

Example 16 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindToClassWithMultipleConstructorsAndFilterShouldBind.

@Test
void bindToClassWithMultipleConstructorsAndFilterShouldBind() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.int-value", "12");
    this.sources.add(source);
    Constructor<?>[] constructors = MultipleConstructorsBean.class.getDeclaredConstructors();
    Constructor<?> constructor = (constructors[0].getParameterCount() == 1) ? constructors[0] : constructors[1];
    Binder binder = new Binder(this.sources, null, (ConversionService) null, null, null, (bindable, isNestedConstructorBinding) -> constructor);
    MultipleConstructorsBean bound = binder.bind("foo", Bindable.of(MultipleConstructorsBean.class)).get();
    assertThat(bound.getIntValue()).isEqualTo(12);
}
Also used : Constructor(java.lang.reflect.Constructor) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 17 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindWithAnnotationsAndDefaultValue.

@Test
void bindWithAnnotationsAndDefaultValue() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.bar", "hello");
    this.sources.add(source);
    ConverterAnnotatedExampleBean bean = this.binder.bind("foo", Bindable.of(ConverterAnnotatedExampleBean.class)).get();
    assertThat(bean.getDate().toString()).isEqualTo("2019-05-10");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 18 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindToAnnotationNamedParameter.

@Test
void bindToAnnotationNamedParameter() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("test.import", "test");
    this.sources.add(source);
    Bindable<NamedParameter> target = Bindable.of(NamedParameter.class);
    NamedParameter bound = this.binder.bindOrCreate("test", target);
    assertThat(bound.getImportName()).isEqualTo("test");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 19 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindToClassWhenHasPackagePrivateConstructorShouldBind.

@Test
void bindToClassWhenHasPackagePrivateConstructorShouldBind() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.property", "test");
    this.sources.add(source);
    ExamplePackagePrivateConstructorBean bound = this.binder.bind("foo", Bindable.of(ExamplePackagePrivateConstructorBean.class)).get();
    assertThat(bound.getProperty()).isEqualTo("test");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 20 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindToClassWhenNoParameterBoundShouldReturnNull.

@Test
void bindToClassWhenNoParameterBoundShouldReturnNull() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    this.sources.add(source.nonIterable());
    BindResult<ExampleFailingConstructorBean> result = this.binder.bind("foo", Bindable.of(ExampleFailingConstructorBean.class));
    assertThat(result.isBound()).isFalse();
}
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