SpringMVC是用Junit测试
作者:网络转载 发布时间:[ 2015/1/13 14:01:01 ] 推荐标签:软件测试工具 JUnit 白盒测试工具
测试action类继承它可以了
package test.java.action;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import com.pinfang.logic.service.UserService;
import test.test.JUnitActionBase;
/**
* action测试列子
* @author fule
*
*/
public class UserActionTest extends JUnitActionBase {
@Autowired UserService service;
@Test
public void testUserShow() throws Exception{
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setServletPath("/userManager/user.show");
request.addParameter("name", "张三");
request.addParameter("password", "123456");
request.setMethod("post");
request.setAttribute("msg", "测试action成功");
final ModelAndView mav = this.excuteAction(request, response);
Assert.assertEquals("userManager/userlist", mav.getViewName());
String msg=(String)request.getAttribute("msg");
System.out.println(msg);
}
}
|
配置文件记得声明两个bean:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
Spring读取WEB-INF下配置文件的方法:
配置文件放在class目录下:
view plaincopyprint?
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springMVCForm-servlet.xml");
WEB-INF下:
[java] view plaincopyprint?
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("WebContent/WEB-INF/springMVCForm-servlet.xml");
多个文件可用*表示:
[java] view plaincopyprint?
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("WebContent/WEB-INF/springMVCForm-*.xml");
注解方式:
配置文件放在class目录下:
[java] view plaincopyprint?
@ContextConfiguration(locations={"classpath:springMVCForm-servlet.xml"})
WEB-INF下:
view plaincopyprint?
@ContextConfiguration(locations={"file:WebContent/WEB-INF/springMVCForm-servlet.xml"})
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11