顯示具有 junit 標籤的文章。 顯示所有文章
顯示具有 junit 標籤的文章。 顯示所有文章

2014年4月30日 星期三

ANT - build junit時出現Errors while applying transformations: Fatal error during transformation...


今天播點時間來實作『一年前』(沒錯...我拖了一年...)上課學習的東西,將我的TestCase搬到Jenkins上,並安裝emma plugin觀看Junit Testing Report,
首先第一件事情當然是要寫一個ANT Build,但執行的時候出現以下錯誤:

[junitreport] Processing /Users/wuanne/workspace/eclipse/XXXXTest/build/junitReport/TESTS-TestSuites.xml to /var/folders/59/jk74nmqj48n_6wksbv0lccw80000gn/T/null1033290394
[junitreport] Loading stylesheet jar:file:/Applications/eclipse/plugins/org.apache.ant_1.8.4.v201303080030/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] : Error! 非靜態 Java 函數 'replace' 的第一個引數不是有效的物件參照。
[junitreport] : Error! 無法編譯樣式表
[junitreport] : Fatal Error! 無法轉換 data-type 'void' 為 'reference'。 Cause: 無法轉換 data-type 'void' 為 'reference'。
[junitreport] Failed to process /Users/wuanne/workspace/eclipse/XXXXTest/build/junitReport/TESTS-TestSuites.xml

BUILD FAILED

/Users/wuanne/workspace/eclipse/XXXXTest/build/build.xml:90: Errors while applying transformations: Fatal error during transformation

報錯的build.xml:90為:
<target name="junit-report">
<junitreport todir="${junitReport.dir}">
...
</target>

當然這行看起來實在沒什麼問題,google了一下,參考上述討論網站的解答:
不要使用eclipse自帶的ANT,自行安裝ANT,並在eclipse->preference->Ant->Runtime,按下『Ant Home』設定自行安裝的ANT,重新build,就可以成功了~

2014年4月4日 星期五

Junit - 如何比較JsonArray是否相同?

參考:http://stackoverflow.com/questions/2253750/compare-two-json-objects-in-java

好久沒寫blog了,這陣子還是忙到昏天地暗,但也寫了一些HttpUnit的練習,有機會再來分享:)

json是很常使用的格式,我自己的專案也大量的運用它來傳遞訊息,那麼,如何比較兩個jsonArray是否相等呢?(或json)

用junit的assertEquals()是無法正確比較其中內容的,因此我們需要額外的方式:

  1. 下載JSONassert From Maven
  2. 使用:
    JSONAssert.assertEquals(expected,actual,false);
上述的第三個參數:boolean strict,說明,可參考doc上所寫:

There are two modes, strict and non-strict. In most cases, you will probably want to set strict to false, since that will make the tests less brittle.
Strict tests require all of the elements requested to be returned, and only those elements (ie, the tests are non-extensible). Arrays of elements must be returned in the same order as expected. For example, say I'm expecting:
{id:123,things['a','b','c']}The following would match when doing non-strict checking, but would fail on strict checking:
{id:123,things['c','b','a'],anotherfield:'blah'}

何時該使用strict(==true)就端看個人需求選擇囉~

2014年3月14日 星期五

cactus - 執行時出現 org.apache.cactus.util.ChainedRuntimeException: Failed to get the test results at...


今天撰寫FilterTest的時候執行cactus出現:

