Junit test for Spring and Struts
来源:互联网 发布:淘宝软件代理加盟 编辑:程序博客网 时间:2024/05/20 13:39
/**
* Copyright 2013 the original author or authors.
*
* May 23, 2013
*/
import java.io.File;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import servletunit.struts.MockStrutsTestCase;
import com.ystech.struts.action.MYBaseActionServlet;
/**
* @author a5
*
*/
//继承 MockStrutsTestCase -- strutstest-2.1.3.jar 测试struts的action
public class SendEmailActionTest extends MockStrutsTestCase {
@Before
public void setUp() throws Exception {
super.setUp();
// 这三句加载struts配置文件
this.setContextDirectory(new File("WebContent"));
this.setServletConfigFile("/WEB-INF/web.xml");
this.setConfigFile("/WEB-INF/struts-config.xml");
// 创建一个spring的context
GenericWebApplicationContext webContext = new GenericWebApplicationContext();
BeanDefinitionReader beanReader = new XmlBeanDefinitionReader(webContext);
beanReader.loadBeanDefinitions(this.chooseSpringConfigs());
AnnotationConfigUtils.registerAnnotationConfigProcessors(webContext);
webContext.refresh();
webContext.registerShutdownHook();
// 将spring的context设置到webcontext里
this.context.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
webContext);
webContext.setServletContext(this.context);
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
public void testSendEmailCS203Pass(String badChar) throws ServletException {
// 加载自定义的servlet
MYBaseActionServlet myBaseActionServlet = new MYBaseActionServlet();
myBaseActionServlet.init(this.config);
this.actionServlet = (ActionServlet)myBaseActionServlet;
// 测试的action
this.setRequestPathInfo("/sendemail");
this.addRequestParameter("article_date", " 6:57 PM ET 05/22/13");
this.addRequestParameter("article_publisher", "Reuters");
this.addRequestParameter("sender", "test@fmr.com");
this.addRequestParameter("recipient", "test@fmr.com");
this.addRequestParameter("message", badChar);
// 执行action
this.actionPerform();
// 验证测试结果
this.verifyNoActionErrors();
}
public void testSendEmailCS203Faild(String badChar) {
this.setRequestPathInfo("/sendemail");
this.addRequestParameter("article_date", " 6:57 PM ET 05/22/13");
this.addRequestParameter("article_publisher", "Reuters");
this.addRequestParameter("sender", "test@fmr.com");
this.addRequestParameter("recipient", "test@fmr.com");
this.addRequestParameter("message", badChar);
this.actionPerform();
String[] errors= {"errors.cs203.knownGood"};
this.verifyActionErrors(errors);
}
@Test
public void testEmailGoodCharacter1() throws ServletException {
this.testSendEmailCS203Pass("\t");
}
@Test
public void testEmailBadCharacter1(){
this.testSendEmailCS203Faild("&");
}
/**
* {@inheritDoc}
*/
public String[] chooseSpringConfigs() {
return new String[] { "file:src/config/spring/propertiesContext.xml",
"file:src/config/spring/cs203Context.xml",
"file:src/config/spring/cacheContext.xml",
"file:src/config/spring/jcaContext.xml",
"file:src/config/spring/taskManagerContext.xml",
"file:src/config/spring/serviceContext.xml",
"file:src/config/spring/modelContext.xml",
"file:src/config/spring/daoContext.xml", };
}
}
* Copyright 2013 the original author or authors.
*
* May 23, 2013
*/
import java.io.File;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import servletunit.struts.MockStrutsTestCase;
import com.ystech.struts.action.MYBaseActionServlet;
/**
* @author a5
*
*/
//继承 MockStrutsTestCase -- strutstest-2.1.3.jar 测试struts的action
public class SendEmailActionTest extends MockStrutsTestCase {
@Before
public void setUp() throws Exception {
super.setUp();
// 这三句加载struts配置文件
this.setContextDirectory(new File("WebContent"));
this.setServletConfigFile("/WEB-INF/web.xml");
this.setConfigFile("/WEB-INF/struts-config.xml");
// 创建一个spring的context
GenericWebApplicationContext webContext = new GenericWebApplicationContext();
BeanDefinitionReader beanReader = new XmlBeanDefinitionReader(webContext);
beanReader.loadBeanDefinitions(this.chooseSpringConfigs());
AnnotationConfigUtils.registerAnnotationConfigProcessors(webContext);
webContext.refresh();
webContext.registerShutdownHook();
// 将spring的context设置到webcontext里
this.context.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
webContext);
webContext.setServletContext(this.context);
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
public void testSendEmailCS203Pass(String badChar) throws ServletException {
// 加载自定义的servlet
MYBaseActionServlet myBaseActionServlet = new MYBaseActionServlet();
myBaseActionServlet.init(this.config);
this.actionServlet = (ActionServlet)myBaseActionServlet;
// 测试的action
this.setRequestPathInfo("/sendemail");
this.addRequestParameter("article_date", " 6:57 PM ET 05/22/13");
this.addRequestParameter("article_publisher", "Reuters");
this.addRequestParameter("sender", "test@fmr.com");
this.addRequestParameter("recipient", "test@fmr.com");
this.addRequestParameter("message", badChar);
// 执行action
this.actionPerform();
// 验证测试结果
this.verifyNoActionErrors();
}
public void testSendEmailCS203Faild(String badChar) {
this.setRequestPathInfo("/sendemail");
this.addRequestParameter("article_date", " 6:57 PM ET 05/22/13");
this.addRequestParameter("article_publisher", "Reuters");
this.addRequestParameter("sender", "test@fmr.com");
this.addRequestParameter("recipient", "test@fmr.com");
this.addRequestParameter("message", badChar);
this.actionPerform();
String[] errors= {"errors.cs203.knownGood"};
this.verifyActionErrors(errors);
}
@Test
public void testEmailGoodCharacter1() throws ServletException {
this.testSendEmailCS203Pass("\t");
}
@Test
public void testEmailBadCharacter1(){
this.testSendEmailCS203Faild("&");
}
/**
* {@inheritDoc}
*/
public String[] chooseSpringConfigs() {
return new String[] { "file:src/config/spring/propertiesContext.xml",
"file:src/config/spring/cs203Context.xml",
"file:src/config/spring/cacheContext.xml",
"file:src/config/spring/jcaContext.xml",
"file:src/config/spring/taskManagerContext.xml",
"file:src/config/spring/serviceContext.xml",
"file:src/config/spring/modelContext.xml",
"file:src/config/spring/daoContext.xml", };
}
}
- Junit test for Spring and Struts
- struts spring integration and test using junit4
- powerMock spring junit test
- Spring单元测试:Spring Test+JUnit
- JUnit integration test with Spring
- spring test junit通用代码
- Spring Test+JUnit完美组合
- Spring Test+JUnit完美组合
- junit+spring Test.jar 单元测试
- Spring Test+JUnit完美组合
- Junit 测试之 Spring Test
- Jakarta Pitfalls : Time-Saving Solutions for Struts, Ant, JUnit, and Cactus
- Android JUnit Test for Eclipse使用入门
- Spring Test 整合 JUnit 4 使用总结
- Spring Test 整合 JUnit 4 使用总结
- Spring Test 整合 JUnit 4 实例
- Spring Test 整合 JUnit 4 使用总结
- Spring Test 整合 JUnit 4 使用总结
- 我的QQ显示暂时无法登录,请稍后再试,错误码,0x0006000d
- IO_数据流
- SSH中事务配置详解(使用拦截器)
- 敏感词过滤
- Oracle 修改带数据的字段类型
- Junit test for Spring and Struts
- Oracle数据库开发——使用OCILIB
- 20个jQuery分页插件和教程(附带实例)
- SSH中事务配置详解(所有Bean共享一个代理基类)
- VS中的虚空格
- Ubuntu linux中怎么查看自己的串口连接情况
- 中国展示世界最大军用战机3D打印钛合金零件
- ubuntu ipv6翻墙
- 新浪微博项目用到的表