Accessing the attributes of a model contained in a ModelAndView object from the context of a controller test?

Up vote 0 down vote favorite share g+ share fb share tw.

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 myModel = new HashMap(); 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.

Java spring-mvc junit link|improve this question edited Nov 5 '10 at 19:24 asked Nov 5 '10 at 18:50Samo1,3921516 78% accept rate.

Well 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, – skaffman Nov 5 '10 at 19:20 I'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 Nov 5 '10 at 19:22.

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: ${model. CompanyInfo}.

Thanks 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. – Samo Nov 5 '10 at 20:23.

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).

I 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 Nov 5 '10 at 19:27.

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)mav.getModel(). Get("model")).

Get("companyInfo").

Sorry 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! – pedrofalcaocosta Nov 5 '10 at 23:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions