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
This commit is contained in:
Lea Farmer
2014-07-07 21:00:51 +01:00
committed by Rossen Stoyanchev
parent 2ba8fd7e59
commit 85cdb9196e
2 changed files with 72 additions and 5 deletions

View File

@@ -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);
}