Search in sources :

Example 81 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testBindErrorNotWritable.

@Test
public void testBindErrorNotWritable() throws Exception {
    this.expected.expectMessage("property 'spam'");
    this.expected.expectMessage("not writable");
    VanillaTarget target = new VanillaTarget();
    BindingResult result = bind(target, "spam: bar\n" + "value: 123");
    assertThat(result.getErrorCount()).isEqualTo(1);
}
Also used : BindingResult(org.springframework.validation.BindingResult) Test(org.junit.Test)

Example 82 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testDisallowedFields.

@Test
public void testDisallowedFields() throws Exception {
    VanillaTarget target = new VanillaTarget();
    RelaxedDataBinder binder = getBinder(target, null);
    // Disallowed fields are not unknown...
    binder.setDisallowedFields("foo", "bar");
    binder.setIgnoreUnknownFields(false);
    BindingResult result = bind(binder, target, "foo: bar\n" + "value: 123\n" + "bar: spam");
    assertThat(target.getValue()).isEqualTo(123);
    assertThat(target.getFoo()).isNull();
    assertThat(result.getErrorCount()).isEqualTo(0);
}
Also used : BindingResult(org.springframework.validation.BindingResult) Test(org.junit.Test)

Example 83 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testBindMapWithClashInProperties.

@Test
public void testBindMapWithClashInProperties() throws Exception {
    Map<String, Object> target = new LinkedHashMap<>();
    BindingResult result = bind(target, "vanilla.spam: bar\n" + "vanilla.spam.value: 123", "vanilla");
    assertThat(result.getErrorCount()).isEqualTo(0);
    assertThat(target).hasSize(2);
    assertThat(target.get("spam")).isEqualTo("bar");
    assertThat(target.get("spam.value")).isEqualTo("123");
}
Also used : BindingResult(org.springframework.validation.BindingResult) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 84 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testBindMapNestedMap.

@Test
public void testBindMapNestedMap() throws Exception {
    Map<String, Object> target = new LinkedHashMap<>();
    BindingResult result = bind(target, "spam: bar\n" + "vanilla.foo.value: 123", "vanilla");
    assertThat(result.getErrorCount()).isEqualTo(0);
    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) target.get("foo");
    assertThat(map.get("value")).isEqualTo("123");
}
Also used : BindingResult(org.springframework.validation.BindingResult) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 85 with BindingResult

use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testBindShallowMap.

@Test
public void testBindShallowMap() throws Exception {
    Map<String, Object> target = new LinkedHashMap<>();
    BindingResult result = bind(target, "vanilla.spam: bar\n" + "vanilla.value: 123", "vanilla");
    assertThat(result.getErrorCount()).isEqualTo(0);
    assertThat(target.get("value")).isEqualTo("123");
}
Also used : BindingResult(org.springframework.validation.BindingResult) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

BindingResult (org.springframework.validation.BindingResult)143 Test (org.junit.jupiter.api.Test)29 Test (org.junit.Test)28 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)24 List (java.util.List)23 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)23 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)21 ResponseEntity (org.springframework.http.ResponseEntity)20 Collectors (java.util.stream.Collectors)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 FieldError (org.springframework.validation.FieldError)18 ObjectError (org.springframework.validation.ObjectError)17 Model (org.springframework.ui.Model)15 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)14 HashMap (java.util.HashMap)13 Nonnull (javax.annotation.Nonnull)13 Nullable (javax.annotation.Nullable)13 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)13 DefaultProblem (org.zalando.problem.DefaultProblem)13 Problem (org.zalando.problem.Problem)13