Search in sources :

Example 11 with MockConfigurationPropertySource

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

the class ValidationBindHandlerTests method bindShouldFailWithAccessToNameAndValue.

@Test
void bindShouldFailWithAccessToNameAndValue() {
    this.sources.add(new MockConfigurationPropertySource("foo.nested.age", "4"));
    BindValidationException cause = bindAndExpectValidationError(() -> this.binder.bind(ConfigurationPropertyName.of("foo"), Bindable.of(ExampleValidatedWithNestedBean.class), this.handler));
    assertThat(cause.getValidationErrors().getName().toString()).isEqualTo("foo.nested");
    assertThat(cause.getMessage()).contains("nested.age");
    assertThat(cause.getMessage()).contains("rejected value [4]");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 12 with MockConfigurationPropertySource

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

the class ValidationBindHandlerTests method validationShouldBeSkippedIfPreviousValidationErrorPresent.

@Test
void validationShouldBeSkippedIfPreviousValidationErrorPresent() {
    this.sources.add(new MockConfigurationPropertySource("foo.inner.person-age", 2));
    BindValidationException cause = bindAndExpectValidationError(() -> this.binder.bind(ConfigurationPropertyName.of("foo"), Bindable.of(ExampleCamelCase.class), this.handler));
    FieldError fieldError = (FieldError) cause.getValidationErrors().getAllErrors().get(0);
    assertThat(fieldError.getField()).isEqualTo("personAge");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) FieldError(org.springframework.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 13 with MockConfigurationPropertySource

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

the class ValidationBindHandlerTests method bindShouldFailWithAccessToOrigin.

@Test
void bindShouldFailWithAccessToOrigin() {
    this.sources.add(new MockConfigurationPropertySource("foo.age", 4, "file"));
    BindValidationException cause = bindAndExpectValidationError(() -> this.binder.bind(ConfigurationPropertyName.of("foo"), Bindable.of(ExampleValidatedBean.class), this.handler));
    ObjectError objectError = cause.getValidationErrors().getAllErrors().get(0);
    assertThat(Origin.from(objectError).toString()).isEqualTo("file");
}
Also used : ObjectError(org.springframework.validation.ObjectError) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 14 with MockConfigurationPropertySource

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

the class ValidationBindHandlerTests method validationErrorsForCamelCaseFieldsShouldContainRejectedValue.

@Test
void validationErrorsForCamelCaseFieldsShouldContainRejectedValue() {
    this.sources.add(new MockConfigurationPropertySource("foo.inner.person-age", 2));
    BindValidationException cause = bindAndExpectValidationError(() -> this.binder.bind(ConfigurationPropertyName.of("foo"), Bindable.of(ExampleCamelCase.class), this.handler));
    assertThat(cause.getMessage()).contains("rejected value [2]");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 15 with MockConfigurationPropertySource

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

the class MapBinderTests method bindToMapWithCustomConverter.

@Test
void bindToMapWithCustomConverter() {
    DefaultConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new MapConverter());
    Binder binder = new Binder(this.sources, null, conversionService, null);
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo", "a,b");
    this.sources.add(source);
    Map<String, String> map = binder.bind("foo", STRING_STRING_MAP).get();
    assertThat(map.get("a")).isNotNull();
    assertThat(map.get("b")).isNotNull();
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) 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