您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
Junit參數(shù)化測試Spring應用Dubbo接口
作者:網(wǎng)絡轉(zhuǎn)載 發(fā)布時間:[ 2017/3/9 13:43:18 ] 推薦標簽:單元測試 軟件測試工具

  一、創(chuàng)建基礎類。
package com.tree.autotest;
import org.junit.Before;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
/**
* 規(guī)則包:
* 規(guī)則編碼:
* 規(guī)則名稱:
* 規(guī)則條件:
* <p>
* Created by zhf2015 on 16/8/1.
*/
//@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring-context.xml"})
@Configuration
public class BaseJunit4Test extends AbstractJUnit4SpringContextTests {
protected TestContextManager testContextManager;
@Before
public void setUpContext() throws Exception {
this.testContextManager = new TestContextManager(getClass());
this.testContextManager.prepareTestInstance(this);
}
}
  二、創(chuàng)建測試類,繼承基礎類。
package com.tree.autotest.testcase.BillDetailTypeService;
import com.datatrees.basisdata.bankbill.model.Bill;
import com.datatrees.basisdata.bankbill.service.BillService;
import com.tree.autotest.BaseJunit4Test;
import com.tree.autotest.commons.CommonUtil_2;
import com.tree.autotest.commons.TestCase;
import com.tree.autotest.jdbc.DBIdConstant;
import com.tree.autotest.jdbc.ManagedSqlUtil;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* Created by lyh on 17/2/13.
*/
@RunWith(value=Parameterized.class)
public class TestSelectListByUserId2 extends BaseJunit4Test{
//    ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:/spring-context.xml");
@Autowired
private BillService billService;
//    private BillService billService = (BillService)factory.getBean("billService");
private static final Logger logger = LoggerFactory.getLogger(TestSelectListByUserId2.class);
private boolean handleOk;
private String insertSql1;
private static String excel;
private static Map allCases;
private Integer testUser;
@Parameterized.Parameters
public static Collection<Integer[]> getTestParameters(){
List<Integer[]> list = new ArrayList<Integer[]>();
list.add(new Integer[]{2000998248});  //expected,valueOne,valueTwo
list.add(new Integer[]{2000020021});
list.add(new Integer[]{2001999335});
String st=list.toString();
System.out.println("list值:" + st);
return list;
}
public TestSelectListByUserId2(Integer userid){
this.testUser = userid;
}
@Before
public void setUp() throws SQLException {
//獲取excel用例列表
excel = "src/main/resources/case/BillService/TestSelectListByUserId.xlsx";
allCases = CommonUtil_2.getAllCases(excel);
System.out.println("setUp 執(zhí)行");
//        //獲取用戶ID
TestCase testcase = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-1");
testUser =Integer.valueOf(testcase.param1);
//獲取SQL
insertSql1 = testcase.stepSql;
//刪除,添加數(shù)據(jù)
handleOk &= deleteData(testUser);
handleOk &= ManagedSqlUtil.insertBySql(insertSql1, DBIdConstant.MySql_Basisdata_ID);
}
@Test
public void test_case_1() throws Exception {
//++++++++++++++實際值+++++++++++++
List<Bill> actual_list =
billService.selectListByUserId(testUser);
System.out.println("testUser值:"+testUser);
//++++++++++++++驗證+++++++++++++
Assert.assertNotNull("測試失敗",actual_list);
for(int i=0;i<actual_list.size();i++) {
Assert.assertEquals("測試失敗",actual_list.get(i).getUserId(),testUser);
}
System.out.println("actual_list的size:"+actual_list.size());
}
@After
public void tearDown() {
deleteData(testUser);
System.out.println("tearDown 執(zhí)行");
}
//定義數(shù)據(jù)清理
public boolean deleteData(Integer userId) {
String deleteSql1 = "delete from t_bill where UserId="+userId+";";
handleOk &= ManagedSqlUtil.deleteBySql(deleteSql1, DBIdConstant.MySql_Basisdata_ID);
return handleOk;
}
}

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd