Spring 3 MVC Controller集成测试-将Principal注入到方法中
作为 Spring 3 MVC 的一部分,可以将当前登录的用户(原理)对象注入到控制器方法中。
例如,
@Controller
public class MyController {
@RequestMapping(value="/update", method = RequestMethod.POST)
public String update(ModelMap model, Principal principal) {
String name = principal.getName();
... the rest here
}
}
这作为 Spring 3 文档的一部分记录在此处: http://static。 springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-arguments。
这适用于生产代码。但是我不知道如何测试这个。 当我创建集成测试时(也设置了 spring 安全上下文) 并调用控制器句柄方法,则主体始终为空!
public class FareTypeControllerIntegrationTest extends SpringTestBase {
@Autowired
private MyController controller;
@Autowired
private AnnotationMethodHandlerAdapter handlerAdapter;
private final MockHttpServletRequest request = new MockHttpServletRequest();
private final MockHttpServletResponse response = new MockHttpServletResponse();
@Test
public void testUpdate() throws Exception {
request.setRequestURI("/update");
request.setMethod(HttpMethod.POST.name());
... setup rest of request
ModelAndView mav = handlerAdapter.handle(request, response, controller);
.. rest of assertions
}
测试运行正确,并且除主体之外的所有内容均为空。
有什么想法吗?
蒂亚·
阿尤布
As part of Spring 3 MVC it is possible to inject the currently logged in user (Principle) object into a controller method.
E.g.
@Controller
public class MyController {
@RequestMapping(value="/update", method = RequestMethod.POST)
public String update(ModelMap model, Principal principal) {
String name = principal.getName();
... the rest here
}
}
This is documented as part of the Spring 3 documentation here:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-arguments.
This works in the production code. However I don't know how to test this.
When I create an integration test (having set up spring security context as well)
and call the controller handle method then the Principal is always null!
public class FareTypeControllerIntegrationTest extends SpringTestBase {
@Autowired
private MyController controller;
@Autowired
private AnnotationMethodHandlerAdapter handlerAdapter;
private final MockHttpServletRequest request = new MockHttpServletRequest();
private final MockHttpServletResponse response = new MockHttpServletResponse();
@Test
public void testUpdate() throws Exception {
request.setRequestURI("/update");
request.setMethod(HttpMethod.POST.name());
... setup rest of request
ModelAndView mav = handlerAdapter.handle(request, response, controller);
.. rest of assertions
}
The tests are running correctly and everything except the Principal is null.
Any ideas?
TIA
Ayub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
快速浏览一下 Spring 源代码后,这应该可以工作:
After a quick look into Spring sources this should work:
我前段时间尝试过这样做,这是我用来设置身份验证的方法。
然后我只是在测试的 @Before 方法中调用它。
希望有帮助。
I've tried to do this some time ago, here is the method i used to set up authentication.
Then i just call it in the @Before method of the test.
Hope it helps.
在使用 Spring Security 调用代码(例如您正在测试的主体参数解析器)之前,我在测试中做了类似的事情:
I do something like this in my tests prior to calling code using Spring Security (such as the Principal parameter resolver you are testing):