使用Spring Mock測試Action中的方法

來源:互聯網
上載者:User

轉載於:http://yedehua.iteye.com/blog/657156

 

使用包:spring-test.jar

ssh(struts1.x和struts2.x都差不多,只需把調用方法的參數修改就行)下測試:

Java代碼 

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", "車行");
        SvsourceAction c = (SvsourceAction)ctx.getBean("/svsource");
        c.SvsourceList(null, null, request, response);
       
}
}

 

hibernate+spring下測試:

Java代碼 
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", "車行");  
29.        SvsourceAction c = (SvsourceAction)ctx.getBean("svsource");  
30.        ModelAndView mav =c.SvsourceList(request, response);  
31.        Map m = mav.getModel();  
32.}  
33.} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.