外部ファイルのデモからの情報の読み取りに対応モコ

 
 

パッケージあざけります。
インポートにjava.io.IOException;
輸入いるjava.nio.charset.Charset;

 
 

輸入org.apache.http.client.ClientProtocolException。
輸入org.apache.http.client.fluent.Content;
輸入org.apache.http.client.fluent.Request;
輸入org.testng.annotations.AfterTest;
輸入org.testng.annotations.BeforeTest;
輸入org.testng.annotations.Test;

 
 

com.github.dreamhead.moco.HttpServerインポート、
インポートcom.github.dreamhead.moco.Moco;
インポートcom.github.dreamhead.moco.MocoJsonRunner;
インポートcom.github.dreamhead.moco.Runner;
インポートcom.github.dreamhead .moco.resource.ContentResource;
publicクラスMocoJsonHttpRunnerTest {
サーバスイッチによって管理//ランナー、このランナーのオブジェクトをインスタンス化するRunner.runner(サーバーサーバー)で
プライベートランナーランナーを。

 
 

@BeforeTest
ます。public voidセットアップが()例外をスロー{
//定义一个Serverの指定端口
ContentResourceリソース= Moco.file( "SRC /テスト/リソース/ foo.json");

HttpServerをサーバ= MocoJsonRunner.jsonHttpServer(12306、リソース)。

//初始化ランナー对象
ランナー= Runner.runner(サーバー)。
//开启服务器
runner.start();


}

@Test
公共ボイドtestRequset()ClientProtocolExceptionは、IOException {スロー

コンテンツ内容= Request.Get( "HTTP:// localhostを:12306").execute()returnContentを()。
System.out.println(content.asString(Charset.forName( "UTF-8")));


}

@Test
公共ボイドtestRequset1()ClientProtocolException、IOExceptionが{スロー

コンテンツ内容= Request.Get( "のhttp:// localhostを:12306").execute()returnContent();。
System.out.println(content.asString(Charset.forName( "UTF-8")));


}


@AfterTest
公共ボイドティアダウン(){
//关闭服务器
runner.stop()。

}
}

 

頼ります

<プロジェクトの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。モック</のgroupId> 
  <たartifactId>あざける者</たartifactId> 
  <バージョン> 0.0.1-SNAPSHOT </バージョン> 
  <依存性> 
<依存性> 
    <のgroupId> org.testng </のgroupId> 
    <たartifactId> TestNGの</たartifactId> 
    <バージョン> 6.9.10 </ version>の
    <スコープ>テスト</スコープ> 
</依存関係> 
<依存> 
  <groupIdを> COM。github.dreamhead </ groupIdを> 
  <たartifactId>モココア</たartifactId> 
<! - <バージョン> 1.0.0 </バージョン> - > 
  <バージョン> 1.1.0 </バージョン>
</依存関係> 

 
        <依存> 
            <groupIdを> org.apache.httpcomponents </ groupIdを> 
            <たartifactId>流暢-HC </たartifactId> 
            <バージョン> 4.5 </ version>の
        </依存関係> 
 
 
 
 
 
        < -のhttps:!// mvnrepository .COM /アーティファクト/ com.github.dreamhead /モコランナー- > 
        <依存性> 
            <のgroupId> com.github.dreamhead </のgroupId> 
            <たartifactId>モコランナー</たartifactId> 
          <バージョン> 1.1.0 </バージョン> 
        </依存関係> 


    </依存関係> 
<ビルド> 
<プラグイン> 
  
<! -确定JDK版本的编译插件- > 
         <プラグイン>
               <groupIdを> org.apache.maven.plugins </ groupIdを> 
               <たartifactId> Mavenのコンパイラ・プラグイン</たartifactId> 
               <バージョン> 3.5.1 </バージョン> 
               <構成> 
                   <ソース> 1.8 </ソース> 
                   <ターゲット> 1.8 </ target>を
                   <エンコーディング> UTF-8 </エンコーディング> 
               </ configuration>の
        </プラグイン> 
        </プラグイン> 
       
</ビルド> 

</プロジェクト>

 

 

JSONファイル

[ 
  {
     "レスポンス" 
      {
         "テキスト": "FOO" 
      } 
  } 
]

 

概要:JSONレスポンスなどの外部ファイルから読み込む、コアはこのAPI MocoJsonRunner.jsonHttpServer(12306、リソース)にモコランナーに依存して、要求の残りの部分は作られたか、どのようにサーバーを管理するためのランナー、前と同じ、それが何であります特別な

おすすめ

転載: www.cnblogs.com/wangzhiqiang004/p/12652450.html