Stack Trace Error
Failed to create autowire field: private javax.servlet.http.HttpServletRequest com.myapp.controller.ProductController.request; ... There is no bean type matching [ javax.servlet.http.HttpServletRequest ]
The problem is that simple Spring tests run with open ap context
Try adding the @WebAppConfiguration annotation to the test class.
Please note that this option is available only in versions of Spring 3.2+.
In earlier versions of Spring, you need to come up with something to implement the implementation implementations of this interface in the context of the application (the easiest way is to show the MockServletContext class and / or the HttpServletRequest interface as beans for tests),
Also note that code that uses the beans session scope is typically controller specific and relevant to the web application context , and I believe that it will be best practice to test controllers in separate tests using @WebAppConfiguration , but leave the tests plain old beans and services belonging to the root context of the application in simple application contexts, i.e. without @WebAppConfiguration .
Just to be clear, ProductDAO refers to the root context of the application, and ProductController refers to the context of the web application, so their definitions must be placed in different xml files. The ProductDAO test should only point to the xml context of the root application and not contain @WebAppConfiguration .
As for the ProductController test, you should specify both the context of the root application and the context xml files of the application context (see How to configure the web application context in the Spring MVC test for examples) and annotate with @WebAppConfiguration
source share