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;
com.github.dreamhead.moco.HttpServer Import;
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 managed by the server switch, the object of this runner is Runner.runner (server server) to instantiate the
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();
}
}
rely
<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> <plugin> <-! jdk determine the version of the compiler plug-ins -> <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 file
[ { "response" : { "text" : "foo" } } ]
Summary: read from an external file as json response, the core is dependent on moco-runner in this api MocoJsonRunner.jsonHttpServer (12306, resource), how the rest of the request made, how a runner to manage server, the same as before, what is it there special