码迷,mamicode.com
首页 > 编程语言 > 详细

基于Spring开发的DUBBO服务接口测试

时间:2017-03-09 20:08:56      阅读:6744      评论:0      收藏:0      [点我收藏+]

标签:extc   excel   count   opera   数据驱动   通过   执行   开发   array   

             基于Spring开发的DUBBO服务接口测试

 

知识共享主要内容:

1、 Dubbo相关概念和架构,以及dubbo服务程序开发步骤。

2、 基于Spring开发框架的dubbo服务接口测试相关配置。

3、 spring test+junit和spring test+TestNG两种测试框架脚本编写方法。

 

一、        DUBBODUBBO架构

1、          什么是dubbo?DUBBO是一个分布式服框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

2、          DUBBO架构:

      

二、        Dubbo程序发过(提供者,服者,配置文件)

  1. 服务提供者:

1)       定义服务接口

2)       定义接口实现类

3)       Spring配置声明暴露服务:

    

4)       加载Spring配置

     

  1. 服务消费者:

5)       Spring配置引用远程服务

      

6)       加载Spring配置,并调用远程服务

u  ClassPathXmlApplicationContext加载配置,然后用getBean方法获取远程代理。

     

u  用IOC注入:测试脚本是用这种方式的。

 

三、        Dubbo服务接口测试环境准备

1、    POM.xml引入对应service应用jar依赖。

比如:

dependency>
    <groupId>com.datatrees.basisdata</groupId>
    <artifactId>basisdata-bankbill-common-facade</artifactId>
    <version>1.1.0</version>
</dependency>

2、    Dubbo服务spring配置

u  由于测试过程是远程调用接口的过程,所以只需要进行消费方spring配置。

u  由于阿里云dubbo应用的测试环境属于外网,本地机器需将请求通过公网机器的端口转发給测试环境,需要在公网IPTable配置映射。

u  没有经过注册中心,所以不用配置注册中心。

Spring-dubbo配置文件只需对每个service如下配置:

<dubbo:reference interface="com.datatrees.basisdata.bankbill.service.BillDetailService" id="billDetailService" url="dubbo://121.43.177.8:20100" timeout="10000"/>
然后在spring-context.xml加入引入资源配置即可。
<import resource="spring-secret.xml" /> 
 

四、        脚本设计结构

  1. 创建测试类公共父类,继承AbstractTestNGSpringContextTests或者AbstractJUnit4SpringContextTests。
  2. 创建测试类,继承父类,编写相应代码。

       

五、        脚本两种基本编写方法:

1、    继承AbstractJUnit4SpringContextTests方法。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring-context.xml"})
@Configuration
public class BaseJunit4Test extends AbstractJUnit4SpringContextTests {
}
 

2、    继承AbstractTestNGSpringContextTests方法。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring-context.xml"})
@Configuration
public class BaseTestNGTest extends AbstractTestNGSpringContextTests {
}
               测试类继承BaseTestNGTest即可。

六、        数据驱动两种基本编写方法:

1、 基于Junit数据驱动。

u  父类配置:

  • @RunWith(Parameterized.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);
        }
    }

接口测试类需编写一个构造类和一个由@Parameterized.Parameters参数数据方法

@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;
//    }

    List<Integer[]> list = new ArrayList<Integer[]>();

    list  =Arrays.asList(new   Integer[][]{{2000998248},{2000020021},{2001999335}});
    return list;
}
  • 构造方法:
  • public TestSelectListByUserId2(Integer userid){
        this.testUser = userid;
    }

2、 基于TESTNG数据驱动。

u  父类配置:

@ContextConfiguration(locations = {"classpath:/spring-context.xml"})
@Configuration
public class BaseTestNGTest extends AbstractTestNGSpringContextTests{
}

u  测试接口类需加一个由@DataProvider(name = "集合标识")注解的数据收集的方法,并将@Test(dataProvider="集合标识")給需要用参数的测试方法。

数据收集方法:
@DataProvider(name = "testdata")
public Object[][] dataprovide()throws IOException{
        System.out.println("dataprovide方法执行");
//        return new Object[][]{{2000020013,2},{2001000138,0},{2001000139,2}};
        Object[][] testData =ExcelHandle.readXlsx(excel, "工作表2");
        return testData;
    }

u  测试方法:

       @Test(dataProvider="testdata")
    public void test_case_1(HashMap<String, String> map) throws Exception {
        operatorUserId=Integer.valueOf(map.get("userId"));
        exceptedvalue =Integer.valueOf(map.get("excepted"));
       
        //++++++++++++++实际值+++++++++++++
        Integer actual_value =
                billService.getUserEmailNameCount(operatorUserId);

        //预期值
        Integer excepted_value =get_excepted_value(operatorUserId);
        //++++++++++++++验证+++++++++++++
        Assert.assertEquals(actual_value,exceptedvalue);
    }

 

基于Spring开发的DUBBO服务接口测试

标签:extc   excel   count   opera   数据驱动   通过   执行   开发   array   

原文地址:http://www.cnblogs.com/ceshi2016/p/6527210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!