junit 测试spring mvc Controller

Controller

 

@Controller
public class DataSourceService  {

  @RequestMapping(value = "/bb/aa/{dataSource}", method = RequestMethod.GET)
    public void datasource(HttpServletRequest request, HttpServletResponse response,
            @PathVariable("dataSource") String dataSource) throws Exception {
        String result = "结果为";
        
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(result + dataSource);
        out.close();
        
    }
}

测试的基类

 

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.BeforeClass;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;

/**
 * @ClassName: JUnitActionBase
 * @Description: 这里用一句话描述这个类的作用
 *
 */
public class JUnitActionBase {
    private static HandlerMapping handlerMapping;
    private static HandlerAdapter handlerAdapter;
    
    /** 
     * 读取spring3 MVC配置文件 
     * @throws UnsupportedEncodingException 
     */
    @BeforeClass
    public static void setUp() throws UnsupportedEncodingException {
        if (handlerMapping == null) {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource("").getPath());
            String separator = File.separator;
            String path = URLDecoder.decode(file.getAbsolutePath(), "utf-8").replace("", "").replace("\\", "/");
            String[] configs = { "file:" + path + separator + "hy-spring-servlet.xml",
                    "file:" + path + separator + "hy-spring.xml" };
            XmlWebApplicationContext context = new XmlWebApplicationContext();
            context.setConfigLocations(configs);
            MockServletContext msc = new MockServletContext();
            context.setServletContext(msc);
            context.refresh();
            msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
            handlerMapping = (HandlerMapping) context.getBean(DefaultAnnotationHandlerMapping.class);
            handlerAdapter = (HandlerAdapter) context.getBean(context
                    .getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);
        }
    }
    
    /** 
     * 执行request对象请求的action 
     *  
     * @param request 
     * @param response 
     * @return 
     * @throws Exception 
     */
    public ModelAndView excuteAction(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HandlerExecutionChain chain = handlerMapping.getHandler(request);
        final ModelAndView model = handlerAdapter.handle(request, response, chain.getHandler());
        return model;
    }
}

 测试类

 

public class DataSourceServiceTest extends JUnitActionBase {

    @Test
    public void testJSONDataSourceServiceSQL() throws Exception {
        
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        
        request.setRequestURI("/bb/aa/hehe");
        
        request.addParameter("P_RQ", "2011-11-29");
        
        request.setMethod("GET");
        
        // 执行URI对应的action  
        this.excuteAction(request, response);
        String result = response.getContentAsString();
        Assert.assertNotNull(result);
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值