使用cactus测试ssh应用(2)

接1

接下来就是考虑如何测试struts了,之前使用的是servelttestcase,不能作为我实际应用的测试,不过有部分代码可以借鉴,那就是jetty的启动代码(事实上,在linux下,使用tomcat和jetty启动相差不了多少,时间都是耗在启动spring的beans上,当然测试下最好还是轻量级别比较好)。

尝试了cactusstrutstestcase和mockstrutstestcase,使用中发现,cactusstrutstestcase不查找begin开头的方法,另外还是那个config问题,所以最终我还是选择了mockstrutstestcase。

选择了mockstrutstestcase之后,按照要求调用了superclass的setup和teardown,按照strutstestcase网站上的例子(后来,我看了另外《Struts Recipes》,应该说这本比较适合我的这次测试,不过看的时候我的测试快要完成了……)启动,我知道这次启动肯定不会成功。
因为我把struts和spring整合过了,如果没有注入commonServices(我所调用的loginAction的服务接口)的话,是不能通过测试的。不过这次显示的是没有找到配置文件……靠着搜索引擎的帮助,我找到了一个英文版的cactus+struts测试的例子,我注意到里面提醒要设置上下文路径。因为例子中使用的是myeclipse编辑器(我以前用过,不过jsp编辑老是卡死,所以不用了)。我这里使用的是netbeans,其实有点类似,因为netbeans的工程结构和classpath不同,所以我必须设置上下文为工程的web(因为我并没有启动tomcat),那个netbeans放网页什么的地方。后来我发现这里其实也有提示:
[url]http://www.blogjava.net/wangxq/archive/2005/08/22/10713.html[/url]

然后启动jetty出现了我想象的contextlistener问题。单纯没有注入的struts类应用是没有问题的,不过考虑到是ssh应用,我必须考虑如何注入。找如何启动spring上下文,注入其实花了我很长时间,吃过一顿中饭,然后一直想这个问题头很痛,又去睡了几个小时。醒来的时候重新找资料,解决问题。

真得很痛苦,找到了资料,然后测试,虽然头不疼了(估计是眼睛痛)。在javaeye的网站上找到了一篇可以使用的例子:
[url]http://www.iteye.com/topic/18492[/url]
按照我的习惯修改了其中的代码,成功的界面:
[img]http://lh6.ggpht.com/_hEMQVYHGsAw/SZ-7Ct_UTsI/AAAAAAAAAZE/fEDedi3yBFs/s800/Screenshot-Hako%20-%20NetBeans%20IDE%206.5-2.png[/img]

接下来又是一个烦人的问题,就是假如我放入两个test,其实这个testsuite会启动两次(因为是在setup中启动webapplicationcontext),即使是jetty也会很浪费时间。spring+cactus其实资料不是非常正式,看到的都是要么偏spring(基本没有cactus代码的)或者偏cactus的。最终还是在搜索strutstestcase的时候找到了,找到了两篇,其实内容是一样的:
[url]http://www.searchfull.net/1171468.html[/url]
[url]http://tide2046.iteye.com/blog/210728[/url]

我注意到了里面的JunitHelper,然后复制了里面的代码,按照我自己的习惯修改了代码,最终测试结果如下:
[img]http://lh6.ggpht.com/_hEMQVYHGsAw/SZ_WrDtKeZI/AAAAAAAAAZY/ees1iRk9jvo/s800/Screenshot-Hako%20-%20NetBeans%20IDE%206.5-3.png[/img]

到此基本上就完成了,最终的代码如下:

首先是保存webapplicationcontext的类(静态内容)
package cn.edu.shu.lehu.hako.tests.actions;

import javax.servlet.ServletContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.context.WebApplicationContext;

/**
*
* @author xnnyygn
*/
public class SpringWebContextHelper {

public static String[] Configuration_Location;
private static XmlWebApplicationContext wac = null;
private static ClassPathXmlApplicationContext fsxac = null;


static {
Configuration_Location = new String[]{
"/WEB-INF/spring-base.xml",
"/WEB-INF/spring-services.xml",
"/WEB-INF/spring-struts.xml"};
}

public static void setWebApplicationContext(ServletContext context) {

if (context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
return;
}

if (wac == null) {
wac = new XmlWebApplicationContext();
wac.setServletContext(context);
wac.setConfigLocations(Configuration_Location);
wac.refresh();
}

context.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
}
}


然后是启动jetty的类,实际测试过,貌似用swingui或者awtui会出错,不太清楚原因。
package cn.edu.shu.lehu.hako.tests.actions;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.cactus.extension.jetty.Jetty5xTestSetup;

/**
*
* @author xnnyygn
*/
public class TestAllWithJetty {

public static Test suite() {
System.setProperty("cactus.contextURL", "your webapp url here");
TestSuite testSuite = new TestSuite("All test with jetty");
testSuite.addTestSuite(TestLoginAction.class);
return new Jetty5xTestSetup(testSuite);
}

public static void main(String[] args) {
junit.textui.TestRunner.run(TestAllWithJetty.suite());
}
}


最后是有两个测试方法的类:
package cn.edu.shu.lehu.hako.tests.actions;

import cn.edu.shu.lehu.hako.struts.forms.LoginForm;
import java.io.File;

import servletunit.struts.MockStrutsTestCase;

/**
*
* @author xnnyygn
*/
public class TestLoginAction extends MockStrutsTestCase {

private LoginForm loginForm = new LoginForm();

public TestLoginAction(String testName) {
super(testName);
}

@Override
protected void setUp() throws Exception {
super.setUp();
setContextDirectory(new File("web"));
SpringWebContextHelper.setWebApplicationContext(context);
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
}

public void testCorrectLogin() {
setRequestPathInfo("/login");

loginForm.setUsername("username");
loginForm.setPassword("password");
setActionForm(loginForm);

actionPerform();
verifyNoActionErrors();
}

public void testInCorrectLogin() {
setRequestPathInfo("/login");

loginForm.setUsername("username");
loginForm.setPassword("password2");
setActionForm(loginForm);

actionPerform();
verifyInputForward();
}
}


最后是所使用的包名:
[quote]spring-mock-2.0-rc3.jar
commons-codec-1.3.jar
aspectjrt-1.5.3.jar
cactus-core-framework.uberjar.jar
httpunit.jar(这个不清楚是否必须)
junit-3.8.2.jar
org.mortbay.jetty-5.1.9.jar
strutstest-2.14.jar[/quote]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章