I usually use the Model
interface and then in a test super class I have code which allows me to get at things in the Model
@Ignore
public abstract class SpringControllerTestCase {
/**
* Spring Model object - initialised in @Before method.
*/
private Model model;
/**
* Initialise fields before each test case.
*/
@Before
public final void setUpAll() {
model = new ExtendedModelMap();
}
public final Model getModel() {
return model;
}
@SuppressWarnings("unchecked")
public <T> T getModelValue(final String key, final Class<T> clazz) {
return (T) getModel().asMap().get(key);
}
}
then in a test I can do
assertEquals("someValue", getModelValue("bean", String.class));
or
assertTrue(getModelValue("student", Student.class).getId() == "767001");
Note this is all just shorthand for code like this
Student student = (Student) model.asMap().get("student");
assertEquals(767001, student.getId());