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"})

sales@spasvo.com