JUnit之Rule的使用,JUnit之Rule使用

來源:互聯網
上載者:User

JUnit之Rule的使用,JUnit之Rule使用

簡介JUnit之Rule的簡單用法。為分析JUnit相關原始碼做點準備。

Rule是一個用於測試單元類如MyTest中定義一個域的標註,該域must be public, not static, and a subtype of  org.junit.rules.MethodRule。

package org.junit;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)public @interface Rule {}

Kent Beck曾經寫過一篇文章<Interceptors in JUnit>,雖然最後叫Rule,但仍然起攔截器/Interceptor的作用——即在運行測試的前後添加一些有用的代碼。

【註:JUnit4.9開始,MethodRule被deprecated,TestRule取代它。MethodRule介面定義的唯一方法:

Statement apply(Statement base, FrameworkMethod method, Object target);

TestRule的對應物:

Statement apply(Statement base, Description description);

】(嚴重影響yqj2065閱讀4.8.2原始碼的心情)

①先為MyRule準備一個Statement 

package myTest.rule;import static tool.Print.*;//pln(Object)import org.junit.runners.model.Statement;public class MyStatement extends Statement {    private final Statement base;    public MyStatement( Statement base ) {        this.base = base;    }    @Override public void evaluate() throws Throwable {        pln( "before...sth..sth" );        try {            base.evaluate();        } finally {             pln( "after...sth..sth" );        }    }}

②定義MyRule

package myTest.rule;//import org.junit.runner .Description;import org.junit.rules.MethodRule;import org.junit.runners.model.Statement;import org.junit.runners.model.FrameworkMethod;public class MyRule implements MethodRule  {    @Override    public Statement apply( Statement base, FrameworkMethod method,  Object target ) {        return new MyStatement( base );    }}

③最後在測試單元類MyTest中使用Rule

package myTest.rule;import org.junit.Rule;import org.junit.Test;public class MyTest {    @Rule    public MyRule myRule = new MyRule();    @Test    public void testCase() {        System.out.println( "testCase()..." );    }}

運行testCase()的輸出:

before...sth..sth
testCase()...
after...sth..sth


JUnit test 怎做Java靜態方法測試

測試只允許你使用void 方法 如果說你別的方法是靜態有傳回值的 你完全可以封裝一成
 
java中架構Junit應該怎使用?

JUnit

JUnit是由 Erich Gamma 和 Kent Beck 編寫的一個迴歸測試架構(regression testing framework)。Junit測試是程式員測試,即所謂白盒測試,因為程式員知道被測試的軟體如何(How)完成功能和完成什麼樣(What)的功能。Junit是一套架構,繼承TestCase類,就可以用Junit進行自動化的測試了。
更多JUnit資訊

Cactus

Cactus是一個基於JUnit架構的簡單測試架構,用來單元測試服務端Java代碼。Cactus架構的主要目標是能夠單元測試服務端的使用Servlet對象的Java方法如HttpServletRequest,HttpServletResponse,HttpSession等
更多Cactus 資訊

Abbot

Abbot是一個用來測試Java GUIs的架構。用簡單的基於XML的指令碼或者Java代碼,你就可以開始一個GUI。
更多Abbot資訊

JUnitPerf

Junitperf實際是junit的一個decorator,通過編寫用於junitperf的單元測試,我們也可使測試過程自動化。
更多JUnitPerf資訊

DbUnit

DbUnit是為資料庫驅動的項目提供的一個對JUnit 的擴充,除了提供一些常用功能,它可以將你的資料庫置於一個測試輪迴之間的狀態。
更多DbUnit資訊

Mockrunner

Mockrunner用在J2EE環境中進行應用程式的單元測試。它不僅支援Struts actions, servlets,過濾器和標籤類還包括一個JDBC和一個JMS測試架構,可以用於測試基於EJB的應用程式。
更多Mockrunner資訊

DBMonster

DBMonster是一個用產生隨機資料來測試SQL資料庫的壓力測試工具。
更多DBMonster資訊

MockEJB

MockEJB是一個不需要EJB容器就能運行EJB並進行測試的輕量級架構。
更多MockEJB資訊

StrutsTestCase

StrutsTestCase 是Junit TestCase類的擴充,提供基於Struts架構的代碼測試。StrutsTestCase同時提供Mock 對象方法和Cactus方法用來實際運行Struts ActionServlet,你可以通過運行servlet引擎來測試。因為StrutsTestCase使用ActionServlet控制器來測試你的代碼,因此你不僅可以測試Action對象的實現,而且可以測試mappings,from beans以及forwards聲明。StrutsTestCase不啟動servlet容器來測試struts應用程式(容器外測試)也屬於Mock對象測試,但是與EasyMock不同的是,EasyMock是提供了建立Mock對象的API,而StrutsTest則是專門負責測試Struts應用程式的Mock對象測試架構。
更多StrutsTestCase資訊

JFCUnit

JFCUnit使得你能夠為Java位移應用程式編寫測試例子。它為從用代碼開啟的視窗上獲得控制代碼提供了支援;為在一個組件層次定位組件提供支援;為在組件中發起事件(例如按一個按鈕)以及以安全執行緒方式處理組件測試提供支援。
更多JFCUnit資訊

JTestCase

JTestCase 使用XML檔案來組織多測試案例資料,聲明條件(操作和期望的結果),提供了一套便於使用的方法來檢索......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.