I have a restFul API http://localhost:8080/books/{id} which return a book with a particular ID.
ex:
GET http://localhost:8080/books/1
will return
{ "id" : 1, "pages" : 20, "price" : 100 }
GET http://localhost:8080/books/2
will return
{ "id" : 2, "pages" : 30, "price" : 120 }
I have written my consumer expectations using regex in place of {id} and have generated the PACT.
There is no data present in the provider now. (no book information in present in the database). When I run pact verfication on provider side it fails since it is not able to get any information by hitting the actual service to cross check the contract.
Can the contract be verified without data being present in the provider database? or is there any workaround for the same?
Also the DB of provider can change if we deploying the provider into multiple environments, how to handle this?
Appreciate the help. Thanks.
Why would you want to workaround that? The point of contract testing is so that you can verify that both sides of the contract are properly met!
You might want to look at Provider States [1] for this.
Which language are you using?
The article provided gives you some background. In JVM, you can look at the @State annotation https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-junit#example-of-http-test.
In your consumer, you would specify a state such as "Given a book with ID 1 exists". On the provider side, the framework will invoke the @State annotation corresponding to this expectation. This gives you the opportunity to ensure any state data exists (in this case, that book) before the test case runs.