Search in sources :

Example 21 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindToClassWhenHasNoPrefixShouldCreateBoundBean.

@Test
void bindToClassWhenHasNoPrefixShouldCreateBoundBean() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("int-value", "12");
    source.put("long-value", "34");
    source.put("boolean-value", "true");
    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.isBooleanValue()).isTrue();
    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 22 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindToClassWithMultipleConstructorsShouldNotBind.

@Test
void bindToClassWithMultipleConstructorsShouldNotBind() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.int-value", "12");
    this.sources.add(source);
    boolean bound = this.binder.bind("foo", Bindable.of(MultipleConstructorsBean.class)).isBound();
    assertThat(bound).isFalse();
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 23 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindToClassWithNoValueForPrimitiveShouldUseDefault.

@Test
void bindToClassWithNoValueForPrimitiveShouldUseDefault() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.string-value", "foo");
    this.sources.add(source);
    ExampleValueBean bean = this.binder.bind("foo", Bindable.of(ExampleValueBean.class)).get();
    assertThat(bean.getIntValue()).isEqualTo(0);
    assertThat(bean.getLongValue()).isEqualTo(0);
    assertThat(bean.isBooleanValue()).isEqualTo(false);
    assertThat(bean.getStringValue()).isEqualTo("foo");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 24 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindWhenAllPropertiesBoundShouldClearConfigurationProperty.

@Test
void bindWhenAllPropertiesBoundShouldClearConfigurationProperty() {
    // gh-18704
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.bar", "hello");
    this.sources.add(source);
    Bindable<ValidatingConstructorBean> target = Bindable.of(ValidatingConstructorBean.class);
    assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.binder.bind("foo", target)).satisfies(this::noConfigurationProperty);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 25 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindWhenBindingToPathTypeWithValue.

@Test
void bindWhenBindingToPathTypeWithValue() {
    // gh-21263
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("test.name", "test");
    source.put("test.path", "specific_value");
    this.sources.add(source);
    Bindable<PathBean> target = Bindable.of(PathBean.class);
    PathBean bound = this.binder.bind("test", target).get();
    assertThat(bound.getName()).isEqualTo("test");
    assertThat(bound.getPath()).isEqualTo(Paths.get("specific_value"));
}
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