2

I am trying to adopt Pact. I understand the consumer side of the equation and it looks very nice. But I am confused about the producer side.

It seems the documentation advocate running the provider app, and verifying the contracts against a running server.

I prefer not to do it. First, I need to curate a database with proper information for each pact, which is painful to say the least. Second, starting up the application is going to be a hassle - did I mention it is a monolith? -, finally, there are POSTS which are going to mutate the state of the database, and make test running brittle.

What I want to do is to do a mockMvc style testing with the pacts. I would like to mock my services, and just test the endpoint, which I think what should be tested in this case.

How can I achieve this with Pact?

Arash
  • 11,697
  • 14
  • 54
  • 81

3 Answers3

1

Well if you don't test your contracts against your Provider that loses the whole point of Contract testing, since your contracts aren't tested against both sides. Because the main point is that Consumers dictate how the Provider should behave and in your case you would like to bypass the provider with a mockMvc and there is no point for doing contract testing only against your Consumer not the Provider. Even though your Provider is a monolith it's still better to run it and test with a contract, then to run all the microservices for end-to-end testing.

cotnic
  • 158
  • 2
  • 11
  • I want to test it against provider, but with mock services. I don't want to bootstrap a running application to do the contract testing. I can just run my endpoint, and mock every thing behind it. – Arash Jul 25 '17 at 03:19
  • Well how can you mock test it against a service, if the service is the provider? You can not test if it is working without deploying it. Maybe this could help you get yourself where you want if you have your API documented: https://bitbucket.org/atlassian/swagger-request-validator – cotnic Jul 25 '17 at 06:34
  • In a proper implementation, service layer contains application logic to prepare data for controllers.. and its up to controller how data are presented to client. So, mocking service layer for data should be fine for contract testing. Purpose of contract testing is not to test service layer or complete e2e api, but only that application is returning data to client in an agreed format. – Kumar Gaurav May 05 '20 at 01:53
1

Yes you can achieve it with PACT, however I have the same opinion with Cotnic that it beats the purpose of having PACT on provider side. The main purpose of PACT is to verify that your server as the provider is working as according to the agreement (PACT). Therefore in my opinion the proper way to use PACT as a CONTRACT is by running it against a fully deployed server, and using @State to "Prepare" the Server (db, startup applications, etc)

Anyway, if you are using Spring, you probably can have a look at this sample for using Pact with MockMvc

https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-spring

Irwan Hendra
  • 150
  • 10
1

Pact-JVM now supports Spring mockmvc tests to verify a Spring or Springboot provider. See https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-spring