首页 > 其他 > 详细

moco 接口测试

时间:2020-04-07 10:03:20      阅读:91      评论:0      收藏:0      [点我收藏+]

帮助文档:https://github.com/dreamhead/moco/blob/master/moco-doc/usage.md#dependency

1、简单案例:创建一个简单的httpServer,用org.apache.http.client.fluent.Request发送请求

package mocker;

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

import com.github.dreamhead.moco.HttpServer;
import com.github.dreamhead.moco.Moco;
import com.github.dreamhead.moco.Runnable;
import com.github.dreamhead.moco.Runner;

public class Testnger {
   
    @Test
    public void should_response_as_expected() throws Exception {
        HttpServer server = Moco.httpServer(12306);

        server.response("hello laowang");
        Runner.running(server, new Runnable() {
            @Override
            public void run() throws Exception {
          Content content =  Request.Get("http://127.0.0.1:12306").execute().returnContent();
          System.out.println(content.asString());
               
            }
        });
       
        Thread.sleep(1000000);

      
    }
}

 

2、用Runner对象来管理Server

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.Runner;

public class ControllServer {
   
    //用runner来管理服务器的开关
    private  Runner runner;
   

    @BeforeTest
    public void setUp(){
       
         HttpServer server = Moco.httpServer(12306);
         server.response("foo");
         //初始化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();
       
    }

}

moco 接口测试

原文:https://www.cnblogs.com/wangzhiqiang004/p/12651621.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!