Reproduced in: http://yedehua.iteye.com/blog/657156
Package: spring-test.jar
SSH (both struts1.x and struts2.x are similar, you only need to modify the parameters of the call method) test:
Java code
Package com. swcrm. Service;
Import JUnit. Framework. testcase;
Import org. springframework. Mock. Web. mockhttpservletrequest;
Import org. springframework. Mock. Web. mockhttpservletresponse;
Import org. springframework. Mock. Web. mockservletcontext;
Import org. springframework. Web. Context. Support. xmlwebapplicationcontext;
Import com. swcrm. Action. svsourceaction;
Public class testaction extends testcase {
Private xmlwebapplicationcontext CTX;
Public void setup (){
String [] paths = {
"File:/D:/Documents and Settings/workspace/swcrm/webroot/WEB-INF/spring-config.xml "};
CTX = new xmlwebapplicationcontext ();
CTX. setconfiglocations (paths );
CTX. setservletcontext (New mockservletcontext (""));
CTX. Refresh ();
}
Public void testgetusers () throws exception {
Mockhttpservletrequest request = new mockhttpservletrequest ();
Mockhttpservletresponse response = new mockhttpservletresponse ();
Request. addparameter ("industry", "chelaile ");
Svsourceaction c = (svsourceaction) CTX. getbean ("/svsource ");
C. svsourcelist (null, null, request, response );
}
}
Test in hibernate + spring:
Java code
1. Package com. swcrm. Service;
2.
3.
4. Import JUnit. Framework. testcase;
5.
6. Import org. springframework. Mock. Web. mockhttpservletrequest;
7. Import org. springframework. Mock. Web. mockhttpservletresponse;
8. Import org. springframework. Mock. Web. mockservletcontext;
9. Import org. springframework. Web. Context. Support. xmlwebapplicationcontext;
10. Import com. swcrm. Action. svsourceaction;
11.
12. Public class testaction extends testcase {
13.
14. Private xmlwebapplicationcontext CTX;
15.
16. Public void setup (){
17. String [] paths = {
18. "file:/D:/Documents and Settings/workspace/swcrm/webroot/WEB-INF/spring-config.xml "};
19. CTX = new xmlwebapplicationcontext ();
20. CTX. setconfiglocations (paths );
21. CTX. setservletcontext (New mockservletcontext (""));
22. CTX. Refresh ();
23 .}
24.
25. Public void testgetusers () throws exception {
26. mockhttpservletrequest request = new mockhttpservletrequest ();
27. mockhttpservletresponse response = new mockhttpservletresponse ();
28. Request. addparameter ("industry", "chelaile ");
29. svsourceaction c = (svsourceaction) CTX. getbean ("svsource ");
30. modelandview MAV = C. svsourcelist (request, response );
31. Map M = Mav. GetModel ();
32 .}
33 .}