1
votes

I would like to use Spring Cloud Contract (https://spring.io/projects/spring-cloud-contract) in order to test frontend to backend interactions: especially to catch such errors as 400 http errors.

I was able to run my stubs with the spring cloud contract stub runner. However I noticed that when the actual backend would return a 400, the running stubs return a 404 not found error.

Here is my contract:

description: |
    Signup use case
    ```
    given:
       a user signs up
    when:
       the sign up request is valid
    then:
       the user is signed up
     ```
request:
    method: POST
    url: /api/signup
    body:
        userAccountType: PARENTS
        email: [email protected]
        firstName: John
        plainPassword: secret
        address:
            placeId: 'an_id'
            description: '10 Downing Street'
    headers:
        Content-Type: application/json
response:
    status: 200

If my frontend (i.e. Angular) just issues a Http POST with, say the email field missing, then I expect the running stubs to return a 400.

I would be grateful if someone could share best practices or tips in order to better leverage Spring Cloud Contract for the purpose of frontend/backend tests.

1
If you get 404 that means that WireMock couldn't find a stub. That means that your request was not matched with a WireMock stub. - Marcin Grzejszczak
I see. But then how can I ensure my consumer (here a frontend) does not send invalid requests to my producer API that would result in 400? Do I have to write contracts (with status 400) for each of the combinations of invalid requests? - balteo
You should create another contract with the missing field and mark it with status code 400 - Marcin Grzejszczak
Then there would be as many status-code-400 contracts as invalid request combinations? - balteo
If that's your business requirement then yes. With groovy you can extract most of the setup and return a list of such contracts - Marcin Grzejszczak

1 Answers

0
votes

Although I agree with what Marcin said in the comments...

If you get 404 that means that WireMock couldn't find a stub. That means that your request >was not matched with a WireMock stub.

You should create another contract [for each invalid request] with [a] missing field and >mark it with status code 400

... there might be a way to cheat a little bit with priority

You could create a low-priority contract for any request that hits the correct URL to returns 400. On its own, this means every call to that URL would return a 400.

Then create contracts that hit the right URL with the right parameters to return 200 and the expected response and set their priority to high. Since these contracts overlap, the priority ensures that the 200 gets returned and not the 400.

Any other URL will still return 404.

Disclaimer: I have not actually tried this out myself.