美文网首页接口自动化测试集
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框架方法使用分享!

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