所有的源代码在JUnitEE的分发(distribution)
的example目录中,也可以在线( http://www.junitee.org/tutorial/index.html)浏览。
org.junitee.ejb.einstein 包中的源代码来了解Bean中的工作机制。
addTwoNumbers方法有个小错误用于说明测试失败的效果,方法emc2仅
抛出一个异常来导致测试错误。
可以使用默认的JNDI InitialContext来获取EJB引用,如下:
Context jndiContext = new InitialContext();
EinsteinHome home =
(EinsteinHome)PortableRemoteObject.narrow(einRef, EinsteinHome.class);
}
String result = this.ein.addTwoNumbers("7", "10");
assert(result.equals("17"));
}
使用一个简单的表单来开始测试:
<body>
<p>
You may type in the name of a test suite:
<br/>
<form action="TestServlet" method="get" name="youTypeItForm">
<input type="text" name="suite" size=60 />
<input type="submit" value="Run" />
</form>
</p>
<hr/>
<p>
You may pick one or more of the following test suites:
<br/>
<form action="TestServlet" method="get" name="youPickItForm">
<select name="suite" size="2" multiple>
<option value="org.infohazard.test.EinsteinTest">
org.infohazard.test.EinsteinTest
</option>
<option value="some.other.Test">
some.other.Test
</option>
</select>
<input type="submit" value="Run" />
</form>
</p>
</body>
</html>
Web应用必须右部署描述符,它提供ejb-fef映射
这样“java:comp/env/ejb/EinsteinEJB” JNDI查询
才能工作。在web.xml中JUnitEEServlet和它的URL映
射也是必须的。这是一个例子:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
" http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<display-name> Einstein Unit Tester Web Application </display-name>
<servlet-name>JUnitEETestServlet</servlet-name>
<description>JUnitEE test framework</description>
<servlet-class>org.junitee.servlet.JUnitEEServlet</servlet-class>
</servlet>
<servlet-name>JUnitEETestServlet</servlet-name>
<url-pattern>/TestServlet/*</url-pattern>
</servlet-mapping>
<ejb-ref-name>ejb/EinsteinEJB</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>org.infohazard.ejb.einstein.EinsteinHome</home>
<remote>org.infohazard.ejb.einstein.Einstein</remote>
</ejb-ref>
</web-app>
引起一些问题。如果你是用Orion,请修改部署描述符中的Servlet映射为
<servlet-name>JUnitEETestServlet</servlet-name>
<url-pattern>/TestServlet*</url-pattern>
</servlet-mapping>
× 你的测试类到 WEB-INF/classes 或 包含测试类的jar文件到 WEB-INF/lib
× 用于测试的前端表单到 index.html
× web.xml 到 WEB-INF
包含Einstein Bean的EJB jar文件。
来执行。几秒钟后,浏览器中将显示测试报告,说明成功的和失败的测试。
文件需要几步,但幸运地,有一种自动执行这些步骤地方法:使用为Ant创建地JUnitEEWarTask。
看看Ant HowTo( http://www.junitee.org/antguide.html)学习更多与此任务相关地内容。
可能你从未在一个产品系统(Production system)上运行测试,为了防止这样
做(和其他的安全原因)你不应在产品机器上启用测试Web应用。
× 对getter和setter方法进行测试或许是在浪费时间
× 了解极限编程,你可以阅读“极限编程:一般介绍”( http://www.extremeprogramming.org/)
然后访问XProgramming.com( http://www.xprogramming.com/)网站。
× 一个极好的模式,奇闻和其他有用信息的资源是Portland Pattern Repostory
( http://www.c2.com/cgi/wiki?WelcomeVisitors)网站。你可以从EjbUnitTest
( http://www.c2.com/cgi/wiki?EjbUnitTest)页面或JunitEe( http://www.c2.com/cgi/wiki?JunitEe)
页面开始。
2034




被折叠的 条评论
为什么被折叠?



