Skip to content

Commit 367e663

Browse files
committed
Assertions for error codes in case of formatting failures
Issue: SPR-14345 (cherry picked from commit c6f63bd)
1 parent 9ac661d commit 367e663

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

spring-context/src/test/java/org/springframework/validation/DataBinderTests.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -226,12 +226,14 @@ public void testBindingWithErrors() throws Exception {
226226

227227
assertTrue("Has age errors", br.hasFieldErrors("age"));
228228
assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
229+
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("age").getCode());
229230
assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
230231
assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
231232
assertEquals(0, tb.getAge());
232233

233234
assertTrue("Has touchy errors", br.hasFieldErrors("touchy"));
234235
assertTrue("Correct number of touchy errors", br.getFieldErrorCount("touchy") == 1);
236+
assertEquals("methodInvocation", binder.getBindingResult().getFieldError("touchy").getCode());
235237
assertEquals("m.y", binder.getBindingResult().getFieldValue("touchy"));
236238
assertEquals("m.y", binder.getBindingResult().getFieldError("touchy").getRejectedValue());
237239
assertNull(tb.getTouchy());
@@ -315,12 +317,14 @@ public String getAsText() {
315317

316318
assertTrue("Has age errors", br.hasFieldErrors("age"));
317319
assertTrue("Correct number of age errors", br.getFieldErrorCount("age") == 1);
320+
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("age").getCode());
318321
assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
319322
assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
320323
assertEquals(0, tb.getAge());
321324

322325
assertTrue("Has touchy errors", br.hasFieldErrors("touchy"));
323326
assertTrue("Correct number of touchy errors", br.getFieldErrorCount("touchy") == 1);
327+
assertEquals("methodInvocation", binder.getBindingResult().getFieldError("touchy").getCode());
324328
assertEquals("m.y", binder.getBindingResult().getFieldValue("touchy"));
325329
assertEquals("m.y", binder.getBindingResult().getFieldError("touchy").getRejectedValue());
326330
assertNull(tb.getTouchy());
@@ -419,6 +423,7 @@ public String print(String object, Locale locale) {
419423

420424
binder.bind(pvs);
421425
assertTrue(binder.getBindingResult().hasFieldErrors("name"));
426+
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode());
422427
assertEquals("test", binder.getBindingResult().getFieldValue("name"));
423428
}
424429

@@ -567,6 +572,7 @@ public void testBindingErrorWithCustomFormatter() {
567572
assertEquals(new Float(0.0), tb.getMyFloat());
568573
assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat"));
569574
assertTrue(binder.getBindingResult().hasFieldErrors("myFloat"));
575+
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("myFloat").getCode());
570576
}
571577
finally {
572578
LocaleContextHolder.resetLocaleContext();
@@ -881,7 +887,6 @@ public void testCustomEditorForPrimitiveProperty() {
881887
public void setAsText(String text) throws IllegalArgumentException {
882888
setValue(new Integer(99));
883889
}
884-
885890
@Override
886891
public String getAsText() {
887892
return "argh";
@@ -906,7 +911,6 @@ public void testCustomEditorForAllStringProperties() {
906911
public void setAsText(String text) throws IllegalArgumentException {
907912
setValue("prefix" + text);
908913
}
909-
910914
@Override
911915
public String getAsText() {
912916
return ((String) getValue()).substring(6);
@@ -979,7 +983,6 @@ public void testCustomFormatterForPrimitiveProperty() {
979983
public Integer parse(String text, Locale locale) throws ParseException {
980984
return 99;
981985
}
982-
983986
@Override
984987
public String print(Integer object, Locale locale) {
985988
return "argh";

0 commit comments

Comments
 (0)