ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

moco 响应信息从 外部文件读取demo

2020-04-07 11:58:13  阅读:309  来源: 互联网

标签:dreamhead 读取 runner demo import org moco com



package mocker;
import java.io.IOException;
import java.nio.charset.Charset;


import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Request;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


import com.github.dreamhead.moco.HttpServer;
import com.github.dreamhead.moco.Moco;
import com.github.dreamhead.moco.MocoJsonRunner;
import com.github.dreamhead.moco.Runner;
import com.github.dreamhead.moco.resource.ContentResource;
public class MocoJsonHttpRunnerTest {
//用runner来管理服务器的开关,这个runner 对象是用Runner.runner(Server server) 来实例化的
private Runner runner;


@BeforeTest
public void setUp() throws Exception{
//定义一个Server 指定端口
ContentResource resource = Moco.file("src/test/resources/foo.json");

HttpServer server = MocoJsonRunner.jsonHttpServer(12306, resource);

//初始化runner对象
runner = Runner.runner(server);
//开启服务器
runner.start();


}

@Test
public void testRequset() throws ClientProtocolException, IOException{

Content content = Request.Get("http://localhost:12306").execute().returnContent();
System.out.println(content.asString(Charset.forName("UTF-8")));


}

@Test
public void testRequset1() throws ClientProtocolException, IOException{

Content content = Request.Get("http://localhost:12306").execute().returnContent();
System.out.println(content.asString(Charset.forName("UTF-8")));


}


@AfterTest
public void tearDown(){
//关闭服务器
runner.stop();

}
}

 

依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.java.mock</groupId>
  <artifactId>mocker</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.9.10</version>
    <scope>test</scope>
</dependency>
<dependency>
  <groupId>com.github.dreamhead</groupId>
  <artifactId>moco-core</artifactId>
<!--   <version>1.0.0</version> -->
  <version>1.1.0</version>
</dependency>

 
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>fluent-hc</artifactId>
            <version>4.5</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>


    </dependencies>
<build>
<plugins>
  
<!--   确定jdk版本的编译插件 -->
         <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>3.5.1</version>
               <configuration>
                   <source>1.8</source>
                   <target>1.8</target>
                   <encoding>UTF-8</encoding>
               </configuration>
        </plugin>
        </plugins>
       
</build>

</project>

 

 

json文件

[
  {
    "response" :
      {
        "text" : "foo"
      }
  }
]

 

总结:从外部读取json文件作为响应,核心是用moco-runner依赖中的这个api MocoJsonRunner.jsonHttpServer(12306, resource),其余怎么发请求,怎么用runner来管理server,跟之前一样,么有什么特别的

标签:dreamhead,读取,runner,demo,import,org,moco,com
来源: https://www.cnblogs.com/wangzhiqiang004/p/12652450.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有