开发者

I've injected HttpServletRequest into a bean. How do I unit test it?

开发者 https://www.devze.com 2023-01-13 12:18 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消