Search in sources :

Example 96 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToClassWhenNoDefaultConstructorShouldBind.

@Test
void bindToClassWhenNoDefaultConstructorShouldBind() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.value", "bar");
    this.sources.add(source);
    BindResult<ExampleWithNonDefaultConstructor> bean = this.binder.bind("foo", Bindable.of(ExampleWithNonDefaultConstructor.class));
    assertThat(bean.isBound()).isTrue();
    ExampleWithNonDefaultConstructor boundBean = bean.get();
    assertThat(boundBean.getValue()).isEqualTo("bar");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 97 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindWhenValueIsConvertedWithPropertyEditorShouldBind.

@Test
void bindWhenValueIsConvertedWithPropertyEditorShouldBind() {
    // gh-12166
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.value", "java.lang.RuntimeException");
    this.sources.add(source);
    ExampleWithPropertyEditorType bean = this.binder.bind("foo", Bindable.of(ExampleWithPropertyEditorType.class)).get();
    assertThat(bean.getValue()).isEqualTo(RuntimeException.class);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 98 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindWhenHasPackagePrivateSetterShouldBind.

@Test
void bindWhenHasPackagePrivateSetterShouldBind() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.property", "test");
    this.sources.add(source);
    PackagePrivateSetterBean bean = this.binder.bind("foo", Bindable.of(PackagePrivateSetterBean.class)).get();
    assertThat(bean.getProperty()).isEqualTo("test");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 99 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToClassShouldBindToMap.

@Test
void bindToClassShouldBindToMap() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.map.foo-bar", "1");
    source.put("foo.map.bar-baz", "2");
    this.sources.add(source);
    ExampleMapBean bean = this.binder.bind("foo", Bindable.of(ExampleMapBean.class)).get();
    assertThat(bean.getMap()).containsExactly(entry(ExampleEnum.FOO_BAR, 1), entry(ExampleEnum.BAR_BAZ, 2));
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 100 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToClassShouldBindToSet.

@Test
void bindToClassShouldBindToSet() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.set[0]", "foo-bar");
    source.put("foo.set[1]", "bar-baz");
    this.sources.add(source);
    ExampleSetBean bean = this.binder.bind("foo", Bindable.of(ExampleSetBean.class)).get();
    assertThat(bean.getSet()).containsExactly(ExampleEnum.FOO_BAR, ExampleEnum.BAR_BAZ);
}
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