From 85cdb9196e0b2e1cbd7454f7cde912e28a69f1e3 Mon Sep 17 00:00:00 2001 From: Lea Farmer Date: Mon, 7 Jul 2014 21:00:51 +0100 Subject: [PATCH] Added field error code matching to MockMvc This change adds a method within the ModelResultMatcher that will allow a user to assert whether the returned Model has an attribute with a field that has a specific error associated with it. Issue: SPR-11971 --- .../servlet/result/ModelResultMatchers.java | 50 +++++++++++++++++-- .../result/ModelResultMatchersTests.java | 27 +++++++++- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java index 8a34723250..c83b05bc6d 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -160,13 +160,57 @@ public class ModelResultMatchers { BindingResult result = getBindingResult(mav, name); assertTrue("No errors for attribute: [" + name + "]", result.hasErrors()); for (final String fieldName : fieldNames) { - assertTrue("No errors for field: [" + fieldName + "] of attribute [" + name + "]", - result.hasFieldErrors(fieldName)); + boolean hasFieldErrors = result.hasFieldErrors(fieldName); + assertTrue("No errors for field: [" + fieldName + "] of attribute [" + name + "]", hasFieldErrors); } } }; } + /** + * Assert a field error code for a model attribute using exact String match. + * @since 4.1 + */ + public ResultMatcher attributeHasFieldErrorCode(final String name, final String fieldName, final String error) { + return new ResultMatcher() { + public void match(MvcResult mvcResult) throws Exception { + ModelAndView mav = getModelAndView(mvcResult); + BindingResult result = getBindingResult(mav, name); + assertTrue("No errors for attribute: [" + name + "]", result.hasErrors()); + + boolean hasFieldErrors = result.hasFieldErrors(fieldName); + assertTrue("No errors for field: [" + fieldName + "] of attribute [" + name + "]", hasFieldErrors); + + String code = result.getFieldError(fieldName).getCode(); + assertTrue("Expected error code '" + error + "' but got '" + code + "'", code.equals(error)); + } + }; + } + + /** + * Assert a field error code for a model attribute using a {@link org.hamcrest.Matcher}. + * @since 4.1 + */ + public ResultMatcher attributeHasFieldErrorCode(final String name, final String fieldName, + final Matcher matcher) { + + return new ResultMatcher() { + @Override + @SuppressWarnings("unchecked") + public void match(MvcResult mvcResult) throws Exception { + ModelAndView mav = getModelAndView(mvcResult); + BindingResult result = getBindingResult(mav, name); + assertTrue("No errors for attribute: [" + name + "]", result.hasErrors()); + + boolean hasFieldErrors = result.hasFieldErrors(fieldName); + assertTrue("No errors for field: [" + fieldName + "] of attribute [" + name + "]", hasFieldErrors); + + String code = result.getFieldError(fieldName).getCode(); + assertThat("Field name '" + fieldName + "' of attribute '" + name + "'", code, matcher); + } + }; + } + /** * Assert the total number of errors in the model. */ diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/ModelResultMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/ModelResultMatchersTests.java index 315095d911..d13829e2e8 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/ModelResultMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/ModelResultMatchersTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.test.web.servlet.result; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.startsWith; import java.util.Date; @@ -24,12 +25,14 @@ import org.junit.Before; import org.junit.Test; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.StubMvcResult; -import org.springframework.test.web.servlet.result.ModelResultMatchers; import org.springframework.validation.BeanPropertyBindingResult; import org.springframework.validation.BindingResult; import org.springframework.web.servlet.ModelAndView; /** + * Unit tests for + * {@link org.springframework.test.web.servlet.result.ModelResultMatchers}. + * * @author Craig Walls */ public class ModelResultMatchersTests { @@ -145,6 +148,26 @@ public class ModelResultMatchersTests { this.matchers.attributeHasFieldErrors("date", "good", "time").match(this.mvcResultWithError); } + @Test + public void attributeHasFieldErrorCode() throws Exception { + this.matchers.attributeHasFieldErrorCode("date", "time", "error").match(this.mvcResultWithError); + } + + @Test(expected = AssertionError.class) + public void attributeHasFieldErrorCode_withoutErrorOnField() throws Exception { + this.matchers.attributeHasFieldErrorCode("date", "time", "incorrectError").match(this.mvcResultWithError); + } + + @Test + public void attributeHasFieldErrorCode_startsWith() throws Exception { + this.matchers.attributeHasFieldErrorCode("date", "time", startsWith("err")).match(this.mvcResultWithError); + } + + @Test(expected = AssertionError.class) + public void attributeHasFieldErrorCode_startsWith_withoutErrorOnField() throws Exception { + this.matchers.attributeHasFieldErrorCode("date", "time", startsWith("inc")).match(this.mvcResultWithError); + } + private MvcResult getMvcResult(ModelAndView modelAndView) { return new StubMvcResult(null, null, null, null, modelAndView, null, null); }