美文网首页接口自动化测试集
moco-runner框架方法使用分享!

moco-runner框架方法使用分享!

作者: Coding测试 | 来源:发表于2021-03-21 22:05 被阅读0次

测试的前置往往离不开mock服务的调用

  • 对于moco框架网上大多是下载一个jar包,开启一个moco服务,然后调用写好的json配置文件使用。单对于多框架的封装使用,这种方式往往不适合使用场景。需要根据每次请求来调用具体的方法来开启和销毁moco服务。

测试涉及工具包

  • moco-runner
<!--        moco-->
        <!-- https://mvnrepository.com/artifact/com.github.dreamhead/moco-core -->
        <dependency>
            <groupId>com.github.dreamhead</groupId>
            <artifactId>moco-core</artifactId>
            <version>1.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.github.dreamhead/moco-junit -->
        <dependency>
            <groupId>com.github.dreamhead</groupId>
            <artifactId>moco-junit</artifactId>
            <version>1.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.github.dreamhead/moco-runner -->
        <dependency>
            <groupId>com.github.dreamhead</groupId>
            <artifactId>moco-runner</artifactId>
            <version>1.1.0</version>
        </dependency>
        <!--        moco end-->
  • json-path
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>2.5.0</version>
        </dependency>
  • testng
        <!--引入testNG-->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
        </dependency>

测试方法

  • moco服务开启,使用moco-runner的jsonHttpServer方法指定端口和json文件
 @BeforeTest
    public void inintStartMoco(){
        String path = this.getClass().getClassLoader().getResource("config.json").getPath().substring(1);
        HttpServer mocoServer = MocoJsonRunner.jsonHttpServer(18080, Moco.file(path));
        Runner.runner(mocoServer).start();
        log.info("开启mock服务");
    }

moco默认会把Headers信息赋值上


  • 测试moco服务是否调用正常
 @Test
    public void testCase(){
        String apiPath = "http://localhost:18080/hr/regist/insertRegist";
        String response = JsonUtils.jsonFormatter( RestTemplateUtils.get(apiPath,String.class).getBody());
        log.info(response);
        Object document = Configuration.defaultConfiguration().jsonProvider().parse(response);
        String author0 = JsonPath.read(document, "$.data[1].key4");
        System.out.println("取出当前结果:"+author0);
    }
    @Test
    public void testCase1(){
        String apiPath = "http://localhost:18080/hr/regist/findRegist";
        String response = JsonUtils.jsonFormatter( RestTemplateUtils.get(apiPath,String.class).getBody());
        log.info(response);
        Object document = Configuration.defaultConfiguration().jsonProvider().parse(response);
        String author0 = JsonPath.read(document, "$.userMobile");
        System.out.println("取出当前结果:"+author0);
    }

RestTemplateUtils工具类是封装了一些springboot 的springframework下的http协议。JsonUtils是josn与map或者list类型的转换工具,需要的话可以下载源码使用->源码地址下载
说明:moco服务的开启和销毁生命周期与http请求和响应一致,并不是一直开启的。

测试结果-控制台信息

20 三月 2021 10:02:04 [main] INFO  开启mock服务
20 三月 2021 10:02:04 [pool-1-thread-2] INFO  Request received:
GET /hr/regist/insertRegist HTTP/1.1
content-length: 0
Accept: text/plain, application/json, application/*+json, */*
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.13 (Java/1.8.0_181)
Host: localhost:18080
Accept-Encoding: gzip,deflate
20 三月 2021 10:02:04 [pool-1-thread-2] INFO  Response return:

HTTP/1.1 200
Content-Length: 112
Content-Type: application/json; charset=utf-8
20 三月 2021 10:02:04 [main] INFO  {
  "code": 200,
  "userid": "userid123456",
  "data": [
    {
      "key1": "value",
      "key2": "value2"
    },
    {
      "key3": "value3",
      "key4": "value4"
    }
  ]
}
取出当前结果:value4


20 三月 2021 10:02:04 [pool-1-thread-2] INFO  Request received:

GET /hr/regist/findRegist HTTP/1.1
content-length: 0
Accept: text/plain, application/json, application/*+json, */*
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.13 (Java/1.8.0_181)
Host: localhost:18080
Accept-Encoding: gzip,deflate
20 三月 2021 10:02:04 [pool-1-thread-2] INFO  Response return:
HTTP/1.1 200
Content-Length: 143
Content-Type: application/json; charset=utf-8
20 三月 2021 10:02:04 [main] INFO  {
  "gender": "12",
  "notifyEntryTime": "2021-01-06",
  "userEmail": "1213@qq.com",
  "userMobile": "13000000000",
  "userName": "testname1",
  "wxAccount": "123kg1"
}
取出当前结果:13000000000

相关文章

  • moco-runner框架方法使用分享!

    测试的前置往往离不开mock服务的调用 对于moco框架网上大多是下载一个jar包,开启一个moco服务,然后调用...

  • Mock接口模拟框架

    一、导入moco-runner框架 下载链接:https://repo1.maven.org/maven2/com...

  • Quartz2D - 1

    开源框架CorePlot oc oc使用方法: Swift swift 使用context的方法:

  • 快速使用 Hexo 搭建个人博客

    课程讲解了目前最常用的博客搭建框架 Hexo 的使用方法以及使用技巧,分享了大量的必备插件、图床部署以及本地编辑的...

  • android权限管理之PermissionsDispatche

    android权限管理之PermissionsDispatcher使用 使用开源管理框架 使用方法 1.使用And...

  • 详解JQuery框架的五大选择器

    摘要:今天来和大家分享一下JQuery的五种选择器的详细使用方法。 本文分享自华为云社区《【JQuery框架】五大...

  • ThinkCMF框架使用分享

    公司开发一直使用的是OneThink框架,由于需求需要,想写一个系统,但是又要和公司系统不完全分离,故数据库要使用...

  • iOS热更新_无需发布新版本实现app更新

    实现热更新的方法: 使用FaceBook 的开源框架 reactive native 使用JSpatch JSPa...

  • 视频播放

    视频播放 方法一 最新的框架需要导入两个框架才能使用 , 使用方法: 两种播放样式 :样式一:再重开一个控制器,...

  • express

    express (非破坏式框架,不影响node原生方法的使用)

网友评论

    本文标题:moco-runner框架方法使用分享!

    本文链接:https://www.haomeiwen.com/subject/orwicltx.html