Skip to content

Reduce Code duplication in tests and make tests easier to read#39

Merged
oleg-nenashev merged 1 commit into
wiremock:mainfrom
bitxon:feature/test-utils
May 18, 2023
Merged

Reduce Code duplication in tests and make tests easier to read#39
oleg-nenashev merged 1 commit into
wiremock:mainfrom
bitxon:feature/test-utils

Conversation

@bitxon
Copy link
Copy Markdown
Contributor

@bitxon bitxon commented May 15, 2023

Description

In every test method we have duplicated code block

final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create(wiremockServer.getUrl("/json-body-transformer")))
    .timeout(Duration.ofSeconds(10))
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();

This code block and similar once were extracted into testsupport utility class TestHttpClient
Result test you can see bellow:

// given
String url = wiremockServer.getUrl("/json-body-transformer");
String body = "{\"name\":\"John Doe\"}";

// when
HttpResponse<String> response = TestHttpClient.newInstance().post(url, body);

// then
assertThat(response.body())
        .as("Wrong response body")
        .contains("Hello, John Doe!");

References

N/A

Submitter checklist

  • The PR request is well described and justified, including the body and the references
  • The PR title represents the desired changelog entry
  • The repository's code style is followed (see the contributing guide)
  • Test coverage that demonstrates that the change works as expected
  • For new features, there's necessary documentation in this pull request or in a subsequent PR to wiremock.org

@bitxon bitxon requested a review from oleg-nenashev as a code owner May 15, 2023 13:19
Copy link
Copy Markdown
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I would probably just use org.wiremock.integrations.testcontainers.test.util but fine with the current package too. Thanks!

@oleg-nenashev oleg-nenashev added the chore Maintenance label May 18, 2023
@oleg-nenashev oleg-nenashev merged commit 9dff6c2 into wiremock:main May 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Maintenance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants