Accessing the attributes of a model contained in a ModelAndView object from the context of a controller test
Solution 1
@pedrofalcaocosta, I'm giving your answer an up vote because it helped me find my answer, but I think it's appropriate to answer my own question here:
((java.util.HashMap<String,Object>)mav.getModel().get("model")).get("companyInfo")
Solution 2
For question 1, Model provides a method that returns the model attributes as a map. In your test, you can do:
Map<String,Object> modelMap = mav.getModel().asMap();
modelMap.get("companyInfo");
Assuming you set companyInfo into the model, it should be there.
As for part2 of the question, I think someone else answered that already.
Solution 3
Ok, now its clear!
Try:
mav.getModel().get("model");
mav.getModel().containsKey("model");
You called your modelmap 'model' in your controller...
In your jsp i would recommend using Jstl:
<%@page contentType="text/html; charset=utf-8" pageEncoding="UTF-8" language="java"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
${model.companyInfo}
</body>
</html>
Solution 4
You forgot to call the constructor of ModelAndView with the viewname, and you forgot to add your objects to the model.
I think you code should look something like this...
@Test
public void shouldDoStuff()
{
request.setRequestURI("/myCompany/123");
// call the constructor with the name of your view
ModelAndView mav = new ModelAndView("viewName");
// add the objects to the model
mav.addAllObjects(controller.getSomeDatas("123", request));
assertEquals(mav.getViewName(), "viewName");
assertTrue(mav.getModel().containsKey("companyInfo"));
}
If you need to add more than one object with custom keys use the addObject method instead;
mav.addObject("key1", 1);
mav.addObject("key2", 2);
Samo
I'm a software consultant in the twin cities, specializing in Ruby on Rails, PostgreSQL, and JavaScript. I also play blues, jazz, and klezmer saxophone and enjoy a chess game on occasion. I spend most of my off time with my wife and kids.
Updated on May 04, 2020Comments
-
Samo about 4 years
I am new to Spring MVC and I'm in the process of learning how to test my controllers. I have a simple test:
@Test public void shouldDoStuff() { request.setRequestURI("/myCompany/123"); ModelAndView mav = controller.getSomeDatas("123", request); assertEquals(mav.getViewName(), "company"); assertTrue(mav.getModel().containsKey("companyInfo")); assertTrue(mav.getModel().containsKey("rightNow")); assertEquals(mav.getModel().get("companyInfo"), "123"); }
Here's my controller action:
@RequestMapping(value = "/myCompany/{companyGuid}", method = RequestMethod.GET) public ModelAndView getSomeDatas(@PathVariable("companyGuid") String myGuid, HttpServletRequest request) { /*ModelAndView mav = new ModelAndView("company"); mav.addObject("companyInfo", myGuid); mav.addObject("rightNow", (new Date()).toString()); return mav;*/ Map<String, Object> myModel = new HashMap<String, Object>(); myModel.put("companyInfo", myGuid); myModel.put("rightNow", (new Date()).toString()); return new ModelAndView("company", "model", myModel); }
I have a breakpoint set on the first assert. In the Display window in Eclipse, mav.getModel() returns exactly what I'd expect:
mav.getModel() (org.springframework.ui.ModelMap) {model={rightNow=Fri Nov 05 13:30:57 CDT 2010, companyInfo=123}}
However, any attempt to access the values in that model fails. For example, I assumed the following would work:
mav.getModel().get("companyInfo") null mav.getModel().containsKey("companyInfo") (boolean) false
But as you can see, get("companyInfo") returns null, and containsKey("companyInfo") returns false.
When I swap out the commented section of the controller with the uncommented section, my tests work just fine, but then my jsp view breaks, because I'm trying to access properties of the model by saying things like ${model.companyInfo}, etc.
So I need to know at least one of two things (but better if you can answer both):
- If I leave the controller as shown, how can I access the attributes of the model in my test?
- If I swap out the commented section for the uncommented section, how can I access the attributes of the model in my jsp view?
Any help is appreciated.
-
skaffman over 13 yearsWell the obvious answer is that your controller is broken, and the test is doing its job. Without seeing your controller code, that's all I can suggest,
-
Samo over 13 yearsI've updated my question to include my controller code. Either way, the fact that the object clearly contains the value I seek seems rather contradictory to the fact that I can't retrieve this value from it, wouldn't you agree?
-
Samo over 13 yearsI can see why this would work, but what I don't understand is this: getSomeDatas() is supposed to return a ModelAndView object. The object it's returning clearly contains the values I've added to it, as is shown above. Why can't I retrieve these values from it? Why should I create a new ModelAndView object in my test and add objects from the controller, when my controller is supposed to RETURN this ModelAndView object with the values contained within?
-
Samo over 13 yearsthanks for the tip, it was helpful, but not quite there :) It did help me find the answer, but I've posted the actual answer in hopes that it will help other developers experiencing the same problem.
-
pedrofalcaocosta over 13 yearsSorry by the incomplete answer, but the update to your question make me a little confuse... I knew the problem was in 'model' name. Anyway... your're welcome!