Minimal Pact consumer test with Clojure
https://docs.pact.io/implementation_guides/jvm/consumer/junit#using-the-pact-dsl-directly
code:clojure
(ns pact-test-clj
(:require clojure.test :refer :all
aleph.http :as http
manifold.deferred :as d)
(:import au.com.dius.pact.consumer ConsumerPactBuilder PactTestRun
au.com.dius.pact.consumer.model MockProviderConfig
au.com.dius.pact.consumer ConsumerPactRunnerKt))
(deftest basic-pact-test
(ConsumerPactRunnerKt/runConsumerTest
(-> (ConsumerPactBuilder/consumer "Consumer")
(.hasPactWith "Provider")
(.uponReceiving "a request to say Hello")
(.path "/hello")
(.method "POST")
(.body "{\"name\": \"harry\"}")
(.willRespondWith)
(.status 200)
(.body "{\"hello\": \"harry\"}")
(.toPact))
(MockProviderConfig/createDefault)
(reify
PactTestRun
(run this mock-server context
(let [{:keys body status}
@(http/post (str (.getUrl mock-server) "/hello")
{:body (cheshire.core/encode {:name "harry"})})]
(is (= {:hello "harry"} (cheshire.core/decode (slurp body) keyword)))
(is (= 200 status)))))))
難しかった点
PactTestRun
is何
https://www.javadoc.io/doc/au.com.dius/pact-jvm-consumer_2.12/3.6.5/au/com/dius/pact/consumer/PactTestRun.html
ただのinterfaceだったのでreifyでok
import au.com.dius.pact.consumer.junit.exampleclients.ProviderClient;
=> mvn artifactには入っていない (class not found)
=> https://github.com/pact-foundation/pact-jvm/blob/7c41fc10492e15e3886c8d1cfd7dbcc582f95cc5/consumer/src/test/java/au/com/dius/pact/consumer/ConsumerClient.java#L13
ソースにあたると要はhttpクライアントをラップしているだけだったっぽいのでaleph.httpを使った
Pact (contract testing framework)
export pact json from consumer