Search in sources :

Example 91 with MockConfigurationPropertySource

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

the class CollectionBinderTests method bindToBeanWithExceptionInGetterForExistingValue.

@Test
void bindToBeanWithExceptionInGetterForExistingValue() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.values", "a,b,c");
    this.sources.add(source);
    BeanWithGetterException result = this.binder.bind("foo", Bindable.of(BeanWithGetterException.class)).get();
    assertThat(result.getValues()).containsExactly("a", "b", "c");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 92 with MockConfigurationPropertySource

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

the class CollectionBinderTests method bindToCollectionWhenIndexedAndCommaListShouldOnlyUseFirst.

@Test
void bindToCollectionWhenIndexedAndCommaListShouldOnlyUseFirst() {
    MockConfigurationPropertySource source1 = new MockConfigurationPropertySource();
    source1.put("foo[0]", "1");
    source1.put("foo[1]", "2");
    this.sources.add(source1);
    MockConfigurationPropertySource source2 = new MockConfigurationPropertySource();
    source2.put("foo", "2,3");
    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 93 with MockConfigurationPropertySource

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

the class CollectionBinderTests method bindToCollectionWhenNonSequentialShouldThrowException.

@Test
void bindToCollectionWhenNonSequentialShouldThrowException() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo[0]", "2");
    source.put("foo[1]", "1");
    source.put("foo[3]", "3");
    this.sources.add(source);
    assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.binder.bind("foo", INTEGER_LIST)).satisfies((ex) -> {
        Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex.getCause()).getUnboundProperties();
        assertThat(unbound).hasSize(1);
        ConfigurationProperty property = unbound.iterator().next();
        assertThat(property.getName().toString()).isEqualTo("foo[3]");
        assertThat(property.getValue()).isEqualTo("3");
    });
}
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 94 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToInstanceShouldBindToInstance.

@Test
void bindToInstanceShouldBindToInstance() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.int-value", "12");
    source.put("foo.long-value", "34");
    source.put("foo.string-value", "foo");
    source.put("foo.enum-value", "foo-bar");
    this.sources.add(source);
    ExampleValueBean bean = new ExampleValueBean();
    ExampleValueBean boundBean = this.binder.bind("foo", Bindable.of(ExampleValueBean.class).withExistingValue(bean)).get();
    assertThat(boundBean).isSameAs(bean);
    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 95 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToClassWithOverloadedSetterShouldUseSetterThatMatchesField.

@Test
void bindToClassWithOverloadedSetterShouldUseSetterThatMatchesField() {
    // gh-16206
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.property", "some string");
    this.sources.add(source);
    PropertyWithOverloadedSetter bean = this.binder.bind("foo", Bindable.of(PropertyWithOverloadedSetter.class)).get();
    assertThat(bean.getProperty()).isEqualTo("some string");
}
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