GithubHelp home page GithubHelp logo

demo-cdc's Introduction

demo-cdc

Spring Cloud Contractの利用サンプル。

通常利用と異なる点は、Consumerテスト時のスタブをConsumerのテストプロセスとは別プロセスに切り出していること。Spring Cloud Contractでは、Consumerテスト時はConsumerのJUnitテストクラス内でスタブサーバを展開することが多いが、JUnit以外のテストを行う場合や、スタブをコンテナ等にデプロイして利用したい場合を想定し、本サンプルを作成した。

構成

構成図

手順

Producer側

Contractからテストやスタブを生成、ローカルリポジトリにinstall

便宜上、ContractはProducer側に格納している。 demo-cdc-producerで以下のように、Spring Cloud Contract Maven Pluginを実行する。

$ mvn clean spring-cloud-contract:generateTests install

これにより以下が実行される。

  • Producerに対するContract適合検証テストクラス生成
  • 上記のテスト実行[図の②]
  • Contractに対応したスタブ資材のローカルリポジトリ(.m2)へのinstall[図の③]
    • スタブ資材の実体は、Contractファイルと、Contractから生成されたスタブサーバ(WireMock)向け定義ファイル

Contract適合検証テストがパスし、mvnがSUCCESSし、.m2 にスタブのjar(xxx-stubs.jar)が出力されていることを確認。

Consumer側

スタブを起動

冒頭で示したよう本サンプルでは、スタブサーバは別PJのdemo-cdc-stubに切り出し、独立したプロセスとして動くようにしている。

demo-cdc-stubで以下でSpring Boot Appを起動すると、

$mvn spring-boot:run

以下のパスのように、8083ポートでスタブが起動する。[図の④]

http://localhost:8083/producer

これは、Spring Cloud Contract Stub Runnerの機能により、 先ほどローカルリポジトリ(.m2)にinstallしたスタブjar(xxx-stubs.jar)をDLし、そのスタブ定義をもとにスタブサーバを起動している。 スタブサーバ自体はWireMockであり、Spring Cloud Contract Stub Runnerに組み込まれて動作している。

本サンプルはローカルリポジトリから読み込むモードとしているが、取得元をRemote Repositoryに変更することも可能であり、他人と分業している場合はそちらのほうが望ましい。

スタブを用いてConsumerをテスト

demo-cdc-consumerDemoCdcApplicationTests(Consumerの疎通テストケース)をJUnit実行。 Consumerの通信向き先について、src/test/resources/applciation.propertiesでスタブのURL、http://localhost:8083/producerに向けている。)

スタブが起動していることで、DemoCdcApplicationTestsの疎通テストが成功することを確認。[図の⑤]

また、スタブサーバにもHTTPリクエストが届き、応答したことを示すログが出力されるので、合わせて確認する。[図の⑥]

参考

https://iikanji.hatenablog.jp/entry/2020/08/04/235009

demo-cdc's People

Contributors

ikeyat avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.