您的位置:首页 > 编程语言 > Java开发

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", };

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: