Search in sources :

Example 41 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToMapShouldReturnPopulatedMap.

@Test
void bindToMapShouldReturnPopulatedMap() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.bar", "1");
    source.put("foo.[baz]", "2");
    source.put("foo[BiNg]", "3");
    this.sources.add(source);
    Map<String, String> result = this.binder.bind("foo", STRING_STRING_MAP).get();
    assertThat(result).hasSize(3);
    assertThat(result).containsEntry("bar", "1");
    assertThat(result).containsEntry("baz", "2");
    assertThat(result).containsEntry("BiNg", "3");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 42 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToMapWhenEmptyRootNameShouldBindMap.

@Test
void bindToMapWhenEmptyRootNameShouldBindMap() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("bar.baz", "1");
    source.put("bar.bin", "2");
    this.sources.add(source);
    Map<String, Integer> result = this.binder.bind("", STRING_INTEGER_MAP).get();
    assertThat(result).hasSize(2);
    assertThat(result).containsEntry("bar.baz", 1).containsEntry("bar.bin", 2);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 43 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToMapShouldConvertMapValue.

@Test
void bindToMapShouldConvertMapValue() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.bar", "1");
    source.put("foo.[baz]", "2");
    source.put("foo[BiNg]", "3");
    source.put("faf.bar", "x");
    this.sources.add(source);
    Map<String, Integer> result = this.binder.bind("foo", STRING_INTEGER_MAP).get();
    assertThat(result).hasSize(3);
    assertThat(result).containsEntry("bar", 1);
    assertThat(result).containsEntry("baz", 2);
    assertThat(result).containsEntry("BiNg", 3);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 44 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToMapShouldRespectMapType.

@Test
void bindToMapShouldRespectMapType() {
    this.sources.add(new MockConfigurationPropertySource("foo.bar", "1"));
    ResolvableType type = ResolvableType.forClassWithGenerics(HashMap.class, String.class, Integer.class);
    Object defaultMap = this.binder.bind("foo", STRING_INTEGER_MAP).get();
    Object customMap = this.binder.bind("foo", Bindable.of(type)).get();
    assertThat(customMap).isExactlyInstanceOf(HashMap.class).isNotInstanceOf(defaultMap.getClass());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.jupiter.api.Test)

Example 45 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToInstanceWithExistingValueShouldReturnUnbound.

@Test
void bindToInstanceWithExistingValueShouldReturnUnbound() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    this.sources.add(source);
    ExampleNestedBean existingValue = new ExampleNestedBean();
    ExampleValueBean valueBean = new ExampleValueBean();
    existingValue.setValueBean(valueBean);
    BindResult<ExampleNestedBean> result = this.binder.bind("foo", Bindable.of(ExampleNestedBean.class).withExistingValue(existingValue));
    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