Failed to get the test results at [http://localhost:8080/CTBC_MI_Web/FilterRedirector]
org.apache.cactus.util.ChainedRuntimeException: Failed to get the test results at
...


其實原因就出在於web.xml沒有加上相對應的設定,因為我是filter出問題,因此應該加上下列:

    
        FilterRedirector
        org.apache.cactus.server.FilterTestRedirector
    

    
        FilterRedirector
        /FilterRedirector
    

之後再重新開啟WebServer就可以了.

2014年3月13日 星期四

selenium - 網頁有Alert視窗執行seleniumTest時出現:driver.close org.openqa.selenium.UnhandledAlertException: Modal dialog present:...


參考:忘記了

因為今天PM告知我一大堆檢核錯誤,看者他的手寫TestCase時我心裡想,那乾脆來寫個seleniumTest好了,幫助他也幫助到我自己,我檢核的流程如下:


  1. 輸入錯誤的使用者密碼,帳號
  2. 跳出相對應的alert訊息(須檢查各種錯誤訊息是否正確)

TestCase如下:

@Test
WebDriver _driver = null;
public void test() throws Exception {
//...
 WebElement textMerchno = _driver.findElement(By.xpath("//input[@id='merchno']"));
 textMerchno.sendKeys(_merchno);
 WebElement textUserid = _driver.findElement(By.xpath("//input[@id='userid']"));
        textUserid.sendKeys(_userid);

 WebElement textPwd = _driver.findElement(By.xpath("//input[@id='userpwd']"));
 textPwd.sendKeys(_userpwd);

 WebElement btn = _driver
  .findElement(By.xpath("//input[@type='button' and @class='table_button']"));
 btn.click();

 Alert alert = _driver.switchTo().alert();
 String actual = alert.getText();
 assertEquals(expectedMsg, actual);
//...
}
但是就在執行時出現了Exception:
 driver.close org.openqa.selenium.UnhandledAlertException: Modal dialog present:... 解決方式如下:
 
 Alert alert = _driver.switchTo().alert();
 String actual = alert.getText();
 _driver.switchTo().alert().accept();
 assertEquals(expectedMsg, actual);

只要加上_driver.switchTo().alert().accept(); 就可以啦~

2014年3月7日 星期五

cactus - 基本安裝與使用(Servlet測試)

參考:Junit In Action 2rd  Tomcat Quickstart forewords

這篇主要使用Tomcat + eclipse做使用環境,因此當然得先確認eclipse有妥善安裝,以及妥善設定好對應的Tomcat,這邊就不多作說明了.


  1. 首先至官網下載欲使用的cactus版本,並且解壓縮
  2. 在Eclipse中建立好Dynamic Web Project
  3. 將步驟1中的 \lib\下的 這幾個檔案找出來:
    >>cactus.jar
    >>commons-httpclient.jar
    >>commons-logging.jar
    >>junit.jar
    >>aspectjrt.jar
    請注意以上都忽略版本號的名稱
  4. 將上述檔案置放於步驟2建立好的專案下的WebContent\WEB-INF\lib下
  5. 建立欲測試的Servlet,如下:
  6.  
    public class TestServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      try{
       String reqBillDate = request.getParameter("reqBillDate");
       process(reqBillDate);
       //...
      }catch(...){
      }
    }
    }
    

  7. 新增cactus test(這裡新增一個package專門置放cactus TestCas)
    package ut.cactus;
    public class CactusTestServlet extends ServletTestCase{
    
     TestServlet _sv = new TestServlet();
    
     public void setUp(){
      // do something..
     }
    
     public void beginDoGet(WebRequest request){
      request.addParameter("reqBillDate", "20131212");
     }
     
     public void testDoGet() throws Exception{  
      _sv.doGet(request, response);
     }
     
     public void endDoGet(WebResponse response) throws Exception {
      String result = response.getText();
      assertEquals("expectedResult",result);
     }
    }
    

    關於上述簡單說明如下:
    >>setUp()與tearDown()為Junit標準使用不另外敘述
    >>beginXXX()與testXXX(),endXXX()為一對的,例如上例中的:beginDoGet(),testDoGet(),endDoGet().beginXXX()代表為做測試之前(執行testXXX()之前)欲做的準備,例如我們會在這邊塞入request的測試參數;而endXXX是做測試之後會被執行的,在這裡我們可以用來檢查servlet Response中的輸出結果.
  8. 準備好web.xml,除了原先就必須建立的TestServlet宣告之外,還必須加入下列:
    
      ServletRedirector
      org.apache.cactus.server.ServletTestRedirector
      
        param1
        value1 used for testing
      
    
    
    
      ServletTestRunner
      org.apache.cactus.server.runner.ServletTestRunner
    
    
        ServletRedirector
        /ServletRedirector
    
    
    
        ServletTestRunner
        /ServletTestRunner
    
    

    p.s:其實我很不希望Test與正是專案綁在一起,而且像上面還一併要得修改web.xml,但目前剛接觸cactus還不知道如何把它區別出來到另一個Test專案執行...
    也許得搭配使用Ant..
  9. 執行Tomcat(在這裡我是使用Eclipse執行)
  10. 輸入網址如下:
    http://localhost:8080/WebProject/ServletTestRunner?suite=ut.cactus. CactusTestServlet&xsl=cactus-report.xsl
    suite參數帶入的就是欲執行的TestCase全名(包含package)
  11. 觀看測試報表囉~~大致如下畫面:

2014年3月6日 星期四

cactus - 執行時出現 javax.servlet.ServletException: Failed to load test suite [xxxxx], Reason is [Class not found "xxxxx"]

參考:Error : “javax.servlet.ServletException: Failed to load test suite [SampleTest], Reason is [Class not found ”SampleTest“]
Cactus - Tomcat Quickstart forewords

第一次使用Cactus,按照官方教學操作,搞了老半天開始執行時卻出現錯誤:
javax.servlet.ServletException: Failed to load test suite [InvoiceSvTDDTest], Reason is [Class not found "InvoiceSvTDDTest"] org.apache.cactus.server.runner.ServletTestRunner.run(ServletTestRunner.java:303) org.apache.cactus.server.runner.ServletTestRunner.doGet_aroundBody0(ServletTestRunner.java:187) org.apache.cactus.server.runner.ServletTestRunner.doGet_aroundBody1$advice(ServletTestRunner.java:218) org.apache.cactus.server.runner.ServletTestRunner.doGet(ServletTestRunner.java:1) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) com.ctbc.mi.web.filter.ClearTempFilter.doFilter(ClearTempFilter.java:43) 

找了半天原來,這東東不支援欲測試的TestCase是有package的(20140307更新:有支援,請參考下方更新)... 哎,可能是還沒弄懂,我覺得用這個cactus測試蠻麻煩的,為了使用還得破壞原先的web.xml,而且還一定得要先啟動web-server端...這樣子很難測試耶...光環境上就要搞一大堆... 可能我還沒弄清楚吧..

2013/3/7更新:是可以使用package的,只要如下使用: http://localhost:8080/test/ServletTestRunner?suite=packagename.TestSampleServlet 就可以囉,
例如: http://localhost:8080/test/ServletTestRunner?suite=ut.test.pkg.TestSampleServlet 其中ut.test.pkg就是packag的全名了

2013年3月31日 星期日

JMock - 參考文章與網站





JUnit4 与 JMock 之双剑合璧:

http://www.ibm.com/developerworks/cn/java/j-lo-junit4jmock/


JMock官方網站Cookbook:
http://jmock.org/cookbook.html


良葛格:
http://caterpillar.onlyfun.net/Gossip/JUnit/JMock.html



In-process Web Integration Tests with Jetty and JWebUnit

http://johannesbrodwall.com/2006/12/10/in-process-web-integration-tests-with-jetty-and-jwebunit/