SPR-6785 - Improve message error when the MarshallingView modelKey attribute is not valid
This commit is contained in:
parent
1dbb64580c
commit
208c2d9c8b
|
@ -122,6 +122,9 @@ public class MarshallingView extends AbstractView {
|
|||
protected Object locateToBeMarshalled(Map model) throws ServletException {
|
||||
if (this.modelKey != null) {
|
||||
Object o = model.get(this.modelKey);
|
||||
if (o == null) {
|
||||
throw new ServletException("Model contains no object with key [" + modelKey + "]");
|
||||
}
|
||||
if (!this.marshaller.supports(o.getClass())) {
|
||||
throw new ServletException("Model object [" + o + "] retrieved via key [" + modelKey +
|
||||
"] is not supported by the Marshaller");
|
||||
|
|
|
@ -71,6 +71,29 @@ public class MarshallingViewTests {
|
|||
verify(marshallerMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderInvalidModelKey() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
String modelKey = "key";
|
||||
view.setModelKey("invalidKey");
|
||||
Map model = new HashMap();
|
||||
model.put(modelKey, toBeMarshalled);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
replay(marshallerMock);
|
||||
try {
|
||||
view.render(model, request, response);
|
||||
fail("ServletException expected");
|
||||
}
|
||||
catch (ServletException ex) {
|
||||
// expected
|
||||
}
|
||||
assertEquals("Invalid content length", 0, response.getContentLength());
|
||||
verify(marshallerMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderModelKeyUnsupported() throws Exception {
|
||||
Object toBeMarshalled = new Object();
|
||||
|
|
Loading…
Reference in New Issue