Search in sources :

Example 56 with MockConfigurationPropertySource

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

the class JavaBeanBinderTests method bindToClassShouldBindHierarchy.

@Test
void bindToClassShouldBindHierarchy() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.int-value", "123");
    source.put("foo.long-value", "456");
    this.sources.add(source);
    ExampleSubclassBean bean = this.binder.bind("foo", Bindable.of(ExampleSubclassBean.class)).get();
    assertThat(bean.getIntValue()).isEqualTo(123);
    assertThat(bean.getLongValue()).isEqualTo(456);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 57 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToMapWithNoConverterForValue.

@Test
void bindToMapWithNoConverterForValue() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo", "a,b");
    this.sources.add(source);
    assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.binder.bind("foo", STRING_STRING_MAP));
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 58 with MockConfigurationPropertySource

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

the class BinderTests method bindToJavaBeanWhenNonIterableShouldReturnPopulatedBean.

@Test
void bindToJavaBeanWhenNonIterableShouldReturnPopulatedBean() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource("foo.value", "bar");
    this.sources.add(source.nonIterable());
    JavaBean result = this.binder.bind("foo", Bindable.of(JavaBean.class)).get();
    assertThat(result.getValue()).isEqualTo("bar");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 59 with MockConfigurationPropertySource

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

the class BinderTests method bindOrCreateWhenBindSuccessfulShouldReturnBoundValue.

@Test
void bindOrCreateWhenBindSuccessfulShouldReturnBoundValue() {
    this.sources.add(new MockConfigurationPropertySource("foo.value", "bar"));
    JavaBean result = this.binder.bindOrCreate("foo", Bindable.of(JavaBean.class));
    assertThat(result.getValue()).isEqualTo("bar");
    assertThat(result.getItems()).isEmpty();
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 60 with MockConfigurationPropertySource

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

the class BinderTests method bindToValueWithCustomPropertyEditorShouldReturnConvertedValue.

@Test
void bindToValueWithCustomPropertyEditorShouldReturnConvertedValue() {
    this.binder = new Binder(this.sources, null, null, (registry) -> registry.registerCustomEditor(JavaBean.class, new JavaBeanPropertyEditor()));
    this.sources.add(new MockConfigurationPropertySource("foo", "123"));
    BindResult<JavaBean> result = this.binder.bind("foo", Bindable.of(JavaBean.class));
    assertThat(result.get().getValue()).isEqualTo("123");
}
Also used : ConfigurationPropertySource(org.springframework.boot.context.properties.source.ConfigurationPropertySource) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) PropertySource(org.springframework.core.env.PropertySource) Validator(org.springframework.validation.Validator) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) ArrayList(java.util.ArrayList) ConfigurationPropertyName(org.springframework.boot.context.properties.source.ConfigurationPropertyName) Map(java.util.Map) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) SpringValidatorAdapter(org.springframework.validation.beanvalidation.SpringValidatorAdapter) ConfigurationPropertySources(org.springframework.boot.context.properties.source.ConfigurationPropertySources) Resource(org.springframework.core.io.Resource) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) Validation(jakarta.validation.Validation) Validated(org.springframework.validation.annotation.Validated) Answers(org.mockito.Answers) InOrder(org.mockito.InOrder) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) ValidationBindHandler(org.springframework.boot.context.properties.bind.validation.ValidationBindHandler) StandardEnvironment(org.springframework.core.env.StandardEnvironment) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test) List(java.util.List) TestPropertySourceUtils(org.springframework.test.context.support.TestPropertySourceUtils) MapPropertySource(org.springframework.core.env.MapPropertySource) Mockito.inOrder(org.mockito.Mockito.inOrder) LocalDate(java.time.LocalDate) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Collections(java.util.Collections) BindRestriction(org.springframework.boot.context.properties.bind.Bindable.BindRestriction) Mockito.mock(org.mockito.Mockito.mock) 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