从控制器测试的上下文访问 ModelAndView 对象中包含的模型的属性
我是 Spring MVC 的新手,我正在学习如何测试我的控制器。我有一个简单的测试:
@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");
}
这是我的控制器操作:
@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);
}
我在第一个断言上设置了一个断点。在 Eclipse 的“显示”窗口中,mav.getModel() 返回的正是我所期望的:
mav.getModel()
(org.springframework.ui.ModelMap) {model={rightNow=Fri Nov 05 13:30:57 CDT 2010, companyInfo=123}}
但是,任何访问该模型中的值的尝试都会失败。例如,我假设以下内容可行:
mav.getModel().get("companyInfo")
null
mav.getModel().containsKey("companyInfo")
(boolean) false
但如您所见,get("companyInfo") 返回 null,并且 containsKey("companyInfo") 返回 false。
当我用未注释的部分替换控制器的注释部分时,我的测试工作正常,但随后我的 jsp 视图中断,因为我试图通过诸如 ${model.companyInfo} 之类的内容来访问模型的属性,所以
我需要至少知道两件事之一(但如果你能回答这两件事就更好了):
- 如果我将控制器保留为如图所示,我如何在测试中访问模型的属性?
- 如果我将注释部分替换为未注释部分,我如何在 jsp 视图中访问模型的属性?
任何帮助表示赞赏。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于问题 1,Model 提供了一种以映射形式返回模型属性的方法。在您的测试中,您可以执行以下操作:
假设您将 companyInfo 设置到模型中,它应该在那里。
至于问题的第二部分,我想其他人已经回答了。
For question 1, Model provides a method that returns the model attributes as a map. In your test, you can do:
Assuming you set companyInfo into the model, it should be there.
As for part2 of the question, I think someone else answered that already.
好吧,现在清楚了!
尝试:
您在控制器中将模型映射称为“模型”...
在您的 jsp 中,我建议使用 Jstl:
Ok, now its clear!
Try:
You called your modelmap 'model' in your controller...
In your jsp i would recommend using Jstl:
您忘记使用视图名称调用 ModelAndView 的构造函数,并且忘记将对象添加到模型中。
我认为你的代码应该看起来像这样......
如果你需要使用自定义键添加多个对象,请使用 addObject 方法;
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...
If you need to add more than one object with custom keys use the addObject method instead;
@pedrofalcaocosta,我对你的答案投赞成票,因为它帮助我找到了答案,但我认为在这里回答我自己的问题是合适的:
@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: