I've injected HttpServletRequest into a bean. How do I unit test it?
I have a bean in which I've injected an HttpServletRequest
using the @Autowired
annotation.
This injection works correctly when the application context is a web application Context. That's not the case for application contexts for JUnit tests with Spring.
How can I test this bean ? Maybe I can mock an http request, but then how to inject this mock in the bean ?
开发者_运维百科This is on Spring 3.0 and Junit 4.4
Create a bean of type MockHttpServletRequest
and add it to your test context. This should then be autowired into your target bean.
精彩评论