Package org.springframework.test.web
Class ModelAndViewAssert
- java.lang.Object
- org.springframework.test.web.ModelAndViewAssert
public abstract class ModelAndViewAssert extends Object
A collection of assertions intended to simplify testing scenarios dealing with Spring Web MVCModelAndView
objects.Intended for use with JUnit 4 and TestNG. All
assert*()
methods throwAssertionError
s.- Since:
- 2.5
- Author:
- Sam Brannen, Alef Arendsen, Bram Smeets
- See Also:
ModelAndView
Constructor Summary
Constructors Constructor Description ModelAndViewAssert()
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
assertAndReturnModelAttributeOfType(ModelAndView mav, String modelName, Class<T> expectedType)
Checks whether the model value under the givenmodelName
exists and checks it type, based on theexpectedType
.static void
assertCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList)
Compare each individual entry in a list, without first sorting the lists.static void
assertModelAttributeAvailable(ModelAndView mav, String modelName)
Assert whether or not a model attribute is available.static void
assertModelAttributeValue(ModelAndView mav, String modelName, Object expectedValue)
Compare a givenexpectedValue
to the value from the model bound under the givenmodelName
.static void
assertModelAttributeValues(ModelAndView mav, Map<String,Object> expectedModel)
Inspect theexpectedModel
to see if all elements in the model appear and are equal.static void
assertSortAndCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList, Comparator comparator)
Compare each individual entry in a list after having sorted both lists (optionally using a comparator).static void
assertViewName(ModelAndView mav, String expectedName)
Check to see if the view name in the ModelAndView matches the givenexpectedName
.
Constructor Detail
ModelAndViewAssert
public ModelAndViewAssert()
Method Detail
assertAndReturnModelAttributeOfType
public static <T> T assertAndReturnModelAttributeOfType(ModelAndView mav, String modelName, Class<T> expectedType)
Checks whether the model value under the givenmodelName
exists and checks it type, based on theexpectedType
. If the model entry exists and the type matches, the model value is returned.- Parameters:
mav
- ModelAndView to test against (nevernull
)modelName
- name of the object to add to the model (nevernull
)expectedType
- expected type of the model value- Returns:
- the model value
assertCompareListModelAttribute
public static void assertCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList)
Compare each individual entry in a list, without first sorting the lists.- Parameters:
mav
- ModelAndView to test against (nevernull
)modelName
- name of the object to add to the model (nevernull
)expectedList
- the expected list
assertModelAttributeAvailable
public static void assertModelAttributeAvailable(ModelAndView mav, String modelName)
Assert whether or not a model attribute is available.- Parameters:
mav
- ModelAndView to test against (nevernull
)modelName
- name of the object to add to the model (nevernull
)
assertModelAttributeValue
public static void assertModelAttributeValue(ModelAndView mav, String modelName, Object expectedValue)
Compare a givenexpectedValue
to the value from the model bound under the givenmodelName
.- Parameters:
mav
- ModelAndView to test against (nevernull
)modelName
- name of the object to add to the model (nevernull
)expectedValue
- the model value
assertModelAttributeValues
public static void assertModelAttributeValues(ModelAndView mav, Map<String,Object> expectedModel)
Inspect theexpectedModel
to see if all elements in the model appear and are equal.- Parameters:
mav
- ModelAndView to test against (nevernull
)expectedModel
- the expected model
assertSortAndCompareListModelAttribute
public static void assertSortAndCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList, Comparator comparator)
Compare each individual entry in a list after having sorted both lists (optionally using a comparator).- Parameters:
mav
- ModelAndView to test against (nevernull
)modelName
- name of the object to add to the model (nevernull
)expectedList
- the expected listcomparator
- the comparator to use (may benull
). If not specifying the comparator, both lists will be sorted not using any comparator.
assertViewName
public static void assertViewName(ModelAndView mav, String expectedName)
Check to see if the view name in the ModelAndView matches the givenexpectedName
.- Parameters:
mav
- ModelAndView to test against (nevernull
)expectedName
- the name of the model value