Junit参数化测试Spring应用Dubbo接口

发表于:2017-3-09 11:55

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:小学生II    来源:51Testing软件测试网采编

  一、创建基础类。
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;
/**
* 规则包:
* 规则编码:
* 规则名称:
* 规则条件:
* <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);
}
}
  二、创建测试类,继承基础类。
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 执行");
//        //获取用户ID
TestCase testcase = CommonUtil_2.getTestCaseBean(allCases, "case1", "case1-1");
testUser =Integer.valueOf(testcase.param1);
//获取SQL
insertSql1 = testcase.stepSql;
//删除,添加数据
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 执行");
}
//定义数据清理
public boolean deleteData(Integer userId) {
String deleteSql1 = "delete from t_bill where UserId="+userId+";";
handleOk &= ManagedSqlUtil.deleteBySql(deleteSql1, DBIdConstant.MySql_Basisdata_ID);
return handleOk;
}
}
  三、配置文件和数据文件和上一篇文章一致。
《2023软件测试行业现状调查报告》独家发布~

精彩评论

  • zxc1342802
    2017-9-13 17:31:39

    鸡肋,没有配置文件.找不到相关资料,垃圾网站

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号