Junit test for Spring and Struts
2013-05-29 10:07
936 查看
/**
* 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", };
}
}
相关文章推荐
- StrutsTestCase for JUnit v2.1.3
- No result defined for action com.buaa.spring_test.action.TestAction and result input
- 新人解惑之—JUnit and SpringTest
- SpringBoot+Junit Test
- spring3.0.5+mybatis 3.04+struts2.2.3+junit 完整配置
- Spring Test 整合 JUnit 4 使用总结
- Spring Test 整合 JUnit 4 使用总结
- Spring Test 整合 JUnit 4 使用总结
- How do I set file.encoding for a junit test in ant?
- SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
- spring TestUtil Junit测试类
- java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail
- [MAVEN] - Add "provided" and "test" scopes for dependency
- Spring Test 整合 JUnit 4 使用总结
- No result defined for action org.action.StrutsAction and result success之解决
- Transforming and Selecting Functional Test Cases for Security Policy Testing
- Spring Test 整合 JUnit 4 使用总结
- Test Driven: TDD and Acceptance TDD for Java Developers
- ts2+Spring的UnitTest编写(使用StrutsTestCase的子类StrutsSpringTestCase)
- junit 测试spring-test 代码!在项目中运行测试通过,留着以后直接使用