所有的源代碼在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)
頁面開始。