Search in sources :

Example 26 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindWhenBindingToPathTypeWithDefaultValue.

@Test
void bindWhenBindingToPathTypeWithDefaultValue() {
    // gh-21263
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("test.name", "test");
    this.sources.add(source);
    Bindable<PathBean> target = Bindable.of(PathBean.class);
    PathBean bound = this.binder.bindOrCreate("test", target);
    assertThat(bound.getName()).isEqualTo("test");
    assertThat(bound.getPath()).isEqualTo(Paths.get("default_value"));
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 27 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindToClassWithNoValueAndDefaultValueShouldNotBind.

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

Example 28 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindToClassWithOnlyDefaultConstructorShouldNotBind.

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

Example 29 with MockConfigurationPropertySource

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

the class ValueObjectBinderTests method bindToClassShouldBindWithGenerics.

@Test
void bindToClassShouldBindWithGenerics() {
    // gh-19156
    ResolvableType type = ResolvableType.forClassWithGenerics(Map.class, String.class, String.class);
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.value.bar", "baz");
    this.sources.add(source);
    GenericValue<Map<String, String>> bean = this.binder.bind("foo", Bindable.<GenericValue<Map<String, String>>>of(ResolvableType.forClassWithGenerics(GenericValue.class, type))).get();
    assertThat(bean.getValue().get("bar")).isEqualTo("baz");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) ResolvableType(org.springframework.core.ResolvableType) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 30 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToMapNonScalarCollectionShouldPopulateMap.

@Test
void bindToMapNonScalarCollectionShouldPopulateMap() {
    Bindable<List<JavaBean>> valueType = Bindable.listOf(JavaBean.class);
    Bindable<Map<String, List<JavaBean>>> target = getMapBindable(String.class, valueType.getType());
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.bar[0].value", "a");
    source.put("foo.bar[1].value", "b");
    source.put("foo.bar[2].value", "c");
    this.sources.add(source);
    Map<String, List<JavaBean>> map = this.binder.bind("foo", target).get();
    List<String> values = map.get("bar").stream().map(JavaBean::getValue).collect(Collectors.toList());
    assertThat(values).containsExactly("a", "b", "c");
}
Also used : JavaBean(org.springframework.boot.context.properties.bind.BinderTests.JavaBean) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) 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