简体   繁体   中英

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

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

  1. If I leave the controller as shown, how can I access the attributes of the model in my test?
  2. 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.

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.

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>

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

@pedrofalcaocosta,我给你的答案投票,因为它帮助我找到了答案,但我认为在这里回答我自己的问题是合适的:

((java.util.HashMap<String,Object>)mav.getModel().get("model")).get("companyInfo")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM