Search code examples
javaspringtestingspring-cloudstubbing

How to get service state on spring cloud contract stub


Studying spring cloud contract have a question about testing statefull services.

Have two services: Server and Client. Server has a state, in my case it has repository with users. I can test Server states with spring cloud contract using scenarios.

My second step is using Server-stub for Client testing. In this case I'd like to have Server-stub with one (or more) users. I can make this contract (just imagine Service always has users), but it will be another one.

What is the way to use the same contract to verify Server answers (firstly Server has zero state) and stubbing it while Client tests(when I need stub with some state)?

UPD Closer to my case: My Server contains users, which can be created/updated without participation of Client. For component testing Server I created contract scenario "Create user". Than I'd like to test Client, but it need Server to contain at least one user, i.e. got some special state on Server-stub. What shall I do in this case?

  1. Have a different contracts to test Server and Client. In this case, how can I verify, that my client-contract is true.
  2. Some how play a scenario or put Server-stub in state having one user.

PS tried to add tag spring-cloud-contract, by rating is not enough.


Solution

  • Yes so the answer to this problem is the fact that when having a controller you should delegate all the work to a service. That service should be mocked. Spring Cloud Contract is about contract tests, not exact replication of business features. You can read more about this sourish in the excellent blog entry from Stephane Nicoll https://spring.io/blog/2016/08/30/custom-test-slice-with-spring-boot-1-4