Browse Source

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
pull/565/merge
Lea Farmer 11 years ago committed by Rossen Stoyanchev
parent
commit
85cdb9196e
  1. 50
      spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java
  2. 27
      spring-test/src/test/java/org/springframework/test/web/servlet/result/ModelResultMatchersTests.java

50
spring-test/src/main/java/org/springframework/test/web/servlet/result/ModelResultMatchers.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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 <T> ResultMatcher attributeHasFieldErrorCode(final String name, final String fieldName,
final Matcher<? super String> 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.
*/

27
spring-test/src/test/java/org/springframework/test/web/servlet/result/ModelResultMatchersTests.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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; @@ -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 { @@ -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);
}

Loading…
Cancel
Save