JUnitEE【轉】

JUnitEE
 
翻譯:slovenboy
郵件:slovenboy AT yahoo dot com dot cn
 
轉載與發表聲明:請保留譯者信息並聯系我
 
指南(Tutorial)
 
本指南將說明如何使用JUnitEE對EJB做單元測試。
所有的源代碼在JUnitEE的分發(distribution)
的example目錄中,也可以在線(http://www.junitee.org/tutorial/index.html)瀏覽。
 
實現EJB
 
在此指南中使用的EJB是一個簡單的對兩個數字求和的會話Bean。查看
org.junitee.ejb.einstein 包中的源代碼來了解Bean中的工作機制。
addTwoNumbers方法有個小錯誤用於說明測試失敗的效果,方法emc2僅
拋出一個異常來導致測試錯誤。
 
編寫測試用例
 
測試用例是一個標準的JUnit“測試用例”(TestCase)。對於fixture,你
可以使用默認的JNDI InitialContext來獲取EJB引用,如下:
 
protected void setUp() throws Exception {
  Context jndiContext = new InitialContext();
  Object einRef = jndiContext.lookup("java:comp/env/ejb/EinsteinEJB");
  EinsteinHome home =
      (EinsteinHome)PortableRemoteObject.narrow(einRef, EinsteinHome.class);
  this.ein = home.create();
}
 
測試方法象這樣編寫:
 
public void testSimpleAddition() throws RemoteException {
 String result = this.ein.addTwoNumbers("7", "10");
 assert(result.equals("17"));
}
 
測試用例的全部代碼包含在示例中—查看org.junitee.ejb.einstein.test包。
 
創建前端Web表單
 
JUnitEEServlet執行由"suite"表單參數指定的測試用例,它可以出現多此。
使用一個簡單的表單來開始測試:
 
<html>
<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.xml部署描述符

Web應用必須右部署描述符,它提供ejb-fef映射
這樣“java:comp/env/ejb/EinsteinEJB” JNDI查詢
才能工作。在web.xml中JUnitEEServlet和它的URL映
射也是必須的。這是一個例子:
 
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
        "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
  <display-name> Einstein Unit Tester Web Application </display-name>
  <servlet>
    <servlet-name>JUnitEETestServlet</servlet-name>
    <description>JUnitEE test framework</description>
    <servlet-class>org.junitee.servlet.JUnitEEServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>JUnitEETestServlet</servlet-name>
    <url-pattern>/TestServlet/*</url-pattern>
  </servlet-mapping>
  <ejb-ref>
    <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應用服務器在它的URL模式匹配算法中有一個缺陷,對JUnitEE
引起一些問題。如果你是用Orion,請修改部署描述符中的Servlet映射爲
 
  <servlet-mapping>
    <servlet-name>JUnitEETestServlet</servlet-name>
    <url-pattern>/TestServlet*</url-pattern>
  </servlet-mapping>
 
打包用於測試的WAR文件
 
現在一切就緒可以打包用於測試的web應用了。必須放置
 × junit.jar 和 junitee.jar 到 WEB-INF/lib
 × 你的測試類到 WEB-INF/classes 或 包含測試類的jar文件到 WEB-INF/lib
 × 用於測試的前端表單到 index.html
 × web.xml 到 WEB-INF
最後,將所有文件使用jar命令打包爲test.war並創建一個EAR文件來包含test.war和
包含Einstein Bean的EJB jar文件。
 
運行測試
 
部署EAR文件後,將你的瀏覽器指向test.war文件中的index.html文件並選擇EinsteinTest
來執行。幾秒鐘後,瀏覽器中將顯示測試報告,說明成功的和失敗的測試。
= 讓你的工作更簡單
現在你已經瞭解如何創建一個包含JUnitEE servlet和你的測試類的.war文件。要創建一個war
文件需要幾步,但幸運地,有一種自動執行這些步驟地方法:使用爲Ant創建地JUnitEEWarTask。
看看Ant HowTo(http://www.junitee.org/antguide.html)學習更多與此任務相關地內容。
 
注意
 
當對EJB做單元測試時需要緊記的幾件事:
    × 除非你(和任何項目中的人)非常細心的編寫單元測試,否則很容易毀壞數據庫。
      可能你從未在一個產品系統(Production system)上運行測試,爲了防止這樣
      做(和其他的安全原因)你不應在產品機器上啓用測試Web應用。
    × 對getter和setter方法進行測試或許是在浪費時間
 
參考
 
你或許會感興趣和/或覺得有用的資源:
 × 獲取JUnit的信息,訪問JUnit主頁(http://www.junit.org)。
 × 瞭解極限編程,你可以閱讀“極限編程:一般介紹”(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)
   頁面開始。
 
--------------------------------
後記
此文翻譯有很多不足之處,歡迎批評指正:)
發佈了27 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章