6

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

Community
  • 1
  • 1
Leonel
  • 28,541
  • 26
  • 76
  • 103

1 Answers1

12

Create a bean of type MockHttpServletRequest and add it to your test context. This should then be autowired into your target bean.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Thanks, but how to "add it to your test context"? [Here](http://forum.springsource.org/showthread.php?69604-Adding-beans-to-application-context) guys haven't found a solution. – Dzmitry Lazerka Jun 18 '13 at 22:34