Struts2 Action Junit 测试
记得刚学struts2时就了解了点struts2 的 action的测试方法,但是一直都没针对Action写过测试,最近稍微研究了下struts2 的 action测试,遇到了很多问题:
struts 2.0 之前
import static org.junit.Assert.*; import java.util.HashMap; import java.util.Map; import org.apache.struts2.StrutsTestCase; import org.junit.Test; import com.jack.lucene.service.LuceneSearchService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.DefaultActionProxyFactory; import com.opensymphony.xwork2.config.Configuration; import com.opensymphony.xwork2.config.ConfigurationManager; import com.opensymphony.xwork2.inject.Container; /** * @author jack * */ public class LuceneSearchHandlerTest extends StrutsTestCase { /** * Test method for {@link com.jack.lucene.handler.LuceneSearch#execute()}. * @throws Exception */ @Test public void testExecute() throws Exception { Map paramMap = new HashMap(); paramMap.put("keyWord", "grails 参考"); Map context = new HashMap(); context.put(ActionContext.PARAMETERS, paramMap); ConfigurationManager cm = new ConfigurationManager(); Configuration conf = cm.getConfiguration(); Container containter = conf.getContainer(); DefaultActionProxyFactory actionProxyFactory = new DefaultActionProxyFactory(); actionProxyFactory.setContainer(containter); ActionProxy proxy = actionProxyFactory.createActionProxy("/lucene","luceneSearch", context); LuceneSearch lsh = (LuceneSearch) proxy.getAction(); lsh.setLuceneSearchService(new LuceneSearchService()); String result = lsh.execute(); assertEquals("success", result); } }
但是如果你的项目是Spring管理的那么执行测试会报错:
要想测试只能暂时不用Spring去管理。或者用其他方法
SEVERE: [26:24.218] ********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION ********** Looks like the Spring listener was not configured for your web app! Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext. You might need to add the following to web.xml: <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
struts2.1 之后 请看这几篇文章
http://kang36897.blog.163.com/blog/static/170473732010710101238126/
相关推荐
shirleypaddy 2020-10-19
qingmumu 2020-10-19
lustdevil 2020-10-18
ganlulululu 2020-10-12
lustdevil 2020-08-03
lustdevil 2020-07-18
lustdevil 2020-06-25
lustdevil 2020-06-21
zhengzf0 2020-06-21
宿命java 2020-06-15
JackYin 2020-06-14
dongxurr 2020-06-07
snowphy 2020-06-06
zhengzf0 2020-05-28
81901836 2020-05-26
Julywhj 2020-05-26