基于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.XXXX.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.xxx.xxx.xxx.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服务接口测试的更多相关文章

  1. 基于SpringBoot开发一个Restful服务,实现增删改查功能

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

  2. 基于Spring Cloud的微服务入门教程

    (本教程的原地址发布在本人的简书上:http://www.jianshu.com/p/947d57d042e7,若各位看官有什么问题或不同看法请在这里或简书留言,谢谢!) 本人也是前段时间才开始接触S ...

  3. 干货|基于 Spring Cloud 的微服务落地

    转自 微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的 ...

  4. 基于Spring Cloud的微服务落地

    微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的微服务 ...

  5. 基于 Spring Cloud 的微服务架构实践指南(下)

    show me the code and talk to me,做的出来更要说的明白 本文源码,请点击learnSpringCloud 我是布尔bl,你的支持是我分享的动力! 一.引入 上回 基于 S ...

  6. 基于 Spring Cloud 的微服务架构实践指南(上)

    show me the code and talk to me,做的出来更要说的明白 GitHub 项目learnSpringCloud同步收录 我是布尔bl,你的支持是我分享的动力! 一. 引入 上 ...

  7. Spring事务引发dubbo服务注册问题

    文章清单 1. 问题 2. 查找bug过程 3. 解决方案 使用spring boot+dubbo写项目,一个服务,之前是正常的,后来调用方出现空指针异常,第一反应提供方出了问题. 1. 看控制台,服 ...

  8. 基于Spring开发的一个BIO-RPC框架(对新人很友好)

    PART1:先来整体看下项目的构成 其中bio-rpc-core就是所谓的rpc框架 bio-rpc-example-client即所谓的服务调用方(你的项目中想要调用服务的地方) bio-rpc-e ...

  9. 基于Spring Boot的微服务搭建

    环境: 项目结构: 关键配置 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project ...

随机推荐

  1. iOS开发-Runloop详解(简书)

    不知道大家有没有想过这个问题,一个应用开始运行以后放在那里,如果不对它进行任何操作,这个应用就像静止了一样,不会自发的有任何动作发生,但是如果我们点击界面上的一个按钮,这个时候就会有对应的按钮响应事件 ...

  2. SVN被锁定解决办法

    转自:https://blog.csdn.net/strwangfan/article/details/78748393: 今天用SVN的时候出现被锁定的情况,既不能更新代码也不能提交. 解决方法如下 ...

  3. intValue()的用法

    今天看到了Integer的这个方法,有点疑惑,查了下,做下笔记; 1.intValue()是java.lang.Number类的方法,Number是一个抽象类.Java中所有的数值类都继承它.也就是说 ...

  4. php常用的系统函数

    字符串函数 strlen:获取字符串长度,字节长度 substr_count 某字符串出现的次数 substr:字符串截取,获取字符串(按照字节进行截取) mb_strlenmb_substr str ...

  5. js 各种取整方式及方法

    1.直接丢弃小数部分,保留整数部分 a:parseInt(1.5555) b: 0|1.5555 2.向上取整 a: Math.ceil(1.5555) b: (1.5555+0.5).toFixed ...

  6. hdu 1422(贪心)

    重温世界杯 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  7. zabbix获取到的数值自定义单位

    1) 查找php文件 # find / -name "func.inc.php" /usr/share/zabbix/include/func.inc.php 2)修改文件 #vi ...

  8. python os用法精简版

    import os print(os.getcwd()) #返回当前路径,无参数 print(os.listdir('E:\zsfile')) #该路径下所有文件名 os.remove('E:\zsf ...

  9. shell脚本学习(二)

    shell传递参数 shell脚本在执行是可以传递参数,脚本内获取参数的格式为:$n,n为一个数字,1为第一个参数,2为第二个参数,以此类推 其中,$0代表了要执行的文件名 实例: 代码如下: #!/ ...

  10. python3类方法,实例方法和静态方法

    今天简单总结下python的类方法,实例方法,静态方法. python默认都是实例方法,也就是说,只能实例对象才能调用这个方法. 那是不是说类方法也只能被类对象本身来调用呢,当然,不是.类方法既可以被 ...