Spring+Dubbo+TestNG接口测试

来源:互联网 发布:php imagejpeg 编辑:程序博客网 时间:2024/05/01 14:35

1、前言

该篇需要的基础是Spring结合TestNG搭建测试环境的知识,这块知识网上很多资料,本人就不在这里详细说明,接下来主要说说在搭Dubbo接口测试。

2、Dubbo

首先来了解一下Dubbo分布式服务框架,致力于高性能和透明化的RPC远程服务调用方案。

服务调用:
下面从Dubbo官网直接拿来,看一下基于RPC层,服务提供方和服务消费方之间的调用关系

这里写图片描述
具体知识自行学习。

当开发使用的是Dubbo进行搭建服务时,那我们如何测试该服务是否运行正常呢,功能是否正常呢,接下来我们来看看如何搭建测试环境

3、Dubbo服务接口测试环境搭建

前提先将Spring+testNG环境搭好先,再进行Dubbo接口测试环境搭建

1、在pom.xml引入Dubbo依赖包

    <dependency>      <groupId>com.101tec</groupId>      <artifactId>zkclient</artifactId>      <version>0.10</version>    </dependency>    <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->    <dependency>      <groupId>com.alibaba</groupId>      <artifactId>dubbo</artifactId>      <version>2.5.3</version>      <exclusions>        <exclusion>          <groupId>org.springframework</groupId>          <artifactId>spring</artifactId>        </exclusion>      </exclusions>    </dependency>

2、Pom.xml引入对应service应用jar依赖
比如:

<dependency>   <groupId>DubboS</groupId>   <artifactId>DubboS</artifactId>   <version>1.0-SNAPSHOT</version>  </dependency>

3、Dubbo服务spring配置
由于测试过程是远程调用接口的过程,所以只需要进行消费方spring配置。由于阿里云dubbo应用的测试环境属于外网,本地机器需将请求通过公网机器的端口转发給测试环境,需要在公网IPTable配置映射。没有经过注册中心,所以不用配置注册中心。
我们先看一下service配置如何:

    <!-- 具体的实现bean -->    <bean id="demoServer" class="com.dub.provider.impl.DemoServerImpl" />    <!-- 提供方应用信息,用于计算依赖关系 -->    <dubbo:application name="xs_provider" />    <!-- 使用multicast广播注册中心暴露服务地址 -->    <!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->    <!-- 使用zookeeper注册中心暴露/172.17.153.50:2181服务地址 -即zookeeper的所在服务器ip地址和端口号 -->    <dubbo:registry address="zookeeper://172.17.153.50:2181" />    <!-- 用dubbo协议在20880端口暴露服务 -->    <dubbo:protocol name="dubbo" port="20880" />    <!-- 声明需要暴露的服务接口 -->    <dubbo:service interface="com.dub.provider.DemoServer"                   ref="demoServer" />

那么我们在工程resources目录下新建一个xml文件,比如:spring-consumer.xml
只需要对每个service如下配置:

<dubbo:application name="hjy_consumer" />    <dubbo:reference interface="com.dub.provider.DemoServer" id="demoServer"        url="dubbo://172.17.153.50:20880" timeout="10000"    />

我们再在spring.xml文件引入资源配置

 <import resource="spring-consumer.xml"/>

4、Spring基本配置好了,那么我们可以进行编写测试脚本了

@ContextConfiguration(locations = {"classpath:Spring.xml"})@Configurationpublic class BaseTestNGTest extends AbstractTestNGSpringContextTests{    @Autowired    private DemoServer demoServer;    protected TestContextManager testContextManager;    @Before    public void setUp() throws Exception {//        this.testContextManager = new TestContextManager(getClass());//        this.testContextManager.prepareTestInstance(this);    }    @Test    public void testcase(){       String hel= demoServer.sayHello("liyulang");        System.out.printf(hel);    }}

当我使用自动注入对象时,提示无法注入

这里写图片描述

不过我运行测试后,发现可以正常运行,上面就是Dubbo服务接口测试环境搭建了,大家有什么问题可以询问本人
微信:happyLANG90

参考:

http://www.mamicode.com/info-detail-1716794.html

原创粉丝点击