Hello,
I'm implementting a form with referended data, as class Pet of the example petclinic, but in my case:
public class Product implements Serializable {
private Integer id;
private String name;
private Category category;
......
}
So, I'm using appfuse and that form (ProductFormController) is running ok on browser. But, my test case not!
I'm getting this error:
[junit] [estoque] DEBUG [main] ProductFormControllerTest.testSave(68) | org.springframework.validation.BindException: BindException: 1 errors;
Here is my code:
public void testEdit() throws Exception {
log.debug("*********** TESTANDO EDIT ****************");
request = newGet("/editProduct.html");
request.addParameter("username", "tomcat");
mv = c.handleRequest(request, new MockHttpServletResponse());
assertEquals("productForm", mv.getViewName());
}
public void testSave() throws Exception {
log.debug("*********** TESTANDO SAVE ****************");
request = new MockHttpServletRequest("GET","/editProduct.html");
request.addParameter("id", "1");
request.addParameter("categoryId", "1");
mv = c.handleRequest(request, new MockHttpServletResponse());
Product product = (Product) request.getAttribute(Constants.PRODUCT_KEY);
assertNotNull(product);
request = new MockHttpServletRequest("POST","/editProduct.html");
super.objectToRequestParameters(product, request);
request.addParameter("name", "Updated Last Name");
request.addParameter("categoryId", (product.getCategory().getId()).toString());
mv = c.handleRequest(request, new MockHttpServletResponse());
product = (Product) request.getAttribute(Constants.PRODUCT_KEY);
assertNotNull(product);
Errors errors =
(Errors) mv.getModel().get(BindException.ERROR_KEY_PREFIX + "product");
log.debug(errors);
assertNull(errors);
assertNotNull(request.getSession().getAttribute("m essages"));
}
The controller class:
protected Map referenceData(HttpServletRequest request) throws ServletException {
Map model = new HashMap();
model.put("categories", mgr.getCategories());
return model;
}
protected void onBind(HttpServletRequest request, Object command, BindException errors) {
if (log.isDebugEnabled()) {
log.debug("ENTRANDO NO METODO 'onBind'...");
}
Product product = (Product) command;
product.setCategory(mgr.getCategory(Short.valueOf( request.getParameter("categoryId"))));
}
}
So, someone can give me a light here.
Gilberto


Reply With Quote