Junit參數化測試的使用方法

來源:互聯網
上載者:User
junit參數化測試的使用方法JUnit參數化測試的五個步驟:

(1)為準備使用參數化測試的測試類別指定特殊的運行器 org.junit.runners.Parameterized。
(2)為測試類別聲明幾個變數,分別用於存放期望值和測試所用資料。
(3)為測試類別聲明一個帶有參數的公用建構函式,並在其中為第二個環節中聲明的幾個變數賦值。
(4)為測試類別聲明一個使用註解 org.junit.runners.Parameterized.Parameters 修飾的,傳回值為 java.util.Collection 的公用靜態方法,並在此方法中初始化所有需要測試的參數對。

(5)編寫測試方法,使用定義的變數作為參數進行測試。 


package springMybatis.test;import java.util.Arrays;import java.util.Collection;import org.junit.BeforeClass;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.Parameterized;import org.junit.runners.Parameterized.Parameters;import springMybatis.service.IUserService;import springMybatis.util.FactoryManager;import springMybatis.vo.User;//(1)步驟一:測試類別指定特殊的運行器org.junit.runners.Parameterized@RunWith(Parameterized.class)public class TestSpringMybatis {private static IUserService userService = null;@BeforeClasspublic static void setUp() {FactoryManager.init("conf/beans/beans-springMybatis.xml");userService = (IUserService) FactoryManager.getBean("userService");}// (2)步驟二:為測試類別聲明幾個變數,分別用於存放期望值和測試所用資料。此處我只放了測試所有資料,沒放期望值。private int idParam;private String usernameParam;// (3)步驟三:為測試類別聲明一個帶有參數的公用建構函式,並在其中為第二個環節中聲明的幾個變數賦值。public TestSpringMybatis(int id, String username) {this.idParam = id;this.usernameParam = username;}// (4)步驟四:為測試類別聲明一個使用註解 org.junit.runners.Parameterized.Parameters 修飾的,傳回值為// java.util.Collection 的公用靜態方法,並在此方法中初始化所有需要測試的參數對。@Parameterspublic static Collection usernameData() {return Arrays.asList(new Object[][] { { 1, "jacky" }, { 2, "andy" },{ 3, "tomcat" }, });}// (5)步驟五:編寫測試方法,使用定義的變數作為參數進行測試。@Testpublic void testFindByName() {System.out.println("-------------");User user2 = userService.findByName(usernameParam);System.out.println(user2);}// (5)步驟五:編寫測試方法,使用定義的變數作為參數進行測試。@Testpublic void testFindById() {System.out.println("************");User user1 = userService.findById(idParam);System.out.println(user1);}}

輸出結果:
-------------
User [userid=1, account=1, username=jacky, gender=male, birthday=Fri Oct 11 09:00:00 CST 1963, email=jacky@xxx.net, address=beijing, school=hongkongmusicshcol, company=hillstone, phone=13900000000, interest=movie]
************
User [userid=1, account=1, username=jacky, gender=male, birthday=Fri Oct 11 09:00:00 CST 1963, email=jacky@xxx.net, address=beijing, school=hongkongmusicshcol, company=hillstone, phone=13900000000, interest=movie]
-------------
User [userid=2, account=2, username=andy, gender=male, birthday=Sat Oct 08 12:00:00 CST 1960, email=andy@xxx.net, address=hongkong, school=hongkongdaxue, company=bigstar, phone=13987654321, interest=music]
************
User [userid=2, account=2, username=andy, gender=male, birthday=Sat Oct 08 12:00:00 CST 1960, email=andy@xxx.net, address=hongkong, school=hongkongdaxue, company=bigstar, phone=13987654321, interest=music]
-------------
User [userid=3, account=3, username=tomcat, gender=male, birthday=Fri Oct 01 14:00:00 CST 1965, email=tomcat@xxx.net, address=hongkong, school=hongkongdaxue, company=bigstar, phone=13912345678, interest=programing]
************
User [userid=3, account=3, username=tomcat, gender=male, birthday=Fri Oct 01 14:00:00 CST 1965, email=tomcat@xxx.net, address=hongkong, school=hongkongdaxue, company=bigstar, phone=13912345678, interest=programing]

JUnit目前的參數化測試只支援一個@Parameters 要是能支援多個就更給力了。不過作為單元測試工具,已經基本滿足單元測試的要求了。

完整的項目原始碼地址:http://download.csdn.net/detail/chenyechao/4646087

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.