Search in sources :

Example 51 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToClassWhenPropertiesMissingShouldReturnUnbound.

@Test
void bindToClassWhenPropertiesMissingShouldReturnUnbound() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("faf.int-value", "12");
    this.sources.add(source);
    BindResult<ExampleValueBean> bean = this.binder.bind("foo", Bindable.of(ExampleValueBean.class));
    assertThat(bean.isBound()).isFalse();
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 52 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToClassWhenHasNoPrefixShouldCreateBoundBean.

@Test
void bindToClassWhenHasNoPrefixShouldCreateBoundBean() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("int-value", "12");
    source.put("long-value", "34");
    source.put("string-value", "foo");
    source.put("enum-value", "foo-bar");
    this.sources.add(source);
    ExampleValueBean bean = this.binder.bind(ConfigurationPropertyName.of(""), Bindable.of(ExampleValueBean.class)).get();
    assertThat(bean.getIntValue()).isEqualTo(12);
    assertThat(bean.getLongValue()).isEqualTo(34);
    assertThat(bean.getStringValue()).isEqualTo("foo");
    assertThat(bean.getEnumValue()).isEqualTo(ExampleEnum.FOO_BAR);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 53 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToInstanceWithNoPropertiesShouldReturnUnbound.

@Test
void bindToInstanceWithNoPropertiesShouldReturnUnbound() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    this.sources.add(source);
    ExampleDefaultsBean bean = new ExampleDefaultsBean();
    BindResult<ExampleDefaultsBean> boundBean = this.binder.bind("foo", Bindable.of(ExampleDefaultsBean.class).withExistingValue(bean));
    assertThat(boundBean.isBound()).isFalse();
    assertThat(bean.getFoo()).isEqualTo(123);
    assertThat(bean.getBar()).isEqualTo(456);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 54 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToExistingInstanceShouldLeaveDefaults.

@Test
void bindToExistingInstanceShouldLeaveDefaults() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.bar", "999");
    this.sources.add(source);
    ExampleDefaultsBean bean = new ExampleDefaultsBean();
    bean.setFoo(888);
    ExampleDefaultsBean boundBean = this.binder.bind("foo", Bindable.of(ExampleDefaultsBean.class).withExistingValue(bean)).get();
    assertThat(boundBean).isSameAs(bean);
    assertThat(bean.getFoo()).isEqualTo(888);
    assertThat(bean.getBar()).isEqualTo(999);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 55 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToClassWhenHasNoSetterAndImmutableShouldThrowException.

@Test
void bindToClassWhenHasNoSetterAndImmutableShouldThrowException() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.nested.foo", "bar");
    this.sources.add(source);
    assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.binder.bind("foo", Bindable.of(ExampleImmutableNestedBeanWithoutSetter.class)));
}
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