Spring+Dubbo+TestNG接口测试初探

最近因工作原因,需要测试dubbo接口,通过公司同事写的框架,再结合度娘的帮助,自己做了一些总结记录。

通过下文意在说明如何搭建一个spring + dubbo + testng的测试环境,并完成一个简单的dubbo接口测试用例。

一、在Idea中新建一个maven工程

二、在pom.xml中添加相关依赖

    <dependencies>
        <!-- Spring框架依赖:用于构建Spring容器 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

        <!-- Spring单元测试依赖:用于测试类启动spring容器 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.5.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- dubbo依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 引入相应工程的jar包:*根据工程不同具体填入相应值-->
        <dependency>
            <groupId>com.*.*</groupId>
            <artifactId>*</artifactId>
            <version>*</version>
        </dependency>

        <!-- testng依赖:测试框架-->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>

        <!-- fastjson依赖:主要用于接口返回结果中解析json对象 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.54</version>
        </dependency>

        <!-- 流式断言器依赖:要比于junit和testng中的断言,assertj的api更强大-->
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.11.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

 三、Dubbo服务spring配置(resources目录下新建一个bean.xml文件)

由于测试过程是RPC(远程调用接口)的过程,测试项目相当于服务消费方Consumer,开发项目相当于服务提供方Provider,

所以测试只需要进行消费方的spring配置。xml的模板可以直接从要测试的dubbo接口对应的应用中拷贝;

<dubbo:application>必须声明,name和owner的值随便取(当然要取有一定意义的),没有对应关系;

<dubbo:reference>中url对应dubbo接口对应的dubbo服务地址和端口,

          id唯一即可,

          interface是dubbo服务的全限定类名

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

        <dubbo:application name="x1_consumer" owner="x2_consumer"/>
        <dubbo:reference
                url="dubbo://192.168.0.100:20860"
                id="testService"
                interface="com.*.*.service.TestService"
                timeout="30000"/>

</beans>

四、编写测试脚本

import com.*.*.TestService;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import javax.annotation.Resource;

//测试类需要继承AbstractTestNGSpringContextTests,如果不这么做测试类是无法启动Spring容器的
@ContextConfiguration(locations = "classpath:bean.xml") 
public class TestBase extends AbstractTestNGSpringContextTests {
    @Resource
    public TestService testService;

}

测试类需要继承AbstractTestNGSpringContextTests,如果不这么做测试类是无法启动Spring容器的

使用了[@ContextConfiguration]是为了加载被测试的Bean

import com.alibaba.fastjson.JSON;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;

public class TestSaveData extends TestBase {
private TestDTO testDTO = new TestDTO(); private List<Integer> categorys = new ArrayList<Integer>(); @Test public void TestSaveData_nameIsNull() { categorys.add(1); testDTO.setCategorys(categorys); ResponseDTO res = TestService.saveData(testDTO); System.out.println(JSON.toJSONString(res)); assertThat(res.getCode()).as("返回状态码").isEqualTo("2") .isBetween("1","2"); assertThat(res.getMessage()).isEqualTo("XX名称不能为空!"); assertThat(res.isSuccess()).isFalse(); } }

 

posted @ 2019-08-06 16:11  那心之所向  阅读(2285)  评论(1编辑  收藏  举报