JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
Consideration:
Don't directly return a RequestResponsePact but a kPact owned type (let's call it KPactResult here) with a toPact() method.
This KPactResult should be a data class like type which allows to copy or maybe even mutate it's state (Design decision: immutable or not).
This would allow something like:
val pact =KPact between "Some Consumer"and"Some Provider" isDefinedBy {
given providerState "a certain state on the provider" then {
whenever receiving "a request for something" withPath "/hello"and {
method("POST")
body("{\"name\": \"harry\"}")
} thenRespondWith {
status(200)
body("{\"hello\": \"harry\"}")
}
}
}
// mutable
pact.consumer ="other Consumer"// immutable
pact.copy(consumer ="other Consumer")
// first proposals, maybe there are better ways and terminologies//replace given block
pact.definition.given["a certain state on the provider"] = {
whenever receiving "a request for something" withPath "/hello"and {
method("POST")
body("{\"name\": \"harry\"}")
} thenRespondWith {
status(400)
body("ERROR")
}
}
//add new given block
pact.definition += given providerState "2nd state" then {
whenever receiving "a request for something" withPath "/hello"and {
method("POST")
body("{\"name\": \"harry\"}")
} thenRespondWith {
status(200)
body("{\"hello\": \"harry\"}")
}
}
The trade-off is that you have to call .toPact() to get the RequestResponsePact
val pact:RequestResponsePact=KPact between "Some Consumer"and"Some Provider" isDefinedBy {
given providerState "a certain state on the provider" then {
whenever receiving "a request for something" withPath "/hello"and {
method("POST")
body("{\"name\": \"harry\"}")
} thenRespondWith {
status(200)
body("{\"hello\": \"harry\"}")
}
}
}.toPact()
Support nested block and flat call structure with invoke method (see Kotlin in Action 11.3.3).
KPact.consumer("Some Consumer").hasPactWith("Some Provider") {
given("a certain state on the provider") {
whenever("a request for something").withPath("/hello") {
method("POST")
body("{\"name\": \"harry\"}")
} thenRespondWith {
status(200)
body("{\"hello\": \"harry\"}")
}
}
vs
KPact.consumer("Some Consumer").hasPactWith("Some Provider")
.given("a certain state on the provider").whenever("a request for something")
.withPath("/hello") {
method("POST")
body("{\"name\": \"harry\"}")
} thenRespondWith {
status(200)
body("{\"hello\": \"harry\"}")
}
}