Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extreme syntactic sugar - allow tests to use same model as we do for rest clients #69

Open
holly-cummins opened this issue Feb 4, 2023 · 0 comments

Comments

@holly-cummins
Copy link
Contributor

holly-cummins commented Feb 4, 2023

When we define a rest client mock, the suggested pattern is this:

    @InjectMock
    @RestClient
    Client mock;

    @BeforeEach
    public void setUp() {
        when(mock.get()).thenReturn("MockAnswer");
    }

It would be nice to be able to do something similar for Pact, something like:

    @InjectPactMock
    @RestClient
    Client contractMock;

    @BeforeEach
    public void setUp() {
        when(contractMock.get()).thenReturn("MockAnswer");
    }

(Or perhaps it would still be in a @Pact block, but as concise.)

The current pattern is much more verbose, and almost all of the content (ports, content type headers, endpoints, schema for return and input parameters ... ) is in there is already in the RestClient definition:

    @Pact(provider = "farmer", consumer = "knitter")
    public V4Pact createPact(PactDslWithProvider builder) throws JsonProcessingException {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");

        // Here we define our mock, which is also our expectations for the provider
        String sweaterBody = "{\"colour\":\"white\"}\n";
        return builder.given("test POST")
                .uponReceiving("POST REQUEST")
                .path("/wool/order")
                .method("POST")
                .willRespondWith()
                .status(200)
                .headers(headers)
                .body(sweaterBody)
                .toPact(V4Pact.class);
    }

Things that aren't in the client that would need to be defined in the equivalent of when(contractMock.get()).thenReturn("MockAnswer") are the various states (given), and the response values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant