1

We used to have wiremock for integration tests with both happy and unhappy paths. Now we are trying to move to Spring cloud contract based integration tests. Though, I could not find any document related to contracts for unhappy paths (status code over 400). And I did some POC with status code 4xx/5xx in response but it didnot work.

Anyone knows the best practice to handle unhappy paths in consumer side? or is it not supported at all for unhappy paths with status code over 400 with spring cloud contract?

  • The unhappy paths work in the very same way as the happy ones. Remember that Spring Cloud Contract, from the DSL / MockMvc test generates WireMock stubs in the very same way as you would create them via WireMock API. So if the stub says that it would return 400 when some data is sent into it then that should happen on the consumer side too. Maybe you should upload your sample somewhere so that I can try and help you? – Marcin Grzejszczak Feb 16 '18 at 20:22

1 Answers1

1

Here is an example:

Producer side

Contract.make {
    description 'get 404 when entity was not found'
    request {
        method GET()
        url '/entities/0'
    }
    response {
        status NOT_FOUND()
    }
}

Client side

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SomeApplication.class)
@AutoConfigureStubRunner(ids = "io.app:entity:+:stubs:8080")
@AutoConfigureTestDatabase
public class EntityClientTest {

    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Autowired
    private EntityClient entityClient; // This is a FeignClient

    @Test
    public void shouldThrowNotFoundWithInvalidId() {
        exception.expect(FeignException.class);
        exception.expectMessage("404");

        entityClient.getById(0);
    }
}

As you can see, the getById thrown a 404 because the contract says so.

Rafael Renan Pacheco
  • 2,138
  • 21
  • 22