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

Spring+Dubbo+TestNG接口测试

时间:2018-03-22 12:37:30      阅读:1166      评论:0      收藏:0      [点我收藏+]

标签:广播   water   server   tle   val   type   case   http   clu   

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"})
@Configuration
public 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);
    }

}

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

技术分享图片

Spring+Dubbo+TestNG接口测试

标签:广播   water   server   tle   val   type   case   http   clu   

原文地址:https://www.cnblogs.com/TestMa/p/8622548.html

